How to remove HTTP headers from CURL response?

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 only the contents and not the header using the option CURLOPT_HEADER in your program with the curl_setopt function

curl_setopt($curlHandle, CURLOPT_HEADER, false);

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);
 /*Don't return the headers*/
 curl_setopt($curlHandle, CURLOPT_HEADER, false);
 /*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

<!DOCTYPE html>
<html lang="en-US" dir="ltr">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <meta name="DC.description" content="" />
    <meta name="DC.subject" content="" />
    <meta name="DC.creator" content="Happy Cog Studios -" />

If you want to see the difference by enabling CURLOPT_HEADER to true.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s