简体   繁体   中英

Instagram API: Get All User Media API and Store to File

I know the way to get all user media in instagram api with pagination. And we must request again with pagination url provided to get next photos.

I just wonder if i can save all of json api response include with next photos in pagination to one flat file for caching. The purpose is i can call all photos value from one file only, eg: cache.json.

Is there a way to realize that in PHP Code if possible? Like using file_get and file_put function. Any help is appreciated so much :)

Here's my code, but need a tweak to fix it. Im using this wrapper 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';

With this code i getting the last pagination only. It seems the code overwrite the cache.json file, not adding. Maybe you can suggest me how to fix it become adding, not overwriting.

-- Edit --

My code now working but not perfect, maybe you can try and fix it.

<?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';
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM