Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2005, 11:11 PM   PM User | #1
Kurashu
Regular Coder

 
Join Date: Aug 2004
Location: The US of A
Posts: 767
Thanks: 1
Thanked 0 Times in 0 Posts
Kurashu is an unknown quantity at this point
Word of the Day

Here's a little snippet to get the Word of the Day from Dictionary.com.

PHP Code:
function wotd() {
        
$xml simplexml_load_string(file_get_contents('http://dictionary.reference.com/wordoftheday/wotd.rss'));
        
$wotd $xml->channel->item->description;
        
$link $xml->channel->item->link;
        
$wotd explode(':'$wotd);
        return array(
'wotd'=>$wotd[0], 'def'=>$wotd[1], 'link'=>$link);
        } 
It's a basic script, and if you have high traffic you might want to use something along this line...

PHP Code:
function get() {
        
$xml cacheCheck();
        if(
$xml === false) {
            return 
false;
            }
        
$xml simplexml_load_string($xml);
        
$wotd $xml->channel->item->description;
        
$link $xml->channel->item->link;
        
$wotd explode(':'$wotd);
        return array(
'wotd'=>$wotd[0], 'def'=>$wotd[1], 'link'=>$link);
        }

function 
cacheCheck($dir='cache/'$expire=3600) {
    
$good true;
    
$filename $dir.'wotd';
    if (
file_exists($filename)) {
        
$time filectime($filename);
        if(
$time > (time() - $expire)) {
            
$good false;
            }
        
$xml file_get_contents($filename);
        }

    if (
$good) {
        
$xml file_get_contents('http://dictionary.reference.com/wordoftheday/wotd.rss');
        if (!
$xml) {
            return 
false;
            }else{
                
file_put_contents($filename$xml);
                }
        return 
$xml;
        }
    } 
The cache is untested, but it should work.
Kurashu is offline   Reply With Quote
Old 12-08-2005, 03:35 PM   PM User | #2
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
You could set the expire time to 1 day as "the word of the day" will most probably change only once a day
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 12-08-2005, 09:05 PM   PM User | #3
Kurashu
Regular Coder

 
Join Date: Aug 2004
Location: The US of A
Posts: 767
Thanks: 1
Thanked 0 Times in 0 Posts
Kurashu is an unknown quantity at this point
I suppose so. But that'd be more difficult. Because if your first hit of the day is at, say 9 in the morning, but the next day you get the first hit at 8 in the morning. Same word, different day.

But I do see your point.
Kurashu is offline   Reply With Quote
Old 12-08-2005, 09:22 PM   PM User | #4
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
Well I thought about that. You could meke the cache valid untill some time. You save the timestamp of when the last time was and check if it's still valid. You could set that the cache is valid untill 0:00 of the next day and if someone opens the page after that you cache it and set the expiure date to 0:00 of the next day. You'd be forcing an update on midnight.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 12-08-2005, 10:24 PM   PM User | #5
Kurashu
Regular Coder

 
Join Date: Aug 2004
Location: The US of A
Posts: 767
Thanks: 1
Thanked 0 Times in 0 Posts
Kurashu is an unknown quantity at this point
Good point.

I did some editing, and a simple date check would be easy enough. I also changed the $xml check.

PHP Code:
function cacheCheck($dir='cache/') {
    
$get true
    
$filename $dir.date('Ymd');
    if (
file_exists($filename)) {
        
$get false
        
$xml file_get_contents($filename);         
        }
    if (
$get) { 
        
$xml file_get_contents('http://dictionary.reference.com/wordoftheday/wotd.rss'); 
        if (!
$xml) { 
            return 
false
        }else{ 
            
file_put_contents($filename$xml); 
            } 
        } 
    return 
$xml;         
    } 
This way you get a cache and a directory of WOTD files. Alternatively, you could store it in a database and do all the checks in there.

Last edited by Kurashu; 12-08-2005 at 10:35 PM..
Kurashu is offline   Reply With Quote
Old 12-09-2005, 01:28 AM   PM User | #6
Velox Letum
Senior Coder

 
Join Date: Apr 2005
Location: Colorado, United States
Posts: 1,208
Thanks: 0
Thanked 0 Times in 0 Posts
Velox Letum is an unknown quantity at this point
Just wanted to add my two cents in saying that this uses a function which is only available in PHP5 (file_put_contents()). Also, in PHP4, simplexml is by default disabled as opposed to PHP5 where it is enabled by default.
__________________
"$question = ( to() ) ? be() : ~be();"
Velox Letum is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:37 AM.


Advertisement
Log in to turn off these ads.