Home  >  Article  >  Backend Development  >  How to Efficiently Retrieve Last Modified Date of a Remote File using cURL's Header-Only Retrieval?

How to Efficiently Retrieve Last Modified Date of a Remote File using cURL's Header-Only Retrieval?

Barbara Streisand
Barbara StreisandOriginal
2024-11-16 02:51:03662browse

How to Efficiently Retrieve Last Modified Date of a Remote File using cURL's Header-Only Retrieval?

Header-Only Retrieval in PHP via cURL

For efficient retrieval of file metadata, such as last modified date, consider using cURL's header-only retrieval feature. This method can significantly reduce processing power and bandwidth consumption on the remote server.

To retrieve only the headers, you can set the following options in your cURL request:

curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);

This tells cURL to send a HEAD request, which instructs the server to respond with only the HTTP header information, omitting the actual body of the response.

Last Modified Date Retrieval

To obtain the last modified date, you can use curl_getinfo() to retrieve the FILETIME information from the cURL handle. Here's an example:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://url/file.xml");
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_exec($curl);
$filetime = curl_getinfo($curl, CURLINFO_FILETIME); // Returns timestamp of last modification
curl_close($curl);

Sample Code

Here's a more complete example that retrieves and displays the last modified date of a remote file using cURL:

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'];
    }

    // Other functions can be added to retrieve other information.
}

$uri_info = new URIInfo('http://example.com/index.html');
$filetime = $uri_info->getFiletime();
if ($filetime != -1) {
    echo date('Y-m-d H:i:s', $filetime);
} else {
    echo 'Filetime not available';
}

The above is the detailed content of How to Efficiently Retrieve Last Modified Date of a Remote File using cURL's Header-Only Retrieval?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn