Aug
17th

Display your Most Popular Posts with Clicky’s API

One of the sessions I did today was on integrating external sites to your own site via API’s. Though the talk was supposed to be non-technical, I couldn’t help but dabble in the code a little. I wanted to dispel the myth of how difficult it was to connect to an external site and pull in data via XML. One attendee was kind enough to re-enact her facial expressions of confusion to me after the presentation (I wish I had it on video!).

When you request data via an API, it’s the same exact process a web browser utilizes. The browser makes a request to the server for the appropriate page (data) and it’s returned and rendered in the browser. With an Application Programming Interface, the server makes the request and then you program how you wish that content to be rendered in the web page yourself. Pretty straight forward!

To illustrate the process, I provided a sample where I query the API of Clicky Web Analytics and get the XML in the result set. It was done with this much PHP (false id and sitekey):

The code requests the XML, parses it and then displays it in a typical HTML list.

Link to Code. Your site must be running PHP 5.2 with cURL enabled. Please don’t email me if it doesn’t work on your server (I get A LOT of emails like this). If you want to experiment with additional APIs out there, be sure to check out Programmable Web, the most comprehensive resource of APIs and Web Services on the net.

Of course, thanks to geeks like me who love to experiment with APIs, we build plugins and widgets for WordPress and other sites so that you don’t have to!

You might also find these posts interesting:

  • **insert facial expression here**
  • Great session today at blogINDIANA!

    When a service does provide an API, I am sure I can find (or create) a bit of code to query their service and produce the output HTML I want. As I mentioned during your talk, this can even cache the content (or just poll the site on a schedule) to improve load times.

    However, it looks like most of the widgets out there are just bits of JavaScript. This seems like a pretty inelegant solution. There will be performance problems, since the browser has to make multiple sequential requests and reach out to servers with varying response times, and finally execute the JavaScript. More importantly, whose to say the third party won't update their remote content to change what appears in my site?

    I think the right solution is to find (or build) a tool that pulls down the remote widget, executes the JavaScript, and re-imports the final rendered HTML into the source of the page. This could cache the content to improve performance and watch for changes from the vendor. One could also customize the appearance of the widget as well.

    Some pondering googling turned up only really esoteric solutions, like running Xvfb+Firefox or running a page which polls document.all and posts the contents to a form. I even tried scripting the Links (not Lynx) browser, which didn't seem to work.

    Any ideas? Or am I off the deep end?

    Robby Slaughter
    http://www.robbyslaughter.com/
  • Hi Robby,

    I think it's really dependent upon your needs. If I want my page to load quickly and I don't care if the content gets crawled by Search Engines, a Javascript solution is pretty good. As well, wherever the Javascript is accessing information is where I can worry about caching.

    Also - the ability to change the content may be something advantageous. With ads, perhaps you've got a 24 hour sale. Why not just swap the content on the adserver to change all the ads on your clients than having to change it on each?

    I do agree that most companies tend to come up with inelegant solutions, but the pressure is on them. I wrote a few posts about the slew of commenting systems that hit the blogosphere and warned people against many of them. Since I and others posted quite a bit, there have been a couple of these services that converted to server-side synching mechanisms. Eventually all of them will!

    Thanks for the great commentary!
blog comments powered by Disqus