Je sens que je suis en train de trop réfléchir. Ce que je veux faire est de tirer les photos les plus récentes de l'API instagram et enregistrer les informations JSON qui en résultent dans un fichier cache. J'utiliserai alors jQuery pour lire ce fichier - j'ai compris cette partie. Ce que j'utilise maintenant est de le sauvegarder dans un fichier cache, mais pas dans un format que je reconnais. Je pense que je suis trop compliqué.Utiliser PHP pour stocker json via l'API Instagram
C'est le code que je travaille avec la base d'un tutoriel j'ai trouvé:
// Client ID for Instagram API
$instagramClientID = '9110e8c268384cb79901a96e3a16f588';
$api = 'https://api.instagram.com/v1/media/popular?client_id='.$instagramClientID; //api request (edit this to reflect tags)
$cache = 'cache.txt';
if(file_exists($cache) && filemtime($cache) > time() - 60*60){
// If a cache file exists, and it is newer than 1 hour, use it
$images = unserialize(file_get_contents($cache));
}
else{
// Make an API request and create the cache file
// For example, gets the 32 most popular images on Instagram
$response = file_get_contents($api); //change request path to pull different photos
$images = array();
// Decode the response and build an array
foreach(json_decode($response)->data as $item){ // Decodes json (javascript) into an array
$title = '';
if($item->caption){
$title = mb_substr($item->caption->text,0,70,"utf8");
}
$src = $item->images->standard_resolution->url; //Caches standard res img path to variable $src
$lat = $item->data->location->latitude; // Caches latitude as $lat
$lon = $item->data->location->longtitude; // Caches longitude as $lon
$images[] = array(
"title" => htmlspecialchars($title),
"src" => htmlspecialchars($src),
"lat" => htmlspecialchars($lat),
"lon" => htmlspecialchars($lon) // Consolidates variables to an array
);
}
// Remove the last item, so we still have
// 32 items when when the cover is added
//array_pop($images);
// Push the cover in the beginning of the array
//array_unshift($images,array("title"=>"Cover", "src"=>"assets/img/cover.jpg"));
// Update the cache file
file_put_contents($cache,serialize($images));
}
Si vous souhaitez enregistrer le fichier en tant que JSON, utilisez 'json_encode' au lieu de 'sérialiser '. –
@MichaelMior merci. –