$apiKey, 'per_page' => $limit, 'extras' => 'url_sq', 'group_id' => FLICKR_GROUP_ID, 'method' => FLICKR_REST_METHOD, 'format' => FLICKR_REST_FORMAT, 'nojsoncallback' => 1 ]); // fetch photo info $response = file_get_contents($url); $decoded = json_decode($response, true); // ensure photo info decoded successfully. if (!isset($decoded['photos']['photo'])) { trigger_error("Failed to decode flickr service response.", E_USER_WARNING); return; } // fetch all of the photos $fresh = []; $photos = $decoded['photos']['photo']; foreach ($photos as $key => &$photo) { // sanitize url_sq for use as the image filename. $imageFile = $outputPath . '/' . FLICKR_FILE_PREFIX . preg_replace('/[^a-z0-9_\.]/i', '', basename($photo['url_sq'])); $photo['filename'] = basename($imageFile); $fresh[] = $photo['filename']; // skip photo if we've already got it. if (file_exists($imageFile)) { continue; } // attempt to fetch the file from flickr. $imageData = file_get_contents($photo['url_sq']); // if image downloaded successfully, write it out; // otherwise, remove it from the list of photos. if ($imageData) { file_put_contents($imageFile, $imageData); } else { unset($photos[$key]); } } // if we failed to download any images, don't commit photo info. if (!count($photos)) { trigger_error("Failed to download any flickr photos.", E_USER_WARNING); return; } // write out the photo info. file_put_contents($outputPath . '/flickr.json', json_encode($photos)); // remove stale flickr images. $files = scandir($outputPath); foreach ($files as $file) { if (strpos($file, FLICKR_FILE_PREFIX) === 0 && !in_array($file, $fresh)) { unlink($outputPath . '/' . $file); } } }