"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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?

Posted on 2025-03-25
Browse:929

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';
}
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3