Sending get JSON Request [PHP Code]

To get JSON from the server using PHP, you must send an HTTP GET request and pass the "Accept: application/json" HTTP header, which will tell the server that the client expects JSON in response. In this PHP GET JSON example, we send a GET request to the ReqBin echo URL with the "Accept: application/json" request header. Click Send to execute the PHP GET JSON example online and see the results. The PHP code was automatically generated for the GET Request Retrieve JSON example.
Sending get JSON Request [PHP Code] Send
GET /echo/get/json HTTP/1.1
Host: reqbin.com
Accept: application/json

Updated: Viewed: 38279 times
PHP code for GET Request Retrieve JSON example

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:

Accept Request-Header Syntax
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*

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:

GET Request to Retrieve a JSON Example
GET /echo/get/json HTTP/1.1
Host: reqbin.com
Accept: application/json

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:

JSON Response Example
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19

{"success":"true"}

See also

Generate code snippets for PHP and other programming languages

Convert your GET Request Retrieve JSON request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.