我的网站上有一个Instagram feed:
$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,但没有成功。
编辑:这是我尝试使用瞬态的方式:
<?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';
}
?>但似乎没有设置瞬态,它每次都会调用。
发布于 2020-03-01 21:32:07
ULook查看您正在使用的代码。
set_transient('transient', $cached_result, 60*60*6);首先,您必须将数据保存在变量cached_result中,然后设置它。在调用get_transient的第一行代码中,暂时键上没有值,因为没有设置该值。
谢谢!!
https://stackoverflow.com/questions/60450804
复制相似问题