简体   繁体   English

卷曲SSL请求400错误请求

[英]Curl SSL Request 400 Bad Request

I'm trying to make a request using php and curl. 我正在尝试使用php和curl发出请求。 Heres a working example that I use from command line 这是我从命令行使用的工作示例

curl -k --cacert c:/cert/server_ca.pem  --cert c:/cert/signed_client_cert.pem --key c:/cert/cert_req_rsa_private_key.pem 

heres what I have so far, It returns a 400- bad request, does anyone know how I can get more usefull errors or what I could be doing wrong? 这是我到目前为止的内容,它返回一个400错误的请求,有人知道我如何得到更多有用的错误或我可能做错了什么吗?

ini_set('display_errors',1);
//phpinfo();

$cert = "C:/cert/signed_client_cert.pem";
$key = "C:/cert/cert_req_rsa_private_key.pem";
$pass = "key1123";
$caInfo = "C:/cert/server_ca.pem";


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.apitest.com/v/2/products/04003274058");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CAINFO, $caInfo);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLKEY, $key);
curl_setopt($ch, CURLOPT_VERBOSE, true);
//curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pass);
//curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
//curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $pass);
//curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');


$response = curl_exec($ch);
echo curl_error($ch);
echo var_dump($response);

The curl command is incomplete, so its hard to say what may (or may not) be different. curl命令不完整,因此很难说出可能不同之处。 But from the command line, Curl uses Accept: */* ; 但是在命令行中,Curl使用Accept: */* ; and does not use Content-Type (see the Wireshark capture below for curl www.google.com )). 并且不使用Content-Type (有关curl www.google.com请参见下面的Wireshark截图curl www.google.com )。

For reading on the difference between Accept and Content-Type , see Difference between accept and content-type http headers on Webmasters Stack Exchange. 要了解AcceptContent-Type之间的区别,请参阅Webmasters Stack Exchange上的accept和content-type http标头之间的区别

在此处输入图片说明


Just bike shedding, but this has room for improvement: 只是自行车脱落,但这还有改进的余地:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

If you feel you have to use it like that, then pick an anonymous protocol. 如果您觉得必须那样使用它,请选择一个匿名协议。 It will save the server from sending its certificate since nothing is being verified anyway. 因为无论如何都无法进行验证,所以它将避免服务器发送证书。

奇怪的是,这个问题似乎是Php卷曲的错误,我切换到了httprequest,它起作用了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM