Setting HTTP request headers with cURL can be done by using the CURLOPT_HTTPHEADER option, which can be set with the curl_setopt function. To add headers to your HTTP request you need to put them into a PHP Array, which you can then pass to the cul_setopt function, like demonstrated in the below example.
$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 as you would normally, using the cURL curl_exec function.
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
Delivering cookies in HTTP headers
In this cURL tutorial we first demonstrated how you can set the User-agent and Accept headers manually, but few seem to realize that cookies are also delivered in the request headers. So to also include cookies we will simply a value called "Cookie", this will contain all cookies for a given website. I.e.
$cookies = 'CookieName1=Value;CookieName2=Value'; $request_headers = 'Cookie: '. $cookies;
Note. Each cookie is separated by a semicolon, and the cookie names must be unique.
Using cURL header options
Some headers can also be set through cURL options, an example would be the User-agent and Cookie headers, which can be set by using the CURLOPT_USERAGENT and CURLOPT_COOKIE options respectively. I.e.
$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'; curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent); curl_setopt($ch, CURLOPT_COOKIE, $cookies);