使用PHP发布缓存推文

时间:2012-08-17 15:26:30

标签: php json caching twitter

我一直在使用各种不同的推文来获取推文,但现在我已经打了一个限速和缓存推文的墙。这是我的代码:

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.''));

这很有效,直到达到速率限制。这是我添加到缓存推文的内容:

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);

然而,这只会返回用户名和时间戳(这是错误的),它应该返回推特头像,推文内容,正确的时间戳等。此外,每隔几次刷新它也会返回一个错误:

  

警告:mkdir()[function.mkdir]:文件存在于第110行的/home/content/36/8614836/html/wp-content/themes/NCmainSite/functions.php中

任何帮助将不胜感激 如果您需要更多信息,请参阅此函数的其余部分:http://snippi.com/s/9f066q0

1 个答案:

答案 0 :(得分:1)

在这里试试这个我已经修复了你的问题,而且你有一个流氓帖子选择卷曲。

<?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);
?>
相关问题