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

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

问题描述

我正在使用 Cosenary Instagram PHP API 类 从 instagram 中获取照片并尝试创建缓存文件以提高速度.下面是代码片段,但它不起作用.

include('conf.php');需要bigflannel-instafeed/data/instagram.class.php";//初始化类$instagram = 新 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){//如果存在缓存文件,并且它比 1 小时更新且文件大小大于 10 字节,则使用它$instaData = file_get_contents($cache);} 别的 {$instaData = file_get_contents($contents);file_put_contents($cache,$instaData);}

我检查了 instagram.json 文件,只留下 0 字节大小的空白文件.我正在使用代码从此处的其他问题创建缓存文件 使用 PHP 缓存 Instagram API 请求?

-- 编辑 --

如果我用 json url 替换 $contents,它会起作用.但是如果我使用 $contents 作为 API Class 的一部分,它就不起作用了.

include('conf.php');需要bigflannel-instafeed/data/instagram.class.php";//初始化类$instagram = 新 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){//如果缓存文件存在,并且超过 1 小时,则使用它$jsonData = json_decode(file_get_contents($cache));} 别的 {$jsonData = json_decode((file_get_contents("https://api.instagram.com/v1/users/3/media/recent/?access_token=180213154.f59def8.f888fe332f7c47e98bd20a44866ef0be&count)=;file_put_contents($cache,json_encode($jsonData));}

解决方案

更新的答案

setAccessToken($accessToken);$response = $instagram->getUserMedia($userID, $settings['count']);如果($响应){file_put_contents($cache,json_encode($response));//另存为json}}echo "

";如果(is_array($响应)){foreach($response['data'] 作为 $data){打印_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天全站免登陆