首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP发出缓存Tweets

使用PHP发出缓存Tweets
EN

Stack Overflow用户
提问于 2012-08-17 15:26:29
回答 1查看 510关注 0票数 0

我一直在使用各种不同的twitter提要来获取tweet,但现在我遇到了限制速率和缓存tweet的难题。这是我的密码:

代码语言:javascript
复制
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.''));

在我达到利率上限之前,这是很好的工作。下面是我在缓存tweet中添加的内容:

代码语言:javascript
复制
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);

然而,这只返回用户名和时间戳(这是错误的),当它应该返回twitter化身、推特内容、正确的时间戳等。此外,它还会每隔几次刷新一次错误:

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

任何帮助都将不胜感激。

如果您需要更多信息,下面是函数的其余部分:http://snippi.com/s/9f066q0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-17 15:49:41

在这里尝试这个我已经解决了你的问题,加上你有一个流氓的帖子,选择在卷曲。

代码语言:javascript
复制
<?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);
?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12008675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档