2014-01-10 46 views
0

我知道如何通過分頁獲取所有用戶媒體的instagram api。我們必須再次提供分頁網址才能獲取下一張照片。Instagram API:獲取所有用戶媒體API並存儲到文件

我只是想知道如果我能保存所有的json api響應包括下一張照片在分頁到一個平面文件進行緩存。目的是我只能從一個文件中調用所有的照片值,例如:cache.json。

有沒有一種方法可以實現在PHP代碼如果可能?就像使用file_get和file_put函數一樣。任何幫助非常感謝:)

+0

讓我們看看你的代碼,你試過了什麼? – cmorrissey

+0

如果您將參數'&count = -1'添加到請求的末尾,您可以將完整的圖像列表視爲一個大文件。 – PlantTheIdea

+0

不,params&count = -1你會失去許多值,如標題值爲空,評論和其他。 我沒試過。我只是有一個想法,可能會將商店全部實現爲一個文件,並且如果使用instagram帳戶進行更新只是觸發代碼重新獲取並存儲所有文件。 – cutez7boyz

回答

0

這是我的代碼,但需要一個調整來解決它。我使用這種包裝https://github.com/cosenary/Instagram-PHP-API

require 'instagram.class.php'; 
$cache = './cache.json'; 

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

do { 

if($response){ 
file_put_contents($cache,json_encode($response)); //Save as json 
} 
} while ($response = $instagram->pagination($response)); 

echo 'finish'; 

有了這段代碼,我只得到最後的分頁。看起來代碼覆蓋了cache.json文件,而不是添加。 也許你可以建議我如何解決它變得添加,而不是覆蓋。

- 編輯 -

我的代碼現在的工作,但並不完美,也許你可以嘗試修復它。

<?php 
include('conf.php'); 
require 'instagram.class.php'; 

$cache = './cache_coba.json'; 

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

while ($response = $instagram->pagination($response)) { 
if($response){ 
$opn = file_get_contents($cache); 
$opn .= json_encode($response); 
file_put_contents($cache, $opn); 
} 
} 

echo 'finish'; 
?> 
相關問題