PHP code for GET Request Retrieve JSON Example
This PHP code snippet was generated automatically for the GET Request Retrieve JSON example.<< Back to the GET Request Retrieve JSON example
What is JSON?
JavaScript Object Notation is a text-based, language-independent format for storing and transmitting data over a network. JSON represents four primitive types (strings, numbers, boolean values, and null) and two structured types (objects and arrays). JSON file names use the .json file extension. JSON is used for client/server communication in mobile and web applications written in many programming languages, including JavaScript, Python, Java, C++, C#, Go, PHP, and many others.
What is HTTP GET?
The HTTP GET request method is used to request data-specified resources from the server. The HTTP GET method is one of nine standard HTTP (Hypertext Transfer Protocol) methods. HTTP GET requests should only receive data and do not change the server's state.
How to send a GET request to get JSON data?
The client indicates to the server a list of supported formats using the HTTP Accept header. For example, browsers send GET requests with the Accept header in the following format:
The server selects the appropriate content type and informs the browser of its choice using the Content-type response header.
GET request to get JSON data Example
To get JSON data, the client must explicitly indicate that it expects JSON data with the "Accept: application/json" header. If the Accept header is not present in the request, the server assumes that the client accepts all media types. Following is an example of getting JSON data from the ReqBin echo URL:
How to return a JSON response?
To return a JSON response, the client needs to include the JSON data in the body of the HTTP response message and specify the data type in the "Content-Type: application/json" header. Clients use the Content-Type response header to interpret the server response data correctly. Below is an example of a JSON response from the server: