当尝试通过PHP Curl发送json时,如何修复400个错误请求?
我正试图将我的Wordpress中的json发送到名为Chorus的法国管理服务器 卷曲类当尝试通过PHP Curl发送json时,如何修复400个错误请求?,php,json,curl,bad-request,Php,Json,Curl,Bad Request,我正试图将我的Wordpress中的json发送到名为Chorus的法国管理服务器 卷曲类 class Curl { protected $url; protected $header = array(); protected $data; public function __construct( $url, $header, $data ) { $this->url = $url; $this->heade
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;
}
}
}
由于某种原因,我得到了一个带有400个错误请求的HTML响应
原始请求
* 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
在DNS缓存中找不到主机名
*正在尝试185.24.185.61。。。
*连接到chorus-pro.gouv.fr(185.24.185.61)端口5443(#0)
*已成功设置证书验证位置:
*CAfile:none CApath:/etc/ssl/certs
*使用TLSv1.2/DES-CBC3-SHA的SSL连接
*服务器证书:
*主题:C=FR;ST=93;L=喧闹的勒格兰德;O=金融信息机构;2.5.4.97=NTRFR-130019771;OU=0002 130019771;序列号=55846EKG620;CN=chorus-pro.gouv.fr
*开始日期:2019-02-22 14:43:00 GMT
*过期日期:2021-02-21 14:43:00 GMT
*主题名称:chorus-pro.gouv.fr匹配
*发行人:C=FR;O=证书;2.5.4.97=NTRFR-433998903;CN=Certinomis-网络CA
*SSL证书验证正常。>发布/服务质量/制作/soumettre HTTP/1.1主机:chorus pro.gouv.fr:5443 Compte技术:技术_1111111111@cpp2017.frMot de passe:MyPWD CERTIFICATE:-----开始PKCS7------证书内容------结束PKCS7------内容类型:应用程序/json接受:应用程序/json内容长度:2193预期:100继续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'
));
}
错误请求的确切含义是:您发送到服务器的请求被视为“错误”。您需要从服务器读取响应(如果有的话),以查看是否指示请求错误的原因。如果没有进一步的信息,请联系API供应商。这是什么?
$ssl\u cert
magic?@MartinBean我无法找到服务器检查为错误请求的原因。我将尝试与他们联系。@AlexBarker,此代码行将更改,因此无所谓。请显示原始请求(必要时使用ngrep或tcpdump)。闻起来有些东西没有正确地urlencode()
ed。