joomla / facebook
Joomla Facebook Package
Fund package maintenance!
joomla
community.joomla.org/sponsorship-campaigns.html
Installs: 13 746
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 17
Forks: 12
Open Issues: 0
Type:joomla-package
Requires
- php: ^5.3.10|>=7.0 <7.2
- joomla/http: ~1.0|~2.0
- joomla/input: ~1.0|~2.0
- joomla/oauth2: ^1.1.2|~2.0
- joomla/uri: ~1.0|~2.0
Requires (Dev)
- joomla/coding-standards: ~2.0@alpha
- joomla/test: ~1.0
- phpunit/phpunit: ^4.8.35|^5.4.3|~6.0
Suggests
- joomla/registry: Registry can be used as an alternative to using an array for the package options.
README
Deprecated
The joomla/facebook
package is deprecated with no further updates planned.
Using the Facebook Package
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
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');
Accessing the Facebook API's objects
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.
A More Complete Example
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");
More Information
The following resources contain more information
Installation via Composer
Add "joomla/facebook": "2.0.*@dev"
to the require block in your composer.json and then run composer install
.
{ "require": { "joomla/facebook": "2.0.*@dev" } }
Alternatively, you can simply run the following from the command line:
composer require joomla/facebook "2.0.*@dev"
If you want to include the test sources, use
composer require --prefer-source joomla/facebook "~1.0"