I am trying to get some data from the API and I am returning it as XML at the moment.
I would prefer jSON and the Doc API to say that it is available in jSON as well as XML.
Documents say ...
Currently, the API supports two types of response formats: 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 can I generate Accept in the applixation / json application header 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;
An echo of the result simply returns the data in XML format.
Thanks in advance.
api php curl
Stublacktt
source share