pingpong-labs / facebook by gravitano

Facebook OAuth v4 for Laravel Framework
29,680
9
5
Package Data
Maintainer Username: gravitano
Maintainer Contact: pingpong.labs@gmail.com (Pingpong Labs)
Package Create Date: 2014-07-07
Package Last Update: 2017-09-04
Home Page: https://packagist.org/packages/pingpong/facebook
Language: PHP
License: BSD-3-Clause
Last Refreshed: 2024-12-14 15:07:26
Package Statistics
Total Downloads: 29,680
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 9
Total Watchers: 5
Total Forks: 7
Total Open Issues: 1

Facebook PHP SDK v4 for Laravel

Build Status

Server Requirement

This package is require PHP 5.4 or higher.

Installation

For Laravel 4 please use version 1.*.

Open your composer.json file, and add the new required package.

   "pingpong/facebook": "~2.0"

Next, open a terminal and run.

composer update

Next, Add new service provider in app/config/app.php.

  'Pingpong\Facebook\FacebookServiceProvider',

Next, Add new aliases in app/config/app.php.

   'Facebook' => 'Pingpong\Facebook\Facades\Facebook',

Next, publish the configuration.

php artisan vendor:publish --provider="Pingpong\Facebook\FacebookServiceProvider"

Done.

Usage

First, you must set the app_id and the app_secret in the configuration file. You can also set the default scope and the redirect url.

return array(
	'app_id'		=>	'',
	'app_secret'	=>	'',
	'redirect_url'	=>	url('facebook/callback'),
	'scope'			=>  array(
		'publish_actions',
		'email'
	)
);

Get facebook login url.

Facebook::getLoginUrl();

You can also update the scope.

$scope = array('email');

Facebook::getLoginUrl($scope);

Authenticate the user and get the permissions. This will automatically redirect the user to the facebook login url.

Facebook::authenticate();

If you want to update/override the scope, you can add the scope in the first parameter.

$scope = array('email');

Facebook::authenticate($scope);

You can also set the version of Facebook API want to use.

$version = 'the-version';
$scope = array('email');

Facebook::authenticate($scope, $version);

By default when you login to to facebook using oauth, it will show you a login page in website version. If you want to using popup version, call displayAsPopup method.

Facebook::displayAsPopup();

return Facebook::authenticate($scope, $version);

Get user profile for the current logged in user.

Facebook::getProfile();

Get call back from redirect. Will return a value of true if the authentication is successful and otherwise.

Facebook::getCallback();

Logout the current active user.

Facebook::destroy();
// or
Facebook::logout();

Call the Facebook API.

Facebook::api($method, $path, $parameters, $version);

Facebook::api('GET', '/me');

Facebook::api('POST', '/me/feed', $parameters);

Facebook::api('PUT', '/path', $parameters);

Facebook::api('PATCH', '/path', $parameters);

Facebook::api('DELETE', '/path/to', $parameters);

Helper method for call Facebook API.

GET Request

Facebook::get('/path', $parameters);

POST Request

Facebook::post('/path', $parameters);

PUT Request

Facebook::put('/path', $parameters);

PATCH Request

Facebook::patch('/me', $parameters);

DELETE Request

Facebook::delete('/me', $parameters);

Example

Authentication and authorization

Route::group(['prefix' => 'facebook'], function ()
{
	Route::get('connect', function ()
	{
		return Facebook::authenticate();
	});

	Route::get('callback', function ()
	{
		$callback = Facebook::getCallback();

		if($callback)
		{
			$profile = Facebook::getProfile();
			
			dd($profile);
		}

		dd($callback);
	});
});

License

This package is open-sourced software licensed under The BSD 3-Clause License