Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Word of the Day

  1. #1
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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();"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •