简体   繁体   中英

I am not getting expected response from Curl request in PHP

I have a curl request which is working fine in https://reqbin.com/ and in the Postman but when I implement same in PHP it is not working; Below is plain CURL command;

curl -X POST \
'http://example.com' \
-H 'content-type: application/json' \
-d '{"Header": {"Token": "xyz"}, "Body": {"Email": "abc@gmail.com", "FirstName": "test", "Surname": "test"}}'

The response I am getting from this is;

{"Header":{"Status":"OK"},"Body":{"BackUrl":"https:example.com","Redirect":"YES"}}

But when I implement same in PHP as;

$curl = curl_init();
$headers = array(
    "content-type: application/json"
);

$postData = '{"Header": {"Token": "xyz"}, "Body": {"Email": "abc@gmail.com",  "FirstName": "test", "Surname": "test"}}';

$opt_arr = array(
    CURLOPT_URL => 'http://example.com',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS => $postData
);
curl_setopt_array($curl, $opt_arr);
echo $resp = curl_exec($curl);
curl_close($curl);

I do not receive the expected response;

{"Header":{"Status":"ERROR","ErrorMsg":"INVALID_REQUEST"},"Body":{"BackUrl":"","Redirect":"NO"}}

I need help converting the above working curl to the correct PHP code.

I am not familiar with PHP, regardless of that I see a difference in sending "Header": {"Token": "xyz"} . The curl command sends it in the post data, whereas PHP sends it in the http headers. The PHP variables should be

$headers = array(
    "content-type: application/json"
);
$postDataHeader = array(
    'Token' => 'xyz'
);
$postDataBody = array(
    'Email' => 'abc@gmail.com',
    'FirstName' => 'test',
    'Surname' => 'test'
);
$postData = json_encode(array(
    'Header' => $postDatHeader,
    'Body' => $postDataBody
));

After many unsuccessful attempts in PHP, I tried to implement it in javascript and here I got to know that error is with SSL security, hence I added HTTPS into the URL, and it worked.

$curl = curl_init();
$headers = array(
    "content-type: application/json"
);

$postData = '{"Header": {"Token": "xyz"}, "Body": {"Email": "abc@gmail.com",  "FirstName": "test", "Surname": "test"}}';

$opt_arr = array(
    CURLOPT_URL => 'https://example.com',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS => $postData
);
curl_setopt_array($curl, $opt_arr);
echo $resp = curl_exec($curl);
curl_close($curl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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