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 developers ensure that special characters are displayed correctly in their templates?
- What potential pitfalls should be considered when allowing users to upload files to different directories in PHP?
- How can PHP be optimized to handle form submissions and data processing on the same page without the need for multiple files?