The following samples should give you an idea how Phlickr works and what you can do with it. There are also some tools written using Phlickr that are useful for both reference and as an idea of how Phlickr can be used in “real world” applications. If you’d like to see other examples please ask.
Most of these examples require that you have an authentication token.
This sample demonstrates how to search for photos by tag, sort photos, and create a photoset.
<?php include_once 'Phlickr/Api.php'; include_once 'Phlickr/AuthedPhotosetList.php'; include_once 'Phlickr/PhotoList.php'; include_once 'Phlickr/PhotoListIterator.php'; require_once 'Phlickr/PhotoSorter.php'; require_once 'Phlickr/PhotoSortStrategy/ByColor.php'; // use the GetToken.php script to generate a config file. define('API_CONFIG_FILE', './authinfo.cfg'); // the cache file isn't required but if you share it's nice. define('CACHE_FILE', dirname(__FILE__) . '/cache.tmp'); print('Comma separated list of tags: '); // split the tags into an array, dropping commas and white space. $tags = trim(fgets(STDIN)); // create a request to search for photos tagged with person and happy // from all users. $api = Phlickr_Api::createFrom(API_CONFIG_FILE); $request = $api->createRequest( 'flickr.photos.search', array( 'tags' => $tags, 'tag_mode' => 'all', 'user_id' => $api->getUserId() ) ); // use the photo list and photo list iterator to display the titles and urls // of each of the photos. print "Searching for matching photos...\n"; $pl = new Phlickr_PhotoList($request, Phlickr_PhotoList::PER_PAGE_MAX); $count = $pl->getCount(); if ($count == 0) { print "No photos could be found tagged with $tags.\n"; } else { print "Found $count photos tagged with $tags...\n"; // create a sorter that will uses the color sort strategy $strategy = new Phlickr_PhotoSortStrategy_ByColor($api->getCache()); $sorter = new Phlickr_PhotoSorter($strategy); // use a photolist iterator so that all the pages are sorted. print "Sorting the photos (this might take a while)...\n"; $photos = $sorter->sort(new Phlickr_PhotoListIterator($pl)); $photo_ids = Phlickr_PhotoSorter::idsFromPhotos($photos); // create the authed photoset list for the current user... $apsl = new Phlickr_AuthedPhotosetList($api); // .. and create a new photo set $id = $apsl->create($tags, 'photo set created from tags ' . $tags, $photo_ids[0]); // wait a few seconds for the set to be created sleep(3); // now, create the photset ojbect and add the photos $aps = new Phlickr_AuthedPhotoset($api, $id); $aps->editPhotos($photo_ids[0], $photo_ids); print "Created a photoset named '$tags':\n". $aps->buildUrl() ."\n"; } ?>
This sample shows how to upload all the JPG files in a directory to Flickr. Take a look at the PhlickrUploader script listed on the tools page for a more complete uploading example.
<?php // use the GetToken.php script to generate a config file. define('API_CONFIG_FILE', './authinfo.cfg'); define('UPLOAD_DIRECTORY', 'D:/sf/phlickr/samples/files_to_testupload'); define('PHOTO_EXTENSION', '.jpg'); require_once 'Phlickr/Api.php'; require_once 'Phlickr/Uploader.php'; $api = Phlickr_Api::createFrom(API_CONFIG_FILE); if (! $api->isAuthValid()) { die("invalid flickr logon"); } $uploader = new Phlickr_Uploader($api); $photo_ids = array(); // Use the Standard PHP Library's DirectoryIterator to list all the files in // the directory. $di = new DirectoryIterator(UPLOAD_DIRECTORY); foreach($di as $item) { // only files with the given extension... if ($item->isFile()) { if (substr(strtolower($item), - strlen(PHOTO_EXTENSION)) === strtolower(PHOTO_EXTENSION)) { print "Uploading $item...\n"; $photo_id[] = $uploader->upload($item->getPathname(), strtolower($item), '', 'some tags'); } } } if (count($photo_ids)) { printf("All done! If you care to make some changes:\n%s", $uploader->buildEditUrl($photo_ids)); } ?>