2014-01-10 89 views
0

我正在與Cosenary Instagram PHP API Classes一起從instagram獲取照片並嘗試創建緩存文件以提高速度。下面是代碼片段,但它不起作用。從Instagram創建緩存文件API類

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字節大小的空白文件。 我使用代碼來創建從這裏Caching Instagram API requests using PHP?

其他問題緩存文件 - 編輯 -

如果我使用JSON網址替換$內容,它的工作原理。但是,如果我使用$內容作爲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)); 
} 
+0

我提供完整的URL,這樣我可以爲你做些什麼。 – kwelsan

+0

我編輯了上面的問題。請檢查,謝謝。 – cutez7boyz

回答

2

修訂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); 
    } 
} 
+0

我想要的是使它適用於Instagram的PHP API類從這裏:https://github.com/cosenary/Instagram-PHP-API/ 但我讚賞你的答案:) – cutez7boyz

+1

請檢查更新的答案與youyr所需的API – kwelsan

+0

如何獲取所有值,不僅限於標題,src,lat,lon,因爲我也想獲取評論值。 – cutez7boyz