insolis / facebook-service-provider
Lightweight silex service to ease communication with facebook
Installs: 265
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 1
pkg:composer/insolis/facebook-service-provider
Requires
- silex/silex: ~1.1
This package is not auto-updated.
Last update: 2025-10-11 17:51:18 UTC
README
It's a lightweight Silex service to help creating Facebook applications.
Dependencies
It depends on two other providers:
It also uses the current request.
Configuration
It's done via the fb.options key. It's supposed to be an array with the following keys:
app_id: your application's idapp_secret: your application's secret tokenpermissions: an array with the needed permissionsredirect_route: name of the route which is responsible for handling new user authentications
Usage
The service sets the following session keys:
fb.access_token: the access token you can use to make requests against the graph APIfb.page_liked: whether the current page is liked or not
A before() middleware is registered to be run before yours. It handles two cases:
- it's a post request and we have a
signed_requestparameter: decoding is done automatically and set among the post values with thefb.datakey - it's a request for the aforementioned redirect route, and we have a
codequery parameter: fetching a token, then the user details is done automatically, set among the post values with thefb.datakey
In both cases the event fb.user_info is dispatched, with the user data as the subject.
Events
fb.user_infowhen getting user data from facebookfb.likewhen the user just liked the pagefb.unlikewhen the user just unliked the page
Samples
A sample application-level before middleware to log a user in automatically:
<?php $app->before(function (Request $request) use ($app) { if ($request->request->has("fbdata")) { $data = $request->request->get("fbdata"); if (isset($data["user_id"])) { $app["session"]->set("user_id", $data["user_id"]); } } $route = $request->get("_route"); if ( !$app["session"]->has("user_id") && !in_array($request->get("_route"), array("homepage", "fb_addhandler")) ) { return new Response("<script type='text/javascript'>top.location = '" . $app["fb"]->getAuthorizationUrl() . "';</script>"); } });
A sample redirect route to handle new user authentications:
<?php $app->post("/fb_addhandler", function (Request $request) use ($app) { if (!$request->request->has("fbdata")) { return $app->redirect($app["url_generator"]->generate("homepage")); } $data = $request->request->get("fbdata"); try { $app["db.user"]->insert(array( "id" => $data["id"], )); } catch (\Exception $e) { //this is not the user's first time enabling the app } $app["session"]->set("user_id", $data["id"]); return $app->redirect($app["url_generator"]->generate("homepage")); })->bind("fb_addhandler");