php curl get response header – Retrieve response headers from PHP cURL

There are main 2 methods to get response headres from PHP cURL. Using CURLOPT_HEADER option and Using CURLOPT_HEADERFUNCTION option.

php curl get response header | curl get headers

There is no build-in way to only return the response headers using cURL in PHP. Script to demonstrate how to extract Header and Body from the Curl response in PHP.

1. Using CURLOPT_HEADER option

$url = "https://www.domain-name.com";
$my_curl = curl_init();
curl_setopt($my_curl, CURLOPT_URL, $url);
curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($my_curl, CURLOPT_HEADER, 1);

curl_setopt($my_curl, CURLOPT_NOBODY, 0);
$response = curl_exec($my_curl);

$header_size = curl_getinfo($my_curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
echo $header;
// echo $body;

don’t Miss : curl show headers

2. Using CURLOPT_HEADERFUNCTION option

$headers = [];
$url = "https://www.domain-name.com";
$my_curl = curl_init();
curl_setopt($my_curl, CURLOPT_URL, $url);
curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($my_curl, CURLOPT_HEADERFUNCTION,
    function ($curl, $header) use (&$headers) {
        $len = strlen($header);
        $header = explode(':', $header, 2);
        if (count($header) < 2) // ignore invalid headers
            return $len;

        $headers[strtolower(trim($header[0]))][] = trim($header[1]);

        return $len;
    }
);
$response = curl_exec($my_curl);
print_r($headers);

curl request php

$data = array(
            "member_number" => "1"
        );

        $headers = array(
            'Content-Type: application/json'
        );
        $url = "ip/api";
        $my_ch = curl_init($url);
        curl_setopt($my_ch, CURLOPT_POST, 1);
        curl_setopt($my_ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($my_ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($my_ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($my_ch);
        curl_close($my_ch);
        $jsonObject = json_decode($response);
        return $jsonObject;

how to display the responce of curl in php?

$response = get_web_page("http://domain-name.com/search?q=mobile+laptops&f=json&t=tamilnew&lang=er");
$outData = array();
$outData = json_decode($response);
echo "<pre>"; print_r($outData); echo "</pre>";

function get_web_page($url) {
    $options = array(
        CURLOPT_RETURNTRANSFER => true,   
        CURLOPT_HEADER         => false,  
        CURLOPT_FOLLOWLOCATION => true,   
        CURLOPT_MAXREDIRS      => 10,     
        CURLOPT_ENCODING       => "",     
        CURLOPT_USERAGENT      => "test", 
        CURLOPT_AUTOREFERER    => true,   
        CURLOPT_CONNECTTIMEOUT => 120,    
        CURLOPT_TIMEOUT        => 120,   
    ); 

    $my_curl = curl_init($url);
    curl_setopt_array($my_curl, $options);

    $content  = curl_exec($my_curl);

    curl_close($my_curl);

    return $content;
}

curl in php
php curl get response header

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://domain-name.com',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => [
        item1 => 'value',
        item2 => 'value2'
    ]
]);

$resp = curl_exec($curl);

curl_close($curl);

CURL PHP POST

<?php

$post = [
    'username' => 'pakainfo_v1',
    'password' => '[email protected]#$',
    'gender'   => 2,
];
$my_curl = curl_init();
curl_setopt($my_curl, CURLOPT_URL, 'http://www.domain-name.com');
curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($my_curl, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($my_curl);
var_export($response);

http post request php curl

$post = [
   'pakainfo' => $_POST['website']
];
httpPost('domain-name.com', $post);
// function
function httpPost($url, $data)
{
   	$my_curl = curl_init($url);
    curl_setopt($my_curl, CURLOPT_POST, true);
    curl_setopt($my_curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($my_curl);
    curl_close($my_curl);
    return $response;
}

php curl print status

$url = 'http://www.domain-name.com';
$my_curl = curl_init($url);
curl_setopt($my_curl, CURLOPT_HEADER, true);    // i want headers
curl_setopt($my_curl, CURLOPT_NOBODY, true);    // i don't need body
curl_setopt($my_curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($my_curl, CURLOPT_TIMEOUT,10);
$output = curl_exec($my_curl);
$httpcode = curl_getinfo($my_curl, CURLINFO_HTTP_CODE);
curl_close($my_curl);

echo 'HTTP code: ' . $httpcode;

I hope you get an idea about php curl get response header.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.