How to hit a url with the Content-Type header set to application/json.
My code returns “request not successfull” (the else side of the condition). I’m a beginner so please help me understand why this is not working.
Code:
$data_string=json data here;
$ch = curl_init('www.here is url');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if(curl_exec($ch))
echo "request successfull";
else
echo "request not successfull"
curl_close($ch);
Is all you need. Custom request is for the lesser-used HTTP verbs. To find out why the curl request failed, use