0

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": "[email protected]", "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": "[email protected]", "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.

4
  • But when I implement same in PHP The code for PHP is not identical to the command. "Header": {"Token": "xyz"} is sent differently. Commented Aug 9, 2022 at 14:48
  • @273K Even if I wrap header & body and send it as a whole $postData = '{"Header": {"Token": "xyz"}, "Body": {"Email": "[email protected]", "FirstName": "test", "Surname": "test"}}'; I am getting same error. Commented Aug 9, 2022 at 14:54
  • 1
    Show you attempt in the code. Don't use the comments for that. Commented Aug 9, 2022 at 14:58
  • @273K I have found the solution, I was missing https in URL. Commented Aug 11, 2022 at 12:09

2 Answers 2

3

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' => '[email protected]', 'FirstName' => 'test', 'Surname' => 'test' ); $postData = json_encode(array( 'Header' => $postDatHeader, 'Body' => $postDataBody )); 

and $postData must be set in CURLOPT_POSTFIELDS, not CURLOPT_HTTPHEADER.

fixed PHP example:

<?php $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'http://example.com', CURLOPT_HTTPHEADER => array( 'content-type: application/json' ), CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode(array( 'Header' => array( 'Token' => 'xyz', ), 'Body' => array( 'Email' => '[email protected]', 'FirstName' => 'test', 'Surname' => 'test', ), ), JSON_THROW_ON_ERROR) )); curl_exec($ch); 
Sign up to request clarification or add additional context in comments.

2 Comments

yup you're very close. he also needs to put $postData in CURLOPT_POSTFIELDS rather than CURLOPT_HTTPHEADER :)
should i fix your answer or make one myself? hmm
-1

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": "[email protected]", "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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.