Header only retrieval in php via curl
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.
Why use CURL for this? There is a PHP-function for that:
$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)
Should be easy to get the content-type after this.
You could also add the 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)
(1) Yes. A HEAD request (as you're issuing in this case) is far lighter on the server because it only returns the HTTP headers, as opposed to the headers and content like a standard GET request.
(2) You need to set the CURLOPT_RETURNTRANSFER option to true
before you call curl_exec()
to have the content returned, as opposed to printed:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
That should also make your class work correctly.