0

I am trying to retrieve data from a server which usually returns it in XML, however I trying to request it in a JSON format (if requested correctly it will return the data in JSON).

$header = array(
           'http' => array(
             'header'=>"Content-type: application/json"
           ),
        );

$response = file_get_contents($query, false, $header);
print_r($response);

This approach was taken from here. Currently the program does not return anything. Does anyone spot any potential problems with this?

Community
  • 1
  • 1
Namit
  • 1,314
  • 8
  • 20
  • 36
  • Whoever manages the server or application you are requesting from should be able to tell you how to properly request JSON for that particular server or application. – Sajan Parikh Jul 02 '13 at 17:26
  • @SajanParikh: The HTTP standard also tells you how. http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 – SLaks Jul 02 '13 at 17:28
  • @SLaks of course, but the application or server you're requesting from has to honor that. – Sajan Parikh Jul 02 '13 at 17:30

1 Answers1

3

You need to set the HTTP Accept header to tell the server that you want it to give you JSON:

Accept: application/json

(assuming that the remote server is correctly implemented to read the header)


The Content-Type request header indicates the type of the payload that you are POSTing.
In your case, it does not apply, since you're sending a GET request.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964