Elliott C. Back: <3 Wendy Bug

Facebook PHP Api Performance

Posted in Code, Computers & Technology, Facebook, Web 2.0 by Elliott Back on June 16th, 2007.

If you’re developing for the Facebook API, you’ll have noticed by now that you probably want to have their F8 platform cache things in people’s profiles, using the ref FBML, which looks like this:

<fb:ref url="http://your-site.com/resource"/>
<fb:ref handle="MyItemToCache#1"/>

With the ref-url construction, Facebook will visit your URL and cache it. You can call their API to refresh the url when you would like. With the superior ref-handle construction, you pass (handle, value) pairs to Facebook to cache. This saves you from being hammered by Facebook on all the refresh callbacks because you can just fire and forget your ref-handle updates.

Here is an example from my Stock Quotes Facebook Application, which refreshes its tickers on a regular interval:

Refreshed 1859 stock tickers in 5 seconds!

How on earth can I refresh 371.8 ref handles a second? You certainly can’t; the performance of the standard API is 4.60 ref handles a second. But, I’ve hacked the Facebook PHP Client to give you 80x the ref-refreshing performance. The trick is to parallelize the Facebook api calls, using curl_multi instead of plain old curl. You create a new Facebook API client called $fb and then:

$fb->openTransaction();
// your calls to set / refresh refs
$results = $fb->closeTransaction();

You get back the $results array as well, which you can check for errors if you’d like. If you want, you can download my modified client for your own personal use. Please let me know if there are any bugs!

This entry was posted on Saturday, June 16th, 2007 at 5:48 pm and is tagged with stock tickers, ref url, value pairs, facebook, fb, interval, personal use, lt, array, bugs, fbml, profiles, earth. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

22 Responses to “Facebook PHP Api Performance”

  1. Jonathan Sadowski says:

    How do I use this? The code as posted looks like it replaces the existing FacebookRestClient class. But since that class contains all the functionality to set/refresh refs, I am not sure how to accomplish this.

  2. Rob says:

    Would be cool if you could post a unified diff, but this is great regardless. Thanks!

  3. Ian Warner says:

    Hi

    I think you simply replace the current methods etc in the Real FB class, and add in any other stuff that isnt there like the class attributes

    Ian

  4. Ian Warner says:

    You may wish to test this when running some FQL statements also I had some strange results, in fact I don’t think it worked at all

    Ian

  5. Nikk Folts says:

    How’d you do this? Can you go into details… show more code on where to change things?

  6. Eddie Flyer says:

    Good of you to let us know this can be done…. better if you could show us the modded code….

  7. Elliott Back says:

    Yeah, it’s posted–check the link.

  8. Ian Warner says:

    Is this using the latest update - published on the 26th September?

    Please state this on the site and you should add a version within the file.

    Good work

  9. stefan says:

    Hi, the sample code zip file seems to be corrupt. I can’t decompress it.

  10. Matt says:

    Anyone got a PHP4 version, or can you do this in PHP4?

  11. Elliott Back says:

    I hate to be mean stefan, but you’re a noob:

    [root@fc624389 ~]# wget http://elliottback.com/wp/wp-content/uploads/2007/09/facebookapi_php5_restlib.zip
    –18:04:52– http://elliottback.com/wp/wp-content/uploads/2007/09/facebookapi_php5_restlib.zip
    Resolving elliottback.com… 66.240.243.89
    Connecting to elliottback.com|66.240.243.89|:80… connected.
    HTTP request sent, awaiting response… 200 OK
    Length: 3939 (3.8K) [application/zip]
    Saving to: `facebookapi_php5_restlib.zip’

    100%[=======================================>] 3,939 –.-K/s in 0s

    18:04:52 (250 MB/s) - `facebookapi_php5_restlib.zip’ saved [3939/3939]

    [root@fc624389 ~]# unzip facebookapi_php5_restlib.zip
    Archive: facebookapi_php5_restlib.zip
    inflating: facebookapi_php5_restlib.php
    [root@fc624389 ~]#

  12. Chris says:

    Quick question - I am developing a Facebook application that requires the profile box to be updated on any profile view, based on the user that is currently logged in(not the user that has the profile box).

    What would you suggest as the best way to do this?

  13. Montoya says:

    Elliott, are you getting the stock data from Yahoo! for free? Is there a limit on how many requests you can make?

  14. Elliott Back says:

    Yes, I am. I don’t think there’s a limit.

  15. Nicole says:

    Are you currently accepting advertising on your website elliottback.com?

    Thank you,

    Nicole
    Marketing Manager
    ZTMC, INC.

  16. Dave Jarvis says:

    It seems some people are having trouble with the Facebook Data Store API. I have abstracted its behaviour into several classes. Although incomplete, the WMSFacebookDataStore API allows rudimentary CRUD access to Facebook’s database.

    http://www.davidjarvis.ca/facebook/
    http://www.davidjarvis.ca/facebook/docs/

    Data Store classes:
    WMSFacebookDataStore
    WMSFacebookDataStoreAssociation
    WMSFacebookDataStoreAssociationLink
    WMSFacebookDataStoreObject
    WMSFacebookDataStoreProperty
    WMSFacebookDataStoreRow

    Core classes:
    WMSFacebook
    WMSFacebookApplication
    WMSFacebookFriend (stub)
    WMSFacebookMessage (stub)
    WMSFacebookObject
    WMSFacebookProfile (stub)
    WMSFacebookUser

    At the very least it shows how to use Facebook’s Data Store API.

  17. Ed says:

    Great hack, just tried it and unlike comment above, seems to work fine with the fql I’m using. I did have to take out a CURL option that wasn’t supported by my hosts version of php curl (CURLOPT_TCP_NODELAY).

    Just a thought though, you don’t throw FacebookExceptions with relevant failed calls (i.e. error results etc) in the closeTransaction(). What were your thoughts behind this?

  18. Steven says:

    Isn’t this obsolete now due to Facebooks API batch calling?

  19. danny says:

    is the ref-handle construction really superior to the ref-url construction? i ran some basic tests and it seems not…

    if you push to all your users profiles, then your server only gets hit once when you call the fbml_refreshRefUrl api function. where is the hammering you speak of?

  20. [...] If you’re using fb:ref (which you SHOULD be), you probably want to be using the “push” method, using named handles. Add the php curl_multi hack for performance, and you’ll be off and running. [...]

  21. Ankit says:

    how can i get original image in facebook album using facebook api?

  22. Brian Sanders says:

    Steven is right - the need for this functionality should be obsolete with the new batch methods. See this page for more info:

    http://wiki.developers.facebook.com/index.php/Using_batching_API

Leave a Reply

Powered by WP Hashcash