Online Book Reader

Home Category

Facebook Cookbook - Jay Goldman [142]

By Root 739 0
When you go into the 7-Eleven and scan your thumbprint to pay for the fifth Big Gulp of the day, that transaction immediately shows up in my records. When you ping me on the ole telelink, instead of telling you that the value of your current purchases is $10,314, I pass you a reference to that number in my accounting system. The next time you swipe your thumb and buy a drink, the number updates on my end, and you automatically get the update since you have a reference to where the value is stored, rather than a copy.

OK! Back to Facebook. When you call Friends.get() under nonbatch circumstances, it returns the value of the array of the friends for the user you requested. If the user has another tab open in her browser and accepts a new friend request in it, your copy of the value of her friends array is now out-of-date. This is a fairly unlikely occurrence when you’ve got her attention and she’s looking at your page, but it’s much more likely to happen when you’re batching a number of API operations in which the 12th call might affect the values retrieved from the third call, which gets used in the 15th.

Getting and Setting Cookies


Problem


I need to get or set cookies for my application.

Solution


Use the Data.setCookie() method to set a cookie (where user is the UID of the user you want to drop the cookie for):

$cookieReturn = $facebook->api_client->data_setCookie($user,

'cookie_type', 'chocolate_chip');

Use the Data.getCookies() method to get a cookie (where user is the UID of the user you want to retrieve the cookie for):

$cookie = $facebook->api_client->data_getCookies($user, 'cookie_type');

Discussion


You can specify optional expires and path settings for cookies when you drop them (the defaults are 24 hours and /, respectively):

$cookieReturn = $facebook->api_client->data_setCookie($user,

'cookie_type', 'chocolate_chip', '1254079800', '/some/path/here');

Note that the expires argument is measured in epoch seconds (see Formatting Relative Time for more information about epoch time).

If you don’t specify a cookie name for Data.getCookies(), it will return all of the available cookies for the specified user.

Cookies in Facebook are handled differently than a regular browser-side cookie dropped by a regular web page. The cookies are actually managed by Platform and are associated with the user’s account rather than residing in her browser, so they’ll be present whenever that user logs into Facebook from any computer. Facebook will pass any unexpired cookies to your application with each request from a Canvas page to your callback URL, and will store any cookies you return (up to a limit of 50 cookies per app) so that you can access them next time. Some important things to note:

You can use cookies to store data that you would otherwise have to roundtrip through API calls. This can be a very effective performance enhancement, since cookies are sent to you with each request. Keep in mind, however, that your request size will grow considerably if you fill it full of cookies. There’s a careful balancing act at play here, but it’s one that you can measure quite easily (check the average roundtrip time over a series of requests using the API, then compare to the average of the same number of roundtrips using cookies).

Cookies without an expires attribute expire after 24 hours by default.

Stored cookies are associated with the application ID and not with your callback URL’s domain (which is great because it means you can move to a different server or address without issues).

Facebook cookies have a path attribute just like regular cookies, which is evaluated relative to the callback URL of your application (rather than to a path on Facebook).

You can drop and retrieve cookies for users who haven’t logged into your application.

Only web-based Facebook apps can use cookies. There is no cookie support for desktop apps.

The cookie API calls do not require a session key.

NOTE

These methods are currently marked as beta in the Developers Wiki, so treat them with a little caution

Return Main Page Previous Page Next Page

®Online Book Reader