I put this page together to keep track of all the cool JavaScript nonsense I've put together recently.
For the internal Yahoo hack day, I wrote a hack that allows you to use the Flickr webservices API from JavaScript, with no backend scripting, such as PHP. Initially, I took the XML returned by the raw Flickr API and converted it to JSON manually. Since I wrote the hack, Flickr has released an option in the API to get JSON results back, rather than stupid XML. I've since updated my JavaScript API to handle such a situation, and I'm going to post it here.
Before I link to the code, it's important to note that unless you're the Flickr team using this API on the flickr.com domain, the script won't work immediately - this is because of browser security settings pertaining to cross-domain XMLHttpRequest calls, that is, you cannot open an XMLHttpRequest to any domain other than the one serving the page you currently have open.
For more info on this, see the Yahoo Developer Network documentation. I handled the cross domain problem using Premshree's hack, (using Apache mod_proxy and mod_rewrite), but there are other ways to handle it, such as a PHP proxy script or dynamic JavaScript embedding.
As I said, there are two ways to do it:
So here's an example of how to use the API:
//the first param is your Flickr developer key
//please substitute your own key here
var flickr = new FlickrAPI('cf0fca0cd8c6058a2d625f50c42ba08d', '');
var params = new Array();
params['per_page'] = 15;
params['api_key'] = 'cf0fca0cd8c6058a2d625f50c42ba08d';
var json = flickr.callMethodJSON('flickr.interestingness.getList', params);
var obj = eval('(' + json + ')');
var photos = obj.photos.photo;
for(i = 0; i < photos.length; i++){
url = flickr.getPhotoURL(photos[i]);
document.write('<img src=' + url + '>my pic</img>');
}
So the exact recipe is:
Here is a working example:
<script type=text/javascript>
var flickr = new FlickrAPI('cf0fca0cd8c6058a2d625f50c42ba08d', '');
var params = new Array();
params['per_page'] = 15;
params['api_key'] = 'cf0fca0cd8c6058a2d625f50c42ba08d';
var json = flickr.callMethodJSON('flickr.interestingness.getList', params);
var obj = eval('(' + json + ')');
var photos = obj.photos.photo;
for(i = 0; i < photos.length; i++){
url = flickr.getPhotoURL(photos[i]);
document.write('<img src=' + url + '></img>');
}
</script>
That code will show these images in your page:
First, you can use the code above in your client page. Make sure to switch on the api key (first arg to FlickrAPI constructor.) Next, you'll need to use any of the domain-security workarounds I linked to above. If you want to use the one that uses mod_rewrite and mod_proxy, which I think is the quickiest and easiest way, then you'll need to make the following changes.
First, create a mod_rewrite entry in your .htaccess file, that looks a little something like this:
RewriteEngine on RewriteRule ^\/rest\/(.*)$ http://api.flickr.com/services/rest/$1 [P]
What does all this mean?
Unfortunately, I can't show you this working at the moment because Dreamhost doesn't enable mod_proxy for me. But trust me, this works! However, as I mentioned above, you can use a PHP proxy script to do this too. To accomplish this, read the section below.
JavaScript Carousel Implementation
Scriptaculous / Prototype Examples