我正在尝试将Wordpress中的json发送到名为Chorus的法语管理服务器中。
This is the API Chorus link to know exactly what I want to do.
卷曲类
class Curl
{
protected $url;
protected $header = array();
protected $data;
public function __construct( $url, $header, $data )
{
$this->url = $url;
$this->header = $header;
$this->data = $data;
}
public function sendPostJsonData( string $ssl_cert = null, string $ssl_certtype = null, $ssl_keypwd = null ){
$json_data = json_encode( $this->data, JSON_FORCE_OBJECT );
if( isJson( $json_data ) ){
$this->header[] = sprintf( '%s: %s', "Content-Type", "application/json" );
$this->header[] = sprintf( '%s: %s', "Content-Length", strlen( $json_data ) );
$ch = curl_init( $this->url );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header );
if( !empty( $ssl_cert ) ){
$tempPemFile = tmpfile();
fwrite( $tempPemFile, $ssl_cert );
$tempPemPath = stream_get_meta_data( $tempPemFile );
$tempPemPath = $tempPemPath['uri'];
curl_setopt( $ch, CURLOPT_SSLCERT, $tempPemPath );
}
if( !empty( $ssl_certtype ) )
curl_setopt( $ch, CURLOPT_SSLCERTTYPE, $ssl_certtype );
if( !empty( $ssl_key ) )
curl_setopt( $ch, CURLOPT_SSLKEYPASSWD, $ssl_keypwd );
if( ! $result = curl_exec($ch) )
{
$result = curl_error($ch);
}
curl_close( $ch );
return $result;
}else{
return false;
}
}
}
由于某种原因,我收到一个HTML响应,其中包含 400错误的请求。
原始请求
* Hostname was NOT found in DNS cache
* Trying 185.24.185.61...
* Connected to chorus-pro.gouv.fr (185.24.185.61) port 5443 (#0)
* successfully set certificate verify locations:
* CAfile: none CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / DES-CBC3-SHA
* Server certificate:
* subject: C=FR; ST=93; L=Noisy Le Grand; O=AGENCE POUR L'INFORMATIQUE FINANCIERE DE L'ETAT; 2.5.4.97=NTRFR-130019771; OU=0002 130019771; serialNumber=55846EKG620; CN=chorus-pro.gouv.fr
* start date: 2019-02-22 14:43:00 GMT
* expire date: 2021-02-21 14:43:00 GMT
* subjectAltName: chorus-pro.gouv.fr matched
* issuer: C=FR; O=Certinomis; 2.5.4.97=NTRFR-433998903; CN=Certinomis - Web CA
* SSL certificate verify ok. > POST /service-qualif/factures/soumettre HTTP/1.1 Host: chorus-pro.gouv.fr:5443 Compte technique:TECH_1111111111@cpp2017.fr Mot de passe:MyPWD Certificat:-----BEGIN PKCS7----- CERTIFICATE CONTENT -----END PKCS7----- Content-Type: application/json Accept: application/json Content-Length: 2193 Expect: 100-continue < HTTP/1.1 400 Bad Request < Date: Mon, 30 Dec 2019 17:23:06 GMT < Content-Length: 226 < Connection: close < Content-Type: text/html; charset=iso-8859-1 <
* Closing connection 0
我的问题是:
答案 0 :(得分:-1)
如果您希望服务获得“漂亮”的响应,则可以检查第三方服务的响应代码。您可以使用try catch来防止错误并通过邮件返回受控的响应
在sendPostJsonData函数中检查响应状态
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
throw new Exception('Invalid response');
}
并在执行结束时发出不同的响应
try {
...
$result = $curl->sendPostJsonData();
...
} catch (Exception $e) {
echo json_encode(array(
'status' => 'error',
'msg' => 'your pretty response'
));
}