Twitter API达到150限制,每10分钟执行一次PHP cron

时间:2012-04-17 23:57:47

标签: php json twitter

我为网页做了一个简单的推特供稿。我使用API​​的JSON响应保存了一个缓存文件,然后使用jQuery读取它。

效果很好,问题是它随机达到了150个请求的REST API限制,而我每小时只做6个(每10分钟1个),据我记得,我没有我的托管(MediaTemple gs)中的任何其他Feed可能每小时都在做很多请求。

我知道我可以使用帐户授权并获得350个请求限制,我还没有测试过,但我认为这根本不能解决问题。

这是我每10分钟执行一次的cron:

<?php  
//Set local timezone
putenv("TZ=America/Caracas");

//Function to get contents using cURL
    function url_get_contents ($Url) {
        if (!function_exists('curl_init')){ 
                die('CURL is not installed!');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $Url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
}
//The path of the file that contains cached tweets
$cache = '/home/xxxxx/domains/example.com/html/demos/webname/twitter-json.txt';  

//Call the api and get JSON response
$data = url_get_contents("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=webname&count=100");
$parsed = json_decode($data, true);
//This is a workaround I made to exit the script if there's an error(it works)
if ($parsed['error']) exit;
//I change the twitter date format to d/m/y H:i
foreach ($parsed as $key => $value) {
$parsed[$key]['created_at'] = date('d/m/y H:i',strtotime($value['created_at']));
}
//I encode to JSON
$data = json_encode($parsed);
//Save the file
$cachefile = fopen($cache, 'wb');  
fwrite($cachefile,$data);  
fclose($cachefile);
?> 

0 个答案:

没有答案