Posted inphp

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. You can use the curl_getinfo() function in PHP to retrieve the response headers of a cURL request.

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 "
"; print_r($outData); echo "

";

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

 'pakainfo_v1',
    'password' => 'Pakai895645d@#$',
    '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;

Here's an example code snippet that demonstrates how to use curl_getinfo() to retrieve the response headers:

// Initialize a cURL session
$ch = curl_init();

// Set the URL and other options
curl_setopt($ch, CURLOPT_URL, 'http://example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the cURL request
$response = curl_exec($ch);

// Get the response headers
$headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);

// Close the cURL session
curl_close($ch);

// Output the response headers
echo $headers;

In this example, we first initialize a cURL session using the curl_init() function. We then set the URL we want to request and enable the CURLOPT_RETURNTRANSFER option to instruct cURL to return the response as a string instead of outputting it directly.

After executing the cURL request with curl_exec(), we call curl_getinfo() to retrieve the response headers using the CURLINFO_HEADER_OUT constant. This constant tells cURL to include the response headers in the information it returns.

Finally, we close the cURL session with curl_close() and output the response headers with echo.

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype