Domanda

Sto lavorando con cosenenary Instagram PHP API Classes per recuperare le foto da Instagrame prova a creare il file della cache per migliorare la velocità.Di seguito lo snippet ma non funziona.

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

Ho controllato il file Instagram.json Basta lasciare il file vuoto con dimensioni 0 byte. Sto usando il codice per creare il file cache da un'altra domanda qui Richieste di caching instagram API usando PHP?

- Modifica -

Se sostituisco $ contenuti con JSON URL, funziona.Ma se utilizzo $ contenuti come parte della classe API, non ha funzionato.

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

È stato utile?

Soluzione

Risposta aggiornata

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top