0

I'm sending content from a PHP Curl script to an API. I'm using this is to do a POST do my script while passing json headers

 $query = new stdClass; 
 $query->test = 'test';
 $query = json_encode($query);
 $ch = curl_init();         
 curl_setopt($ch, CURLOPT_URL, 'http://localhost');
 curl_setopt($ch, CURLOPT_HEADER, ['Content-Type: application/json', 'Content-Length: '.strlen($query)]);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
 curl_setopt($ch, CURLOPT_VERBOSE, true);               
 $res = curl_exec($ch);
 curl_close($ch);  

But when I trace what the content type of the request in on the API side, I get

 var_dump($_SERVER['CONTENT_TYPE']);
 //application/x-www-form-urlencoded

Shouldn't I get this instead?

 application/json 
1
  • I think, you are SENDING a REQUEST using curl. but I dont I see any RESPONSE Commented Sep 25, 2018 at 1:33

1 Answer 1

2

You should use CURLOPT_HTTPHEADER instead of CURLOPT_HEADER

CURLOPT_HEADER can be true/false and define whether include header to response or not

FYI:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); These lines are redundant as you are not using https

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.