I am trying to get some data from an API and I am getting it back at the moment as XML.
I would prefer it as jSON and the API Doc's say that it is available in jSON aswell as XML.
The Docs say...
The API currently supports two types of response format: XML and JSON
You can specify the desired format using the HTTP Accept header in the request:
Accept: application/xml Accept: application/json
So how do I generate the Accept Header of applixation/json in my PHP code?
My PHP code at the moment is :
header('Content-Type: application/json');
$endpoint = "http://api.api.com";
// Initiate curl
$ch = curl_init();
// Set The Response Format to Json
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json'));
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$endpoint);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
echo $result;
The echo of result is just returning XML formatted data.
Thanks in advance.
Accept
but you are usingContent-Type
inCURLOPT_HTTPHEADER
– Absentminded