How can a HEAD request be implemented in cURL to retrieve only the header information of a webpage in PHP?
To retrieve only the header information of a webpage using cURL in PHP, you can use a HEAD request. This type of request fetches only the headers of the response without downloading the actual content of the page. By setting the CURLOPT_NOBODY option to true in the cURL request, you can achieve this.
$url = 'https://example.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if($response === false){
echo 'Error: ' . curl_error($ch);
} else {
echo $response;
}
curl_close($ch);
Keywords
Related Questions
- How can PHP be used to integrate a complete path from the browser's address bar back into a script for purposes like updating?
- What are best practices for structuring and organizing cURL requests in PHP scripts to avoid errors and improve readability?
- What are the advantages of using XPath over Simple html dom for accessing specific elements in PHP?