Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header only retrieval in php via curl

Tags:

php

curl

header

Actually I have two questions.

(1) Is there any reduction in processing power or bandwidth used on remote server if I retrieve only headers as opposed to full page retrieval using php and curl?

(2) Since I think, and I might be wrong, that answer to first questions is YES, I am trying to get last modified date or If-Modified-Since header of remote file only in order to compare it with time-date of locally stored data, so I can, in case it has been changed, store it locally. However, my script seems unable to fetch that piece of info, I get NULL, when I run this:

class last_change {   public last_change;   function set_last_change() {   $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);   // $header = curl_exec($curl);   $this -> last_change = curl_getinfo($header);   curl_close($curl);  }   function get_last_change() {   return $this -> last_change['datetime']; // I have tested with Last-Modified & If-Modified-Since to no avail  }  } 

In case $header = curl_exec($curl) is uncomented, header data is displayed, even if I haven't requested it and is as follows:

HTTP/1.1 200 OK Date: Fri, 04 Sep 2009 12:15:51 GMT Server: Apache/2.2.8 (Linux/SUSE) Last-Modified: Thu, 03 Sep 2009 12:46:54 GMT ETag: "198054-118c-472abc735ab80" Accept-Ranges: bytes Content-Length: 4492 Content-Type: text/xml 

Based on that, 'Last-Modified' is returned.

So, what am I doing wrong?

like image 428
Krule Avatar asked Sep 04 '09 12:09

Krule


People also ask

How do I get the header info from cURL?

We can use curl -v or curl -verbose to display the request headers and response headers in the cURL command. The > lines are request headers .

How can I get response header in PHP?

There is a simple solution built into cURL if you are only interested in the response headers and not the content of the body. By setting the CURLOPT_HEADER and CURLOPT_NOBODY options to true, the result of curl_exec() will only contain the headers.

How do I print response headers in cURL?

By default, curl doesn't print the response headers. It only prints the response body. To print the response headers, too, use the -i command line argument.

What does PHP cURL return?

Functions of cURL in PHP curl_error — It will return the string which represents the error for the particular current session.


1 Answers

You are passing $header to curl_getinfo(). It should be $curl (the curl handle). You can get just the filetime by passing CURLINFO_FILETIME as the second parameter to curl_getinfo(). (Often the filetime is unavailable, in which case it will be reported as -1).

Your class seems to be wasteful, though, throwing away a lot of information that could be useful. Here's another way it might be done:

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://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'; } 

Yes, the load will be lighter on the server, since it's only returning only the HTTP header (responding, after all, to a HEAD request). How much lighter will vary greatly.

like image 87
GZipp Avatar answered Sep 20 '22 09:09

GZipp