2012-08-17 27 views
0

我一直在使用各种不同的Twitter推文来抓取推文,但现在我已经通过限速和缓存推文撞墙了。这是我的代码:使用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); 

但是,这仅返回用户名和时间戳(这是错误的),当它应该返回Twitter的头像,鸣叫内容,正确的时间戳等。此外,它也是返回错误每隔几刷新:

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

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

+1

mkdir错误已经足够明显 - 你试图创建缓存目录,EVERYTIME这个函数运行。在尝试创建它之前,您应该检查目录是否存在。 – 2012-08-17 15:28:53

+0

在'mkdir()'之前加'@'来静音警告。或者在调用'mkdir()'之前使用'is_dir()'检查它是否存在。 – Florent 2012-08-17 15:33:02

+1

千万不要使用错误抑制。如果您收到警告,请修复它。 – 2012-08-17 15:34:18

回答

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

非常感谢!我添加了一些编辑来处理我包含的功能。 – 2012-08-17 16:50:11