Skip to content

Instantly share code, notes, and snippets.

@GDmac
Created March 8, 2012 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GDmac/2000022 to your computer and use it in GitHub Desktop.
Save GDmac/2000022 to your computer and use it in GitHub Desktop.
CodeIgniter Curl Head first
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* 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