Using http_get php
16,862
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
Author by
Namit
Updated on June 04, 2022Comments
-
Namit almost 2 years
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 inJSON
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. -
Namit almost 11 yearsFor some reason, this doesn't seem to work at all (stops page from loading anything), probably some syntax error I think, but I can't find any.
-
Namit almost 11 yearsShould
'Accept': 'application/json'
be'Content-type: application/json'
instead? -
Brad almost 11 years@Namit, No,
Content-Type
is a response header.Accept
is the request header telling the server what content types you will accept. Turn on your error log to see what's going on. Your server may not allow a connection to the outside. -
Namit almost 11 yearsThis is what it says: parse error, expecting ')'' on the line with
'Accept' : ...
-
Brad almost 11 yearsOops, sorry, wrong language.
:
should be=>
instead. Fixed. -
KathyA. about 8 yearsLink is dead. Wayback copy: web.archive.org/web/20160101042724/http://php.net/manual/en/…