How to get HTTP headers from CURL response?

Programming in Linux

You can write a simple PHP curl program to download a page. Every HTTP response contains a Header and the contents. In most cases, you will be concerned more about the contents. By default, curl response will give you the contents. But you can also specify that you need the header using the option CURLOPT_HEADER in your program with the curl_setopt function

curl_setopt($curlHandle, CURLOPT_HEADER, true);

Let’s see the complete program which is also give here.


function downloadURL($URL) {
 if(!function_exists('curl_init')) {
 die ("Curl PHP package not installedn");

 /*Initializing CURL*/
 $curlHandle = curl_init();

 /*The URL to be downloaded is set*/
 curl_setopt($curlHandle, CURLOPT_URL, $URL);
 /*Return the HTTP headers*/
 curl_setopt($curlHandle, CURLOPT_HEADER, true);
 /*Now execute the CURL, download the URL specified*/
 $response = curl_exec($curlHandle);
 return $response;

echo downloadURL("");

Let’s execute the program

$ php download.php | less

The output of the program will be something like this

HTTP/1.1 200…

View original post 64 more words