Header-Only Retrieval in PHP via Curl
Reducing Server Load with Header-Only Retrieval
When retrieving web content using PHP and curl, it is possible to specify whether to fetch only the header or the entire page. Selecting the header-only option reduces the processing power and bandwidth required on the remote server, as it excludes the need to generate and transmit the page body.
Fetching Last Modified Date via curl_getinfo
To retrieve the last modified date or If-Modified-Since header of a remote file, you can use curl_getinfo(). Pass the curl handle (not the header data) as the first argument, and specify CURLINFO_FILETIME as the second argument. However, it's important to note that the filetime may not always be available, in which case it will be reported as -1.
Example: Retrieving the Last Modified Date
<?php class URIInfo { public $info; public $header; private $url; public function __construct($url) { $this->url = $url; $this->setData(); } public function setData() { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $this->url); curl_setopt($curl, CURLOPT_FILETIME, true); curl_setopt($curl, CURLOPT_NOBODY, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); $this->header = curl_exec($curl); $this->info = curl_getinfo($curl); curl_close($curl); } public function getFiletime() { return $this->info['filetime']; } } $uri_info = new URIInfo('http://www.codinghorror.com/blog/'); $filetime = $uri_info->getFiletime(); if ($filetime != -1) { echo date('Y-m-d H:i:s', $filetime); } else { echo 'filetime not available'; }
?>
Additional Considerations
The URIInfo class can be extended with methods to retrieve other header information, such as the content type or the ETag.
The above is the detailed content of How Can I Retrieve the Last Modified Date of a Remote File Using curl in PHP?. For more information, please follow other related articles on the PHP Chinese website!