Most APIs let you specify the return type you want with a parameter, ie:
http://api.site.com/get/stuff?output=xml
However, some APIs will return the format that you specify in the HTTP accept header instead. So the URL would always be:
http://api.site.com/get/stuff
But depending on the HTTP Accept header, the API returns different data back.
Here is how to force XML or JSON output for such an API.
XML
$context = stream_context_create(
array(
'http' => array(
'header' => "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n",
)
)
);
$jobs_raw = file_get_contents('http://api.site.com/get/stuff', null, $context);
JSON
$context = stream_context_create(
array(
'http' => array(
'header' => "Accept: application/json, application/javascript\r\n",
)
)
);
$jobs_raw = file_get_contents('http://api.site.com/get/stuff', null, $context);