Created
March 8, 2012 09:49
-
-
Save GDmac/2000022 to your computer and use it in GitHub Desktop.
CodeIgniter Curl Head first
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
* This fetches the header only for a remote page. | |
* | |
* Pro's: | |
* - when fetching static stuff (images, css, html) | |
* you can first compare modified date or statuscode (304) | |
* | |
* Con's: | |
* - hitting a heavy webapp with dynamic content might be slow, | |
* you essentially hit the remote page two times | |
* | |
* http://stackoverflow.com/questions/1378915/header-only-retreival-in-php-via-curl | |
* | |
*/ | |
function fetch_page($remote_url, $header_only = FALSE) | |
{ | |
$this->curl->create($remote_url); | |
$this->curl->option(CURLOPT_FILETIME, TRUE); | |
if ($header_only) $this->curl->option(CURLOPT_NOBODY, TRUE); | |
return $this->curl->execute(); | |
} | |
$this->fetch_page($remote_url, TRUE); // only fetch header | |
$modified_date = $this->curl->info['filetime']; | |
if ($modified_date == -1) | |
{ | |
$modified_date = time(); | |
} | |
$modified_date = gmdate('Y-m-d H:i:s', $modified_date); | |
// compare the modified date to what we have | |
if ($item->modified != $modified_date) | |
{ | |
$item->content = $this->fetch_page($remote_url); // get full page | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment