缓存instagram API请求 [英] Cache instagram API request

查看:52
本文介绍了缓存instagram API请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现instagram中的一些照片,以便在我的页脚中用作laravel项目中的装饰.但是,当我刷新页面时,它会一遍又一遍地进行api调用.我当时正在考虑在会话中缓存或存储该调用,因此我不必总是发送API请求...那么如何缓存此api请求?

I am implementing some photos from instagram to use in my footer as decoration in my laravel project. But when I refresh the page it does the api call over and over again. I was thinking of caching or storing the call in a session so I don't always have to send an API request... So how can I cache this api request?

PHP:

<?php
$clientid = "XXXXXXXXXXXXXXXXXXXXXXXX";
$url = "https://api.instagram.com/v1/tags/kittens/media/recent?client_id=".$clientid;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

$results = json_decode(curl_exec($curl));
?>

HTML:

<footer>
<?php
    echo "<ul class='inline-list'>";    
    foreach ($results->data as $result) {
        echo "<li><img src='".$result->images->thumbnail->url."'></li>";
    }
    echo "</ul>";
?>
</footer>

推荐答案

要缓存请求,可以将其写入文件并检查修改日期或使用某种缓存库.

To cache request you can write it into a file and check modification date or use some kind of caching library.

例如 http://www.phpfastcache.com/

// In your config file
include("phpfastcache/phpfastcache.php");
phpFastCache::setup("storage","auto");

// phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "sqlite" and "xcache"
// You don't need to change your code when you change your caching system. Or simple keep it auto
$cache = phpFastCache();

// In your Class, Functions, PHP Pages
// try to get from Cache first. product_page = YOUR Identity Keyword
$results = $cache->get("cache_instagram");

if($results == null) {
    $clientid = "XXXXXXXXXXXXXXXXXXXXXXXX";
    $url = "https://api.instagram.com/v1/tags/kittens/media/recent?client_id=".$clientid;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

    $results = json_decode(curl_exec($curl));
    // set products in to cache in 600 seconds = 10 minutes
    $cache->set("cache_instagram", $results,600);
}

// Output Your Contents $products HERE
// Re-written first example from page for purpouse of your script.

对于Laravel内置缓存

For Laravel built-in caching

// http://laravel.com/docs/4.2/cache

if (Cache::has('cache_instagram')) {
    $results = Cache::get('cache_instagram');

} else {
    $clientid = "XXXXXXXXXXXXXXXXXXXXXXXX";
    $url = "https://api.instagram.com/v1/tags/kittens/media/recent?client_id=".$clientid;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

    $results = curl_exec($curl);

    Cache::put('cache_instagram', $results, 60); // 1 hour

}

$results = json_decode($results);

这篇关于缓存instagram API请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆