Friday April 19, 2024

Daily Dilbert

Save The Image Locally
The first step we're going to take is to save the file locally on our system. Don't worry, this won't consume an infinite amount of space, since the image we'll be writing will be named the same thing every time. Pretty cool, huh?

Here's the code, commented for your enjoyment:
<?php
 
// Define the URL we'll be pulling from
 
$URL "http://www.dilbert.com/";

 
// Grab the full contents of the web page, 8192 bytes at a time until done
 
$file fopen("$URL""r");
 
$r "";
 do {
    
$data fread($file8192);
    if (
strlen($data) == 0) {
        break;
    }
    
$r .= $data;
 } while (
true);

// Define a Start & End location for the regular expression
$Start 'src="/dyn/str_strip/';
$End '" border="0" />';

// Grab the text between the Start & End location (ie. The picture name)
$stuff eregi("$Start(.*)$End"$r$content);

$todaysDilbert str_replace("src=\"","",$content[0]);
$todaysDilbert str_replace("\" border=\"0\" />","",$todaysDilbert);

 
// Open a socket to www.dilbert.com so we can download data from port 80 (the web server)
 
if($fi=fsockopen("www.dilbert.com",80))
 {
    
// Tell it the image we want to download
    
fputs($fi,"GET ".$todaysDilbert." HTTP/1.0\r\n\r\n");

    
// Get rid of unneccessary HTTP headers the web server sends us
    
while(!preg_match("/^\r?\n$/",fgets($fi,1024))); //skip headers

    // Define & open a file on the local system for writing
    
if($fo=fopen($local_filename,"w")){
     
// Grab the dilbert picture 65535 bytes at a time until done
     
while(!feof($fi))
     {
         
// Write the image data to our local file
         
fwrite($fo,fread($fi,65535));
     }
     
// Close the local file
     
fclose($fo);
    }
    
// Close the connection with dilbert.com
    
fclose($fi);
 }
?>

We're almost done! Now, AS-IS the script is still hitting up dilbert.com like Jenna Jameson on prom night. The only difference is, we've saved the file locally and could just have this PHP script run in a CRON job once a day to update this image. But what if we want it to be more (you guessed it) dynamic? How about we only download the new Dilbert image if someone accesses the page we're displaying it on?

Let's only update the local Dilbert image if it's out of date..

page1 page2 page3


Comments:
New Regex.
Posted 06/01/08 10:14AM by Anonymous Techdoser
The Regular expression had to be changed to reflect the new page with flash.

// Define a Start & End location for the regular expression
$Start = '';
Good one
Posted 10/24/07 7:11PM by Anonymous Techdoser
Good work, I was actually looking for something like this. in fact, didn't wanna spend time on that. thank you so much.
re: "An excellent article" and "Oddly written" com
Posted 11/14/05 4:43PM by Anonymous Techdoser
It looks correct. Just watch the curly braces.
if (file_exists($filename))
{
$filedate = date ("Y-m-d", filemtime($filename));
$todaysdate = date("Y-m-d", time());
if($filedate != $todaysdate)
{
$update = 1;
}
}
else
{
$update = 1;
}
See code at http://www.bestof417.com/calvin.php
Posted 08/14/05 11:01PM by wolfdogg
View source of my code at: http://www.bestof417.com/calvin.php
Thx!
Jeff
Trying to implement this on another cartoon site f
Posted 08/14/05 11:00PM by wolfdogg
I'm getting a blank image to pull when trying to do this on Calvin and Hobbes site. Can anyone figure out what might be wrong? The concept seems simple enough, but it won't pull.
My code:


Oddly written
Posted 03/30/05 5:53PM by AceBHound
I think that part of the code is just written oddly. It's actually saying "If the image exists, check the date & if the time of the file is old, grab the image again. If the image does not exist, grab the image."
An excellent article
Posted 03/29/05 10:56PM by Anonymous Techdoser
Possible error in the code on the last page - at the top, shouldn't the "else{$update = 1;" be anything but 1, since that is the setting to update?