首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用WordPress缓存Instagram feed

使用WordPress缓存Instagram feed
EN

Stack Overflow用户
提问于 2020-02-28 19:28:37
回答 1查看 144关注 0票数 1

我的网站上有一个Instagram feed:

代码语言:javascript
运行
复制
$instagram_user_id = 'MY USER ID';
$instagram_access_token = 'MY ACCESS TOKEN';

<?php

function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

$result = fetchData("https://api.instagram.com/v1/users/$instagram_user_id/media/recent/?access_token=$instagram_access_token&count=9");
$result = json_decode($result); ?>

<?php foreach ($result->data as $insta) { 

echo '<a target="blank" href="'.$insta->link.'">';

$img = $insta->images->low_resolution->url;
?>

<div style="background-image: url('<?php echo $img; ?>'); background-size: cover; background-position: center center;"></div></a>

<?php } ?>

这很有效,但我最近发现,Instagram的请求限制在每小时200次。

我的问题是-我可以缓存这些结果吗?

我试过使用set_transient,但没有成功。

编辑:这是我尝试使用瞬态的方式:

代码语言:javascript
运行
复制
<?php

$cached_result = get_transient('transient');

//if transient is not set
if(empty($cached_result)) { 

function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

$result = fetchData("https://api.instagram.com/v1/users/$instagram_user_id/media/recent/?access_token=$instagram_access_token&count=9");
$result = json_decode($result);

//set the transient
set_transient('transient', $cached_result, 60*60*6);

}
//tell me if transient is set
if(!empty($cached_result)) {

echo 'full';

} 

?>

但似乎没有设置瞬态,它每次都会调用。

EN

回答 1

Stack Overflow用户

发布于 2020-03-01 21:32:07

ULook查看您正在使用的代码。

代码语言:javascript
运行
复制
set_transient('transient', $cached_result, 60*60*6);

首先,您必须将数据保存在变量cached_result中,然后设置它。在调用get_transient的第一行代码中,暂时键上没有值,因为没有设置该值。

谢谢!!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60450804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档