Monday October 13, 2008

Daily Dilbert - Page 2

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 '<IMG SRC="/comics/dilbert/archive/images/';
$End '" ALT="Today\'s Comic" BORDER="0">';

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

$todaysDilbert $content[1];

echo 
$todaysDilbert;    // this will echo something like "dilbert2004034066713.gif"

// 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
   //  -notice the /comics/dilbert/archive/images/ since only the image name changes
   
fputs($fi,"GET /comics/dilbert/archive/images/".$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
   // Just switch the path /www/vhosts/mydomain.com/htdocs/images to wherever you want
   // the Dilbert image stored.
   
if($fo=fopen("/www/vhosts/mydomain.com/htdocs/images/dilbert.jpg","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?

<-- Previous Page Continue to Page 3 -->



Comments:
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?
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."
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:


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
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;
}
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.
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 = '';



If you have an account, please login below. You can sign up for a new account, it's free!
You can also post anonymously as Anonymous Techdoser if you leave the username/password fields blank.

*Username
*Password
Subject
Comment  
 


*Due to URL spamming, comments will be approved before being posted.




Partners

Comparison shopping:
GPS, Digital camera,
LCD TV, Nintendo Wii,
iPod, Tomtom,
Asus, Digital Photo Frame
www.shopbot.com.au

Sony LCD TV
Read Reviews On Sony LCD TV's
Price Comparison & Online Shopping
Read Reviews, Compare Prices & buy at CheckCost UK...
Compare Laptops
Find the best deals on our huge range of Laptops.
Reviews on Desktops
Read Reviews, Compare Prices & buy Desktop PC.
Buy LCD & TFT Monitors
Choose the reasonable LCD Monitors at CheckCost UK.
Compare Prices on LCD TV’s
Compare Prices & Read Reviews on LCD TV's.
Compare DVD Players
Find the best deals on DVD Players at CheckCost UK.
Compare MP3 Players
Read Reviews & Compare prices for the best deal of Mp3 player
Reviews on Digital Camera
Reviews, Compare prices on Digital Camera
Compare Camcorders
Find the best deals on our huge range of Camcorders
Latest Office Suites Software
Read reviews about Latest Office Suites software like office 2007 & more

Advertising Sponsors