Skip to content

Instantly share code, notes, and snippets.

@woodwardtw
Last active March 20, 2016 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodwardtw/08a1bf90e6726e866a5f to your computer and use it in GitHub Desktop.
Save woodwardtw/08a1bf90e6726e866a5f to your computer and use it in GitHub Desktop.
You'll need an API key, a folder named *imgs* and a file named data.json in the same directory as the script. Change the user_id to your id . . . and I think it'll work.
<?php
$api_key = 'YOUR_API_KEY';
$userID = 'YOUR_USER_ID';
$url = 'https://api.flickr.com/services/rest/?method=flickr.people.getPhotos&api_key='. $api_key . '&user_id='.$userID.'&extras=url_o%2Ctags%2Cgeo%2Cdate_taken&per_page=2&page=1&format=json&nojsoncallback=1';
//return API data
$json = file_get_contents($url);
//var_dump($json);
$obj = json_decode($json);
//open json file to write additional image metadata
$json = file_get_contents('results.json');
$data = json_decode($json);
$totalPages = $obj->photos->pages; //how many total pages . . .
var_dump($totalPages);
for ($i = 1; $i <= $totalPages; $i++) {
$pageNum = 1; //maybe switch w $photoPage
$perPage = 500; //photos returned per page, you might need to adjust this downward
$url = 'https://api.flickr.com/services/rest/?method=flickr.people.getPhotos&api_key='. $api_key . '&user_id='.$userID.'&extras=url_o%2Ctags%2Cgeo%2Cdate_taken&per_page=' . $perPage .'&page=' . $i . '&format=json&nojsoncallback=1';
getPhotos($url);
};
//the function to do the stuff . . .
function getPhotos($url){
//return API data
$json = file_get_contents($url);
//var_dump($json);
$obj = json_decode($json);
//open json file to write additional image metadata
$json = file_get_contents('results.json');
$data = json_decode($json);
//breakdown json from Flickr to get the stuff we want per image
$list = array();
$photoPage = $obj->photos->page; //what page are you on
$photoAllPages = $obj->photos->pages; //how many total pages . . .
foreach ($obj->photos->photo as $media) {
$farmid = $media->farm;
$serverid = $media->server;
$photoid = $media->id;
$photosecret = $media->secret;
$photoid = $media->id;
$ownername = $media->owner;
$phototitle = $media->title;
$tags = $media->tags;
$imageOG = $media->url_o;
$imageLat = $media->latitude;
$imageLong = $media->longitude;
$imageDate = $media->datetaken;
$pageurl = 'https://flickr.com/photos/' . $ownername . '/' . $photoid;
$image = 'https://farm' . $farmid . '.staticflickr.com/' . $serverid . '/' . $photoid . '_' . $photosecret . '_s.jpg' ;
$arr = array('title' => $phototitle, 'tags' => $tags, 'latitude' => $imageLat, 'longitude' => $imageLong, 'date_taken' => $imageDate);
var_dump($arr);
//echo json_encode($arr);
//push the metadata to the json file
$data[] = $arr;
//you may need to make the results.json first- maybe I'll fix that . . .
file_put_contents('results.json', json_encode($data));
copy($imageOG, 'imgs/' . $photoid . '_' . $photosecret . '_o.jpg' );
};
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment