Cache instagram API request

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>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

For example 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.

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);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x