【发布时间】:2014-01-16 12:42:50
【问题描述】:
{
"code":420,
"error_type":"OAuthRateLimitException",
"error_message":"You have exceeded the maximum number of requests per hour. You have performed a total of 253 requests in the last hour. Our general maximum request limit is set at 30 requests per hour."
}
我刚刚注意到我正在关注的一个客户网站已停止显示 Instagram 提要,因此我将提要 URL 直接加载到浏览器中,但出现了上述错误。我不认为一个小时内应该有 253 个请求,但是在谷歌搜索这个问题时,我遇到有人说这是因为每个请求都登录了 API。可悲的是,我已经“继承”了这段代码,并且之前并没有真正使用过 Instagram API,除了之前修复了同一个网站的错误。
客户站点在 WordPress 中,因此我将代码封装在一个函数中以获取图像:
function get_instagram($user_id=USERID,$count=6,$width=190,$height=190){
$url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=ACCESSTOKEN&count='.$count;
// Also Perhaps you should cache the results as the instagram API is slow
$cache = './'.sha1($url).'.json';
if(file_exists($cache) && filemtime($cache) > time() - 60*60){
// If a cache file exists, and it is newer than 1 hour, use it
$jsonData = json_decode(file_get_contents($cache));
} else {
$jsonData = json_decode((file_get_contents($url)));
file_put_contents($cache,json_encode($jsonData));
}
$result = '<a style="background-image:url(/wp-content/themes/iwear/inc/img/instagram-background.jpg);" target="_BLANK" href="http://www.instagr.am" class="lc-box lcbox-4 instagram">'.PHP_EOL.'<ul>'.PHP_EOL;
foreach ($jsonData->data as $key=>$value) {
$result .= "\t".'<li><img src="'.$value->images->low_resolution->url.'" alt="'.$value->caption->text.'" data-width="'.$width.'" data-height="'.$height.'" /><div class="lc-box-inner"><div class="title"><h2>images</h2></div><div class="description">'.$value->caption->text.'</div></div></li>'.PHP_EOL;
}
$result .= '</ul></a>'.PHP_EOL;
return $result;
}
但正如我所说,这段代码已经停止工作。有什么办法可以优化它以实际工作吗?我还注意到(可能被盗的)instagram 内容中提到了缓存,但它实际上并不是缓存,所以这也可能是一个解决方案
谢谢
【问题讨论】:
-
您是否尝试过在服务器上缓存结果 1 小时?也许使用 memcache / memcached。此外,您是在使用任何具有内置缓存的框架,还是依靠 instagram api 来进行缓存?
-
大声笑我以为我写的,只是表明你可以复制和粘贴而无需了解它的作用。 stackoverflow.com/questions/10231369/… ,如果不是,则结果应缓存 1 小时然后检查文件权限并且
'./'.sha1($url).'.json';是可写的。您还应该考虑迁移到 v2 API -
@LozCherone 确实表明了这一点。一般来说,我反对复制粘贴,并且宁愿让自己的代码变得更好,但是在我被以前的开发人员抛弃的情况下,我希望尽可能不参与他们的解决方案。感谢您的建议