Daily Dilbert
Checking Modification Times
Rather than running the
script in CRON on a daily basis, we can build-in the ability for the
script to check if the image is outdated. This conserves on bandwidth
if your site only gets a visitor a few times a week, rather than
downloading the image regardless if someone actually needs to view it
on YOUR site.
Here's the code, once again 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($file, 8192);
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 <?php
// initially assume we have to update the local image
$update = 0;
// set local file system & website paths for the dilbert.gif image
$local_filename='/www/yoursite.com/images/dilbert.gif';
$website_filename='http://www.yoursite.com/images/dilbert.gif';
// check if a local dilbert.gif exists or needs to be updated
if (file_exists($local_filename)) {
$filedate = date ("Y-m-d", filemtime($local_filename));
$todaysdate = date("Y-m-d", time());
if($filedate != $todaysdate){
$update = 1;
}
}else{
$update = 1;
}
// If the Dilbert image needs to be updated
if($update == 1){
// 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($file, 8192);
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);
}
}
// Print the image and credit the source
echo "<table bgcolor='#FFFFFF' style='font-size:10px;font-family:Arial;' cellpadding=0 cellspacing=0><tr><td><img src='".$website_filename."' /></td></tr><tr><td align='right' style='padding-right:3px;padding-bottom:1px;'>Source: <a href='http://www.dilbert.com' style='color:#000000;font-size:10px;font-family:Arial;text-decoration:none;'>http://www.dilbert.com</a></td></tr></table>";
?>
Now all you need to do on any pages you want the image to appear is:
<?php include("/path.to/dilbertscript.php"); ?>
(replacing the path with your path, of course)
Demo
http://www.techdose.com/demos/Daily-Dilbert/
Final Thoughts
That's it! That should get you started in parsing images out of a web
page. For this particular instance all we needed was some Start &
End parameters, but you will find for more difficult parsing jobs (for
content, or where you can't narrow down the code you want to grab that
easily) that it is much more difficult and other methods to parse
through the page need to be applied. Now you have a daily comic on your
own website that doesn't require user interaction to get updated. W00t!
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? |
