How to send http request header using curl in PHP

When we open a site by our browser, the browser will send a http request header to site server and the server received and then send a http response header to our browser.

The http request header is usually sent by browser, while we also can construct our http request header and send it to web server by php. Here we provide an example using curl.

To send a http request header, you should know:

1.What web server you want to send

You should be sure the url of site, and you can send a request header to it.

2.What http request header you want to send

A http request header is consisted of some parameters such as:Accept, Accept-Encoding, Accept-Language,Cache-Control, Connection, Cookie etc, you should know what request parameters you want to send and what are values of them.

3.Get response header or output

When you send a http request header to web server, it will give you a response header. How to get the response header you should also know, only that, you can know the http request header is sent sucessfully or not.

Sending a http request header example by curl.

function sendHttpRequest($url)
$urls=parse_url($url);

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return web server output
curl_setopt($ch, CURLOPT_HEADER, 1); // return http response header

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,30); 
curl_setopt($ch, CURLOPT_TIMEOUT,60); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 7.0; Windows NT '.rand(5,6).'.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30'.rand(300,800).'; .NET4.0C; .NET4.0E)'); 
curl_setopt($ch, CURLOPT_REFERER, "https://www.google.com"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: ".$urls['host']));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"));
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
$data = curl_exec($ch); // the web server output
curl_close($ch);
$data=trim($data);
return $data;
}

,