从Instagram的API类创建缓存文件 [英] Creating cache file from Instagram API Class

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

问题描述

我与 Cosenary Instagram的PHP API类工作的来去的Instagram照片尝试创建缓存文件以提高速度。下面是片段,但它并没有奏效。

 包括('conf.php');
需要'bigflannel-instafeed /数据/ instagram.class.php';
//初始化类
$ Instagram的=新的Instagram($的accessToken);
$ instagram-> setAccessToken($的accessToken);
$内容= $ instagram-> getUserMedia($用户ID,$设置['伯爵']);
$缓存='./instagram.json';
如果(file_exists($缓存)及&放大器; filemtime($缓存)>时间() - 60 * 60安培;&放大器;文件大小($缓存)小于10){
//如果缓存文件存在,它是新的1个多小时和文件大小大于10字节,使用它
$ instaData =的file_get_contents($缓存);
}其他{
$ instaData =的file_get_contents($内容);
的file_put_contents($缓存,$ instaData);
}

我检查的文件instagram.json只留下0字节大小的空白文件。
我使用code创建从这里的其他问题缓存Instagram的API请求缓存文件使用PHP?

- 编辑 -

如果我使用JSON网址替换$内容,它的工作原理。但是,如果使用$内容的API类的一部分,它并不奏效。

 包括('conf.php');
需要'bigflannel-instafeed /数据/ instagram.class.php';
//初始化类
$ Instagram的=新的Instagram($的accessToken);
$ instagram-> setAccessToken($的accessToken);
$内容= json_en code($ instagram-> getUserMedia($用户ID,$设置['伯爵']));
$缓存='./instagram.json';
如果(file_exists($缓存)及&放大器; filemtime($缓存)>时间() - 60 * 60安培;&放大器;文件大小($缓存)→10){
//如果缓存文件存在,它是较新的1个多小时,用它
$ jsonData = json_de code(的file_get_contents($缓存));
}其他{
$ jsonData = json_de$c$c((file_get_contents(\"https://api.instagram.com/v1/users/3/media/recent/?access_token=180213154.f59def8.f888fe332f7c47e98bd20a44866ef0be&count=34\")));
的file_put_contents($缓存,json_en code($ jsonData));
}


解决方案

已更新答案

 < PHP
需要'instagram.class.php';$缓存='./cache.json';如果(file_exists($缓存)及&放大器; filemtime($缓存)>时间() - 60 * 60){
    //如果缓存文件存在,它是较新的1个多小时,用它
    $响应= json_de code(的file_get_contents($缓存),TRUE); //德code作为一个JSON数组
}其他{    $ Instagram的=新的Instagram($的accessToken);
    $ instagram-> setAccessToken($的accessToken);
    $响应= $ instagram-> getUserMedia($用户ID,$设置['伯爵']);    如果($响应){
        的file_put_contents($缓存,json_en code($响应)); //保存JSON
    }}
回声< pre>中;
如果(is_array($响应)){
    的foreach($响应['数据']为$数据){
            的print_r($数据);
    }
}

I'm working with Cosenary Instagram PHP API Classes to fetch photos from instagram and try to create cache file to improve speed. Below is the snippet but it doesn't worked.

include('conf.php');
require 'bigflannel-instafeed/data/instagram.class.php';
// Initialize class
$instagram = new Instagram($accessToken);
$instagram->setAccessToken($accessToken);
$contents = $instagram->getUserMedia($userID,$settings['count']);
$cache = './instagram.json';
if(file_exists($cache) && filemtime($cache) > time() - 60*60 && filesize($cache) < 10){
// If a cache file exists, and it is newer than 1 hour and file size bigger than 10 bytes, use it
$instaData = file_get_contents($cache);
} else {
$instaData = file_get_contents($contents);
file_put_contents($cache,$instaData);
}

I checked file instagram.json just leave blank file with 0 byte size. I'm using code to create cache file from other question here Caching Instagram API requests using PHP?

-- Edit --

If i replace $contents with json url, it works. But if i use $contents as part of API Class, it doesnt worked.

include('conf.php');
require 'bigflannel-instafeed/data/instagram.class.php';
// Initialize class
$instagram = new Instagram($accessToken);
$instagram->setAccessToken($accessToken);
$contents = json_encode($instagram->getUserMedia($userID,$settings['count']));
$cache = './instagram.json';
if(file_exists($cache) && filemtime($cache) > time() - 60*60 && filesize($cache) > 10){
// 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("https://api.instagram.com/v1/users/3/media/recent/?access_token=180213154.f59def8.f888fe332f7c47e98bd20a44866ef0be&count=34")));
file_put_contents($cache,json_encode($jsonData));
}

解决方案

UPDATED ANSWER

<?php
require 'instagram.class.php';

$cache = './cache.json';

if(file_exists($cache) && filemtime($cache) > time() - 60*60){
    // If a cache file exists, and it is newer than 1 hour, use it
    $response = json_decode(file_get_contents($cache),true); //Decode as an json array
} else {

    $instagram = new Instagram($accessToken);
    $instagram->setAccessToken($accessToken);
    $response = $instagram->getUserMedia($userID, $settings['count']);

    if($response){              
        file_put_contents($cache,json_encode($response)); //Save as json
    }     

}
echo "<pre>";
if(is_array($response)){
    foreach($response['data'] as $data){
            print_r($data);
    }
}

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

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