Home > Back-end >  php curl_exec returns "HTTP/1.1 100 Continue" followed by "HTTP/1.1 404 Not Found&quo
php curl_exec returns "HTTP/1.1 100 Continue" followed by "HTTP/1.1 404 Not Found&quo

Time:06-30

I am trying to convert the below curl command which gives me JSON response to PHP curl code.

curl -X POST --tls-max 1.2 --insecure https://xxxxxxxxx.com/getkey --cert client.crt --key client.key --header "Content-Type: application/json" --data "{\"param1\": \"value1\", \"param2\": \"value2\"}"

My PHP code is,

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, "https://xxxxxxx.com/getkey");
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);

curl_setopt($curl, CURLOPT_SSLCERT, "/var/www/html/certs/client.crt");
curl_setopt($curl, CURLOPT_SSLKEY, "/var/www/html/certs/client.key");
 
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_MAXREDIRS, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$header = array('Content-Type: application/json');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

curl_setopt($curl, CURLOPT_VERBOSE, TRUE);

$result = curl_exec($curl);

curl_close($curl);

return $result;

When I run this PHP code I am getting below error.

* About to connect() to xxxxxxx.com port 443 (#1)
*   Trying xx.xxx.xxx.xxx...
* Connected to xxxxxxx.com (xx.xxx.xxx.xxx) port 443 (#1)
* skipping SSL peer certificate verification
* SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate:
*       subject: CN=*.xxxxx.com,O="xxxxxxxxxx.",L=xxxxxxxx,ST=xxxxxx,C=US
*       start date: Nov 10 00:00:00 2021 GMT
*       expire date: Nov 10 23:59:59 2022 GMT
*       common name: *.xxxxx.com
*       issuer: CN=DigiCert TLS RSA SHA256 2020 CA1,O=DigiCert Inc,C=US
> POST /getkey HTTP/1.1
Host: xxxxxxx.com
Accept: */* 
Content-Length: 272
Expect: 100-continue
Content-Type: application/json; boundary=----------------------------d7326044efb7

* skipping SSL peer certificate verification
* NSS: client certificate from file
*       subject: CN=XXX-Dev-Client
*       start date: Sep 09 04:00:00 2020 GMT
*       expire date: Sep 09 04:00:00 2040 GMT
*       common name: XXX-Dev-Client
*       issuer: CN=CARoot-XXX-Dev
< HTTP/1.1 100 Continue
< HTTP/1.1 404 Not Found
< Connection: Close
< Content-Type: application/json
<
* Closing connection 1

What is the setting I am missing to get the 100 and then 404 codes.

Note: When I execute the curl command using shell_exec() I am getting the json response, but when I use only PHP curl I am getting the mentioned error.

CodePudding user response:

Try to disable sending Expect: 100-continue:

$header = array('Expect:', 'Content-Type: application/json');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

The same for curl for test purposes

curl -X POST --tls-max 1.2 --insecure https://xxxxxxxxx.com/getkey --cert client.crt --key client.key -H "Expect:" -H "Content-Type: application/json" --data "{\"param1\": \"value1\", \"param2\": \"value2\"}"

You seem to post JSON incorrectly. The correct post data

$payload = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);

CodePudding user response:

Thanks @273K for the solution.

The reason for this error is, I didn't convert the POST data to JSON format. After converting the POST data to JSON format using json_encode it worked and I am able to get the response from the URL.

  • Related