The Facebook package is designed to be a straightforward interface for working with Facebook. It is based on the Graph API. You can find documentation on the API at http://developers.facebook.com/docs/reference/api/.
Instantiating Facebook is easy:
use Joomla\Facebook\Facebook;
$facebook = new Facebook;
This creates a basic Facebook object that can be used to access publicly available resources on facebook.com which don't require an active access token.
Sometimes it is necessary to provide an active access token with the required permissions. This can be done by instantiating OAuth.
Create a Facebook application at https://developers.facebook.com/apps in order to request permissions. Instantiate OAuth, passing the Registry options needed. The API key, API secret and callback URL (which is the script's path) from the Facebook application are passed through the Registry object. By default you have to send headers manually in your application, but if you want this to be done automatically you can set Registry's option 'sendheaders' to true.
use Joomla\Facebook\Facebook;
use Joomla\Facebook\OAuth;
$options = array(
'clientid' => $app_id,
'clientsecret' => $app_secret,
'redirecturi' => $callback_url,
'sendheaders' => true,
'authmethod' => 'get'
);
$oauth = new OAuth($options);
$facebook = new Facebook($oauth);
Now you can authenticate and request the user to authorise your application in order to get an access token, but if you already have an access token stored you can set it to the OAuth object and if it's still valid your application will use it.
// Set the stored access token.
$oauth->setToken($token);
$access_token = $oauth->authenticate();
When calling the authenticate() method, your stored access token will be used only if it's valid, a new one will be created if you don't have an access token or if the stored one is not valid. The method will return a valid access token that's going to be used.
Set scope to the OAuth object. Scope is a comma separated list of requested permissions:
$oauth->setScope('read_stream,publish_stream');
The Facebook package has 12 objects of the Graph API currently implemented:
- Album
- Checkin
- Comment
- Event
- Group
- Link
- Note
- Photo
- Post
- Status
- User
- Video
Once a Facebook object has been created, it is simple to use it to access Facebook:
$user = $facebook->user->getFeed($user_id);
This will retrieve an array of Post objects containing (up to) the last 25 posts.
Below is an example demonstrating more of the Facebook package.
use Joomla\Facebook\Facebook;
use Joomla\Facebook\OAuth;
$app_id = "app_id";
$app_secret = "app_secret";
$my_url = 'http://localhost/facebook_test.php';
$options = array(
'clientid' => $app_id,
'clientsecret' => $app_secret,
'redirecturi' => $callback_url,
'sendheaders' => true,
'authmethod' => 'get'
);
$oauth = new OAuth($options);
$oauth->authenticate();
$facebook = new Facebook($oauth);
$user = $facebook->user;
$response = $user->getFeed("me");
The following resources contain more information
Add "joomla/facebook": "~1.0"
to the require block in your composer.json and then run composer install
.
{
"require": {
"joomla/facebook": "~1.0"
}
}
Alternatively, you can simply run the following from the command line:
composer require joomla/facebook "~1.0"