LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » Get Headers And Content Of A URL Using Single CURL Request In PHP

Get Headers And Content Of A URL Using Single CURL Request In PHP

Get-Headers-And-Content-Of-A-URL-Using-Single-CURL-Request-In-PHP
A common question is that Is there any way to get both headers and body for a cURL request using PHP? or Can PHP cURL retrieve response headers AND body in a single request?

When an HTTP request has been received by a server, typically the response is sent back to the client. In the case of web resources, the response will usually consist of two parts, the Response Header, and the Response Body.

The header part of the response contains all the response headers, including cookies (if any) and information about the mime type of the content. The response body contains the content, which should match the mine-type in the headers. For example, common mime-types might be text/html for HTML pages, and text/css for external StyleSheets.

There is no build-in way to only return the response headers using cURL in PHP. However, we can still “cut” them from the full response. To do this, we first determine the size of the response header, and then simply cut it from the response using the strings function.

So Are you looking for a PHP script to get Headers and Content of a URL using a single cURL request in PHP? This tutorial will provide a code snippet that will help you to get Headers and Content of a URL from a given website.

Recommended For You:
Awesome And Stylish Smooth Sliding Menu On-Hover

There are many code snippets available online or on many other blogs and website but everyone is not able to optimize your blog or website so you need some optimized code snippet. So now checkout out code snippet for your blog and website that will give you all features for your desired code. Now grab the ready to use code and paste it where you want.

How To Get Headers And Content Of A URL Using Single CURL Request In PHP?

The below snipper with return two PHP variables. One is $webPageHeaders that is a multidimensional associative array and another is $webPageContent that is a plain string variable.

<?php
// Get Headers/Content Of A URL Using One CURL Request
$ch = curl_init();
$webPageHeaders = [];
$timeout = 5; //5 is seconds
curl_setopt($ch, CURLOPT_URL, $websiteURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_HEADER, 1);
// this function is called by curl for each header received
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$webPageHeaders)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) {// ignore invalid headers
return $len;
}
$webPageHeaders[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
}
);
// this function is called by curl for each header received
$beforeTime = microtime(true);
$beforeTime = (float) $beforeTime;
$webPageContent = curl_exec($ch);
$aftertime = microtime(true);
$aftertime = (float) $aftertime;
$allContentLoadTime = $aftertime - $beforeTime;
if(curl_exec($ch) === false) {
echo '<div class="alert alert-danger">ERROR: '.curl_error($ch).'. Please <a href="../contact">Contact Us</a> for support.</div>';
exit();
die();
} else {
$webPageContent = curl_exec($ch);
}
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//echo $httpcode;
//echo '<pre>';
//print_r($webPageHeaders);
//echo '</pre>';
curl_close($ch);
// Get Headers/Content Of A URL Using One CURL Request

A bonus $httpcode the variable is also available to check HTTP status of that URL and also $allContentLoadTime to get loading time of the WebPage.

Recommended For You:
PHP REST API Calling General Function With Header And Body Response

Final Words:

Be aware that the is placed well in your document. Rest all is in your hand if you want to customize it or play with it. That’s all we have. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allowed to us. Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

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