Reputation: 1322
While using a certain API to extract information the docs suggest that I need to pass a URL to retrieve data. The data is returned in XML
, however to receive it in JSON
format HTTP Accept header value of 'application/json'
should be specified.
I am trying to achieve this in PHP, e.g. the URL to retrieve information is http://www.example.com?someContent
which returns data in XML.
I am currently using http_get
function from PHP, however, it doesn't seem to be working at all.
Any suggestions on how can I extract information and then also request it in JSON
format.
The above link is invalid. This documentation exists at http_get
Upvotes: 2
Views: 18068
Reputation: 163622
Untested, but this should work. Set the headers option to an array of headers you want to use.
$response = http_get("http://www.example.com/?someContent", array(
'headers' => array(
'Accept' => 'application/json'
)
), $info);
print_r($info);
http://php.net/manual/en/function.http-get.php
Upvotes: 4
Reputation: 9142
Try: $body = http_parse_message(http_get($url))->body;
then you can use $body
.
Alternatively, you can use cURL. Since the result is in JSON, you'll need to parse the XML then output as an array into the json_encode
function.
Upvotes: 0