Need to cache external html and save the images locally

Hi guys,

Hope someone can point me in the right direction here! :slight_smile:

I’m calling a few external html files from other sites in my network to my main site, showing the latest photos from the photo site, video from the video site etc. I’m doing this with a curl function and then I’m caching that with the following code:

<?php
// get external data
function getData($url)
{
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

// cache code
$url = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];
$cachefile = substr_replace($file ,"",-4).'.htm';
$cachetime = 18000;

// serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
    echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\
";
    include($cachefile);
    exit;
}
ob_start(); // start the output buffer
?>
        <ul>
              <?php $latestPhotos = getData('http://externalsite.com/latest-gallery-list'); echo $latestPhotos; ?>
        </ul>
<?php

// cache the contents to a file
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
?>

That gives me a static html that looks something like this:

<ul>
<li><a href="http://www.externalsite.com/gallery-link-1" target="_blank">
<img src="http://www.externalsite.com/gallerythumbnail-1.jpg">
</a></li>

<li><a href="http://www.externalsite.com/gallery-link-2" target="_blank">
<img src="http://www.externalsite.com/gallerythumbnail-2.jpg">
</a></li>

<li><a href="http://www.externalsite.com/gallery-link-3" target="_blank">
<img src="http://www.externalsite.com/gallerythumbnail-3.jpg">
</a></li>
</ul>

Is it possible to modify this in someway so that the thumbnails get saved locally? Or some other better way to to this all around?

Thanks in advanced! :slight_smile: