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.
Can PHP cURL retrieve response headers AND body in a single request?
One solution to this was posted in the PHP documentation comments: http://www.php.net/manual/en/function.curl-exec.php#80442
Code example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// ...
$response = curl_exec($ch);
// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
Warning: As noted in the comments below, this may not be reliable when used with proxy servers or when handling certain types of redirects. @Geoffrey's answer may handle these more reliably.
cURL request to a page to get headers without waiting for page contents to load
When you use cURL to access the headers of a page, the whole PHP file will be executed, even if there is long-running tasks inside. That's because HTTP headers may be overrided by the header function.
If you don't want to hang up, my suggestion is to use a command-line instead of a function to copy your file : instead of copy($source, $target)
, run the following if you're on a Linux system :
$source = escapeshellarg($source);
$target = escapeshellarg($target);
exec("cp $source $target &");
The &
symbol will execute the command in background (so if the copy takes 3 secondes, it will be run in background and not hang your PHP file).
PHP Using cURL and GET request with a header
According to PHP documentation for CURLOPT_HEADER
:
TRUE to include the header in the output.
Your $response
will probably look like this:
HTTP/1.1 200 OK
Some: headers
More: header lines
{
"real": "json content"
}
This is because you added the CURLOPT_HEADER
option.
You don't need to set any options to let the curl request send your headers. As long as you set the CURLOPT_HTTPHEADER
option, the headers will be sent.
If you really want to receive the response headers too, check existing questions like "Can PHP cURL retrieve response headers AND body in a single request?"
Get Header from PHP cURL response
It converts all headers into an array
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//enable headers
curl_setopt($ch, CURLOPT_HEADER, 1);
//get only headers
curl_setopt($ch, CURLOPT_NOBODY, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$headers = [];
$output = rtrim($output);
$data = explode("\n",$output);
$headers['status'] = $data[0];
array_shift($data);
foreach($data as $part){
//some headers will contain ":" character (Location for example), and the part after ":" will be lost, Thanks to @Emanuele
$middle = explode(":",$part,2);
//Supress warning message if $middle[1] does not exist, Thanks to @crayons
if ( !isset($middle[1]) ) { $middle[1] = null; }
$headers[trim($middle[0])] = trim($middle[1]);
}
// Print all headers as array
echo "<pre>";
print_r($headers);
echo "</pre>";
Retrieve the response code from header cURL php
I think you need to pass $curl
to the curl_getinfo
method, not the $response
$response = curl_exec($curl);
$theInfo = curl_getinfo($curl);
$http_code = $theInfo['http_code'];
You can see the doco here.. https://www.php.net/manual/en/function.curl-getinfo.php
Get Headers as part of CURL
The reason this was happening is because I was discarding the input of curl_exec. I changed that section of the code to:
$headers=curl_exec($info);
cURL - How to fetch page only if it has changed since last fetch?
I looked for answer for more than 2 days, and nobody could give me universal answer.
So I implemented etag and if-modified-since headers (as Matt Raines and sowa posts here), also to lower traffic I used compression like gzip.
Also there is request header Range, so that i could grap only part of the page as someone told me, but i think it is used only for files not web pages.
Thank you all for your time
How to get the headers from the last redirect with PHP's curl functions?
Search the output for "HTTP/1.1 200 OK" in the beginning of the line - this is where your last request will begin. All others will give other HTTP return codes.
Related Topics
How to Write a File in Utf-8 Format
Redefine Built in PHP Functions
Transform Relative Path into Absolute Url Using PHP
Convert Number to Month Name in PHP
Convert Number of Minutes into Hours & Minutes Using PHP
How to Add a PHP Page to Wordpress
PHP Curly Brace Syntax For Member Variable
Fetching Data from MySQL Database to HTML Dropdown List
Best Practice on PHP Singleton Classes
How to Upload & Save Files With Desired Name
How to Prepend File to Beginning
Send Json Data from JavaScript to PHP
Add 'X' Number of Hours to Date
Create or Write/Append in Text File
PHP Variable in HTML No Other Way Than: ≪PHP Echo $Var; ≫