How To Make HTTP Requests in PHP - curl() Function

How To Make HTTP Requests in PHP - curl() Function

The cURL means client URL. It allows you to connect with other URLs and use their responses in your code. You can use Curl to make all kinds of HTTP requests, including sending custom headers, sending body data, and using different verbs to make your request. 

Using curl in PHP is very simple process:

  1. Initialize a curl session
  2. Set various options for the session
  3. Execute and fetch data from server or send data to server
  4. Close the session
// Initialize curl session
$ch = curl_init();

// Set options
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

// Execute
$result = curl_exec($ch);

// Close curl session

// Output result
echo $result;

The curl_setopt() function is used to set many different options on the Curl.

Set the CURLOPT_URL option with the $url variable passed into the function:

curl_setopt($ch, CURLOPT_URL, $url);

The CURLOPT_RETURNTRANSFER option is to true, which causes Curl to return the results of the HTTP request rather than output them. In most cases, this option should be used to capture the response rather than letting PHP echo it as it happens.


To make a POST request, set the CURLOPT_POST option to 1, and pass the data you want to send as an array to the CURLOPT_POSTFIELDS option. Also, set a Content-Type header, which indicates to the server what format the body data is in.

$data = array("name" => "Alex", "email" => "");
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

GET Requests

URLs used with GET can be bookmarked, they can be called as many times as needed, and the request should not affect change to the data it accesses. For example, a GET request is used when filling in a search web form, which should always use GET.

POST Requests

In contrast to GET requests, a POST request is one that does cause change on the server that handles the request. These requests shouldn’t be repeated or bookmarked, which is why your browser warns you when it is resubmitting data.

It is possible to see what kind of request was made to a PHP script acting as a server by inspecting the $_SERVER["REQUEST_METHOD"] value, which indicates which verb was used in the request.

Common HTTP Headers

Many of the headers you see in HTTP make sense in both requests and responses. Others might be specific to either a request or a response.

1. User-Agent

The User-Agent header gives information about the client making the HTTP request and usually includes information about the software client.

it is possible to inspect $_SERVER["HTTP_USER_AGENT"] to see what the User-Agent header was set to.

2. Content-Type

It is used to describe what format the data being delivered in the body of a request or a response is in. This allows the target to understand how to decode this content.

3. Accept

It allows the client to indicate what kind of content is acceptable, which is another way of allowing the client to specify what kind of content it actually knows how to handle.

Using the Accept and Content-Type headers together to describe what can be understood by the client, and what was actually sent, is called Content Negotiation.