Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php /* vim: set noet ts=4 sw=4 ft=php: : */
- define('FLICKR_GROUP_ID', '610963@N20');
- define('FLICKR_REST_URL', 'http://api.flickr.com/services/rest/');
- define('FLICKR_REST_METHOD', 'flickr.groups.pools.getPhotos');
- define('FLICKR_REST_FORMAT', 'json');
- function pregen_flickr($apiKey, $outputPath, $limit = 100)
- {
- // ensure caller specified a place to write to.
- $outputPath = rtrim($outputPath, '/');
- if (!$outputPath) {
- trigger_error("No flickr output directory specified.", E_USER_WARNING);
- return;
- }
- // ensure path exists and is writable.
- if (!is_dir($outputPath)) {
- if (!@mkdir($outputPath, 0755)) {
- trigger_error("Can't create flickr output directory: $outputPath", E_USER_WARNING);
- return;
- }
- }
- if (!is_writable($outputPath)) {
- if (!@chmod($outputPath, 0755)) {
- trigger_error("Can't make flickr output directory writable: $outputPath", E_USER_WARNING);
- return;
- }
- }
- // construct flickr api call
- // extras is set to 'url_sq' to get the href of the square thumbnail.
- // see http://www.flickr.com/services/api/flickr.groups.pools.getPhotos.html
- $url = FLICKR_REST_URL . '?' . http_build_query(array(
- 'api_key' => $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);
- $photos = $decoded['photos']['photo'];
- // write out the photo info.
- file_put_contents($outputPath . '/photos.json', $response);
- // fetch all of the photos
- foreach ($photos as $photo) {
- file_put_contents(
- $outputPath . '/' . basename($photo['url_sq']),
- file_get_contents($photo['url_sq'])
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment