Facebook PHP Api Performance
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. |




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
how can i get original image in facebook album using facebook api?
[...] 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. [...]
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?
Isn’t this obsolete now due to Facebooks API batch calling?
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?
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.
Are you currently accepting advertising on your website elliottback.com?
Thank you,
Nicole
Marketing Manager
ZTMC, INC.
Yes, I am. I don’t think there’s a limit.
Elliott, are you getting the stock data from Yahoo! for free? Is there a limit on how many requests you can make?
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?
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 ~]#
Anyone got a PHP4 version, or can you do this in PHP4?
Hi, the sample code zip file seems to be corrupt. I can’t decompress it.
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
Yeah, it’s posted–check the link.
Good of you to let us know this can be done…. better if you could show us the modded code….
How’d you do this? Can you go into details… show more code on where to change things?
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
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
Would be cool if you could post a unified diff, but this is great regardless. Thanks!
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.