我一直在使用各种不同的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
mkdir错误已经足够明显 - 你试图创建缓存目录,EVERYTIME这个函数运行。在尝试创建它之前,您应该检查目录是否存在。 – 2012-08-17 15:28:53
在'mkdir()'之前加'@'来静音警告。或者在调用'mkdir()'之前使用'is_dir()'检查它是否存在。 – Florent 2012-08-17 15:33:02
千万不要使用错误抑制。如果您收到警告,请修复它。 – 2012-08-17 15:34:18