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

Share:
16,862
Namit
Author by

Namit

Updated on June 04, 2022

Comments

  • Namit
    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 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.

  • Namit
    Namit almost 11 years
    For 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
    Namit almost 11 years
    Should 'Accept': 'application/json' be 'Content-type: application/json' instead?
  • Brad
    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
    Namit almost 11 years
    This is what it says: parse error, expecting ')'' on the line with 'Accept' : ...
  • Brad
    Brad almost 11 years
    Oops, sorry, wrong language. : should be => instead. Fixed.
  • KathyA.
    KathyA. about 8 years