Home > Backend Development > PHP Tutorial > How Can I Retrieve the Last Modified Date of a Remote File Using curl in PHP?

How Can I Retrieve the Last Modified Date of a Remote File Using curl in PHP?

Mary-Kate Olsen
Release: 2024-11-13 01:48:02
Original
276 people have browsed it

How Can I Retrieve the Last Modified Date of a Remote File Using curl in PHP?

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';
}
Copy after login

?>

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template