Sending GET Request using PHP

To make an HTTP GET request with PHP, you can use the PHP Curl library or the built-in PHP streaming functions. The Curl-based method is preferred when you need to send additional HTTP headers to the server with your GET request, limit download speed, or diagnose request errors, while PHP's built-in streaming functions are less verbose and easier to use. In this PHP GET Request Example, we send a GET request using the PHP-Curl library. The Curl-less method is provided below with detailed description. Click Execute to run the PHP GET Request Example online and see the result.
Sending GET Request using PHP Execute
<?php
$url = "https://reqbin.com/echo/get/json";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$resp = curl_exec($curl);
curl_close($curl);

echo $resp;
?>

Updated: Viewed: 11676 times

What is PHP?

PHP is an open-source server-side scripting language used for web development. PHP is a widely used general-purpose language that can be embedded in HTML code. PHP can support multiple databases, including MySQL, PostgreSQL, and SQLite, and is compatible with various operating systems like Windows, Linux, and macOS.

What is HTTP GET?

The GET method requests a resource from the server using the provided URL. The GET method is one of the nine standard HTTP (Hypertext Transfer Protocol) methods. The HTTP GET method should only be used to retrieve data from the server. HTTP GET requests cannot send data to the server in the body of a GET message and cannot change the server's state. If you need to change data on the server, use the POST, PUT, PATCH, or DELETE methods.

What is PHP Curl Library?

The PHP Curl Library offers a easy and convenient way to make HTTP requests in PHP. The PHP Curl Library offers a range of methods for sending requests and interacting with servers. It supports HTTPS certificates, multiple HTTP methods, FTP file upload, proxy, cookies, user authentication, and HTML form-based upload. To use PHP Curl features, you need to install libcurl 7.10.5 or later and compile PHP with Curl support. The behavior of the PHP Curl functions is affected by the curl.cainfo setting in the php.ini file.

How to send GET request with PHP Curl Library?

To send GET requests using the Curl library, follow these steps: initialize a Curl session with curl_init(), set parameters with curl_setopt() (including the target URL and returning the string if necessary), execute the Curl session with curl_exec(), close the session with curl_close(), and output the return string.

PHP GET Curl Example
<?php
$url = "https://reqbin.com/echo/get/json";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$resp = curl_exec($curl);
curl_close($curl);

echo $resp;
?>

#output: {"success":"true"}

How to send PHP GET Request with Additional Headers?

The following an example of sending a GET with additional headers in PHP:

PHP GET request with PHP streaming functions
<?php
$url = 'https://reqbin.com/echo/get/json'; 

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);

$headers = array(
  'Authorization: BT',
  'Accept: application/json'
);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);

echo $response;
?>

#output: {"success":"true"}

How to send PHP GET Request with Custom Headers?

To send a GET request with custom HTTP headers, you need to pass those headers to the "headers" parameter:

PHP GET request with Additional Headers
<?php
$url = 'https://reqbin.com/echo/get/json'; 

$headers = array(
  'Authorization: Bearer TOKEN',
  'Accept: application/json'
);

$context = stream_context_create([
  'http' => [
    'header' => $headers
  ]
]);

$response = file_get_contents($url, false, $context);

echo $response;
?>

#output: {"success":"true"}

How to send a GET request using PHP streaming functions?

The PHP-Curl library is very powerful, but requires additional code to initialize and execute the request. If your GET request does not require additional functionality provided by PHP-Curl library, such as diagnosing request errors, viewing server response headers, or limiting the download speed, then you can use the PHP streaming function instead of the PHP-Curl library, which is easier to use.

PHP GET request with PHP streaming functions
<?php
$url = "https://reqbin.com/echo/get/json";

$json = file_get_contents($url);

echo $json;
?>

#output: {"success":"true"}

See also