使用 PHP 发布缓存推文


Issue Caching Tweets with PHP

我一直在使用各种不同的推特提要来抓取推文,但现在我已经在速率限制和缓存推文方面遇到了障碍。这是我的代码:

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]:文件存在于/home/content/36/8614836/html/wp-content/themes/NCmainSite/functions.php 第 110 行

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

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

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