Вопрос

Я работаю с COSENARY Instagram PHP-классы API для извлечения фотографий из InstagramИ попробуйте создать файл кеша для улучшения скорости.Ниже есть фрагмент, но это не работает.

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

Я проверял файл Instagram.json Просто оставьте пустой файл с размером 0 байта. Я использую код для создания файла кеша с другого вопроса здесь кэширование запросов Instagram API с использованием PHP?

- редактировать -

Если я заменил $ содержимое с JSON URL, он работает.Но если я использую $ Content, как часть класса API, он не работает.

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

Это было полезно?

Решение

Обновлен ответ

<?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);
    }
}
.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top