I need to make a POST request to an external http API using curl. When I call the API using the curl command line, it returns the correct data. However, I'm struggling to get the right data when calling it from a php script.
When I use the curl command from the terminal, I get the correct data:
curl -i -H "Accept: application/json" -X POST -d "type_category_id=4" http://example.com/api/
What is the correct format for the CURLOPT_POSTFIELDS in my curl_setopts() in php?
I tried the following (and failed):
<?php
$data = "type_category_id=4";
$ch = curl_init('http://example.com/api/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
$result = curl_exec($ch);
?>