Luke Keller
Luke Keller

Reputation: 2556

Issue Caching Tweets with PHP

I've been using various pieces of different twitter feeds to grab tweets, but now I've hit a wall with the rate limiting and caching tweets. Here's my code:

function tweets($twitter_handle, $tweet_limit, $tweet_links, $tweet_tags, $tweet_avatar,   $tweet_profile) {

    /* Store Tweets in a JSON object */
    $tweet_feed =   json_decode(file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name='.
                    $twitter_handle.'&include_entities=true&include_rts=true&count='.$hard_max.''));

This works great until I hit the rate limit. Here's what I added to cache tweets:

function tweets($twitter_handle, $tweet_limit, $tweet_links, $tweet_tags, $tweet_avatar, $tweet_profile) {

$url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name='.$twitter_handle.'&include_entities=true&include_rts=true&count='.$hard_max.'';
$cache = dirname(__FILE__) . '/cache/twitter';

if(filemtime($cache) < (time() - 60))
{
    mkdir(dirname(__FILE__) . '/cache', 0777);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
    $data = curl_exec($ch);
    curl_close($ch);
    $cachefile = fopen($cache, 'wb');
    fwrite($cachefile, $data);
    fclose($cachefile);
}
else
{
    $data = file_get_contents($cache);
}

$tweet_feed = json_decode($data);

This however only returns the username and timestamp (which is wrong), when it should be returning the twitter avatar, tweet content, correct timestamp, etc. Additionally, it's also returning an error every few refreshes:

Warning: mkdir() [function.mkdir]: File exists in /home/content/36/8614836/html/wp-content/themes/NCmainSite/functions.php on line 110

Any help would be appreciated.
If you need more info, here's the rest of the function: http://snippi.com/s/9f066q0

Upvotes: 0

Views: 515

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46620

Here try this ive fixed your issues, plus you had a rogue post opt in curl.

<?php 
function tweets($twitter_handle, $tweet_limit, $tweet_links, $tweet_tags, $tweet_avatar, $tweet_profile) {

    $http_query = array('screen_name'=>$twitter_handle,
                        'include_entities'=>'true',
                        'include_rts'=>'true',
                        'count'=>(isset($hard_max))?$hard_max:'5');

    $url = 'http://api.twitter.com/1/statuses/user_timeline.json?'.http_build_query($http_query);

    $cache_folder = dirname(__FILE__) . '/cache';
    $cache_file = $cache_folder . '/twitter.json';

    //Check folder exists
    if(!file_exists($cache_folder)){mkdir($cache_folder, 0777);}

    //Do if cache files not found or older then 60 seconds (tho 60 is not enough)
    if(!file_exists($cache_file) || filemtime($cache_file) < (time() - 60)){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
        $data = curl_exec($ch);
        curl_close($ch);
        file_put_contents($cache_file,$data);
    }else{
        $data = file_get_contents($cache_file);
    }

    return json_decode($data);
}

$twitter = tweets('RemotiaSoftware', 'tweet_limit','tweet_links', 'tweet_tags', 'tweet_avatar', 'tweet_profile');
print_r($twitter);
?>

Upvotes: 1

Related Questions