Only header search in php via curl

Actually, I have two questions.

(1) Is there a reduction in processing power or bandwidth used on the remote server if I get only the headers and not a full page search using php and curl?

(2) As I think, and I may be wrong, this answer to the first questions is YES, I try to get the last modified date or If-Modified-Since header of the deleted file just to compare it with the -date time of the locally stored data. so I can, if it has been modified, save it locally. However, my script seems unable to get this piece of information, 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 } } 

If $header = curl_exec($curl) not executed, the header data is displayed, even if I did not request it and looks like this:

 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 this, "Last-Modified" is returned.

So what am I doing wrong?

+51
php curl header
Sep 04 '09 at 12:26
source share
6 answers

You pass $ header to curl_getinfo() . It should be $curl ( $curl pen). You can only get filetime by passing CURLINFO_FILETIME as the second parameter to curl_getinfo() . (Often filetime not available, in which case it will display as -1).

Your class seems to be wasteful, although it discards a lot of information that might be useful. Here you can do another way:

 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('Ymd H:i:s', $filetime); } else { echo 'filetime not available'; } 

Yes, loading will be easier on the server since it only returns an HTTP header (in the answer, after all, a HEAD request). How much easier will vary greatly.

+47
04 Sep '09 at 13:35
source share

Why use curl for this? There is a PHP function for this:

 $headers=get_headers("http://www.amazingjokes.com/img/2014/530c9613d29bd_CountvonCount.jpg"); print_r($headers); 

returns the following:

 Array ( [0] => HTTP/1.1 200 OK [1] => Date: Tue, 11 Mar 2014 22:44:38 GMT [2] => Server: Apache [3] => Last-Modified: Tue, 25 Feb 2014 14:08:40 GMT [4] => ETag: "54e35e8-8873-4f33ba00673f4" [5] => Accept-Ranges: bytes [6] => Content-Length: 34931 [7] => Connection: close [8] => Content-Type: image/jpeg ) 

After that, it will be easy for you to get the contents.

You can also add format = 1 to get_headers:

 $headers=get_headers("http://www.amazingjokes.com/img/2014/530c9613d29bd_CountvonCount.jpg",1); print_r($headers); 

This will return the following:

 Array ( [0] => HTTP/1.1 200 OK [Date] => Tue, 11 Mar 2014 22:44:38 GMT [Server] => Apache [Last-Modified] => Tue, 25 Feb 2014 14:08:40 GMT [ETag] => "54e35e8-8873-4f33ba00673f4" [Accept-Ranges] => bytes [Content-Length] => 34931 [Connection] => close [Content-Type] => image/jpeg ) 

Read more here (PHP.NET)

+28
Mar 11 '14 at 22:47
source share

(1) Yes. The HEAD request (as you issue in this case) is much easier on the server because it returns HTTP headers, not headers and content, like a standard GET request.

(2) Before calling curl_exec() you need to set the option CURLOPT_RETURNTRANSFER true to return the contents, and not print:

 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

This should also make your class work correctly.

+15
04 Sep '09 at 12:39
source share

You need to add

 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

to return the title instead of printing it. A.

Whether to return only headers is easier on the server depends on the script being executed, but usually it will.

I think you also want "filetime" instead of "datetime".

+4
04 Sep '09 at 12:40
source share

You can set the default thread context:

 stream_context_set_default( array( 'http' => array( 'method' => 'HEAD' ) ) ); 

Then use:

 $headers = get_headers($url,1); 

get_headers seems more efficient than cURL when get_headers skip steps such as trigger authentication procedures such as login requests or cookies.

+3
Aug 24 '15 at 16:27
source share

Here is my implementation using CURLOPT_HEADER and then parsing the output string to the map:

 function http_headers($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); $headers = curl_exec($ch); curl_close($ch); $data = []; $headers = explode(PHP_EOL, $headers); foreach ($headers as $row) { $parts = explode(':', $row); if (count($parts) === 2) { $data[trim($parts[0])] = trim($parts[1]); } } return $data; }; 

Sample Usage:

 $headers = http_headers('https://i.ytimg.com/vi_webp/g-dKXOlsf98/hqdefault.webp'); print_r($headers); Array ( ['Content-Type'] => 'image/webp' ['ETag'] => '1453807629' ['X-Content-Type-Options'] => 'nosniff' ['Server'] => 'sffe' ['Content-Length'] => 32958 ['X-XSS-Protection'] => '1; mode=block' ['Age'] => 11 ['Cache-Control'] => 'public, max-age=7200' ) 
+3
Feb 03 '16 at 12:02
source share



All Articles