PHP-Curl-Request Rückgabe von Daten in application/json

Ich versuche, einige Daten aus einer API und ich bin immer wieder in dem moment, als XML.

Ich würde es vorziehen, es als jSON und die API-Doc ' s sagen, dass es in jSON sowie XML.

Die Docs sagen...

Die API unterstützt derzeit zwei Arten von Antwort-format: XML und JSON

Können Sie das gewünschte format über die HTTP-Accept-header in der Anfrage:

Accept: application/xml
Akzeptieren: application/json

So wie Generiere ich den Accept-Header von applixation/json in meinem PHP-code?

Mein PHP-code im moment lautet :

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;

Das echo Ergebnis ist einfach nur die Rückgabe von XML-formatierten Daten.

Vielen Dank im Voraus.

  • Die docs sagen Accept aber Sie sind mit Content-Type im CURLOPT_HTTPHEADER
  • Verwenden curl_setopt($ch, CURLOPT_HTTPHEADER, array ("Accept: application/json'));
InformationsquelleAutor StuBlackett | 2014-07-24
Schreibe einen Kommentar