How can I Retrieve Only Headers in PHP using cURL?

Patricia Arquette
Release: 2024-11-10 12:02:02
Original
657 people have browsed it

How can I Retrieve Only Headers in PHP using cURL?

Header-Only Retrieval via cURL in PHP

This article addresses two inquiries regarding the use of cURL in PHP for retrieving only headers instead of full page data, focusing on processing efficiency and specific implementation challenges.

Processing Efficiency

Yes, there is a reduction in both processing power and bandwidth consumption on the remote server when retrieving only headers compared to fetching the entire page. This is because the server can avoid the overhead of generating and parsing the HTML content, resulting in a lighter load.

Retrieving File Modification Date

To retrieve the last modified date (Last-Modified header) or the If-Modified-Since header of a remote file, the following steps can be taken:

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

$filetime = (new URIInfo('http://www.example.com/file.xml'))->getFiletime();
if ($filetime != -1) {
    echo date('Y-m-d H:i:s', $filetime);
} else {
    echo 'filetime not available';
}
?>
Copy after login

This approach utilizes a class to encapsulate the functionality for fetching header information and extracting the Last-Modified header.

Implementation Challenges

The original code you provided attempted to obtain the last modification date using curl_getinfo($header), which is incorrect. curl_getinfo() should be used with the cURL handle ($curl) to retrieve header information. Furthermore, the use of CURLOPT_FILETIME should be sufficient for retrieving the last modified date.

In conclusion, header-only retrieval using cURL in PHP can potentially reduce server load while allowing you to access specific header information. By following the guidelines outlined above, you can effectively implement this feature in your code.

The above is the detailed content of How can I Retrieve Only Headers in PHP using cURL?. 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