Share via:

PHP: HTTP Request Headers (cURL)

Setting custom HTTP Headers with cURL is useful when changing User Agent or Cookies. Headers can be changed two ways, both using the curl_setopt function.

19483 views

Edited: 2018-02-02 17:41

Setting cURL request headers in PHP.

Controlling the cURL headers is done using the CURLOPT_HTTPHEADER option. This can be useful if you want to set custom request headers when performing a HTTP request through cURL in PHP.

The CURLOPT_HTTPHEADER option is used with the curl_setopt function. For example, to add headers to a request, we simply place them in an Array, which can then be passed to the cul_setopt function.

To change the User-Agent and Accept headers, we can simply do like shown below:

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';

$request_headers = array();
$request_headers[] = 'User-Agent: '. $User_Agent;
$request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';

After having included all the request headers that you want, you need to pass on the Array to the curl_setopt function. Your request can then be carried out by calling curl_exec().

curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);

A complete version of the above can look like this:

// URL to fetch
$url = "https://beamtic.com/";

// Setting the HTTP Request Headers
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';

$request_headers = array();
$request_headers[] = 'User-Agent: '. $User_Agent;
$request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';


// Performing the HTTP request
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$response_body = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).

The $response_body variable contains the response body, you can try to output it directly to the browser with echo, just be sure to match the mime-type of the resource (I.e. text/html).

Delivering cookies in HTTP headers

When Cookies are used by a website, a client will include them in the headers of its requests. So, to make cookies available to a website when visiting the site with cURL, we add them to a "Cookie" field.

The cookie field contains all cookies for a site, so if there is more than one cookie, they should be separated by a semicolon (;).

$cookies = 'CookieName1=Value;CookieName2=Value';
$request_headers[] = 'Cookie: '. $cookies;

Note. Cookie Names must be unique.

Using cURL header options

Alternatively, some headers can also be set with dedicated options in the curl_setopt function. Both the User-agent and Cookie headers can be set using the CURLOPT_USERAGENT and CURLOPT_COOKIE options respectively. This may be easier than adding the each header field manually.

If we have both a cookie and a user-agent string stored in variables, as shown below:

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$cookies = 'CookieName1=Value;CookieName2=Value';

We can simply feed them directly via curl_setopt:

curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);

Compared to using an Array, this may be easier. However, which method you choose to use, is mostly down to your own personal preferences.

Comments