1. Go to this page and download the library: Download moomak/laravel-facebook-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// Directly from the IoC
$fb = App::make('Moomak\LaravelFacebookSdk\LaravelFacebookSdk');
// Or in PHP >= 5.5
$fb = app(Moomak\LaravelFacebookSdk\LaravelFacebookSdk::class);
// From a constructor
class FooClass {
public function __construct(Moomak\LaravelFacebookSdk\LaravelFacebookSdk $fb) {
// . . .
}
}
// From a method
class BarClass {
public function barMethod(Moomak\LaravelFacebookSdk\LaravelFacebookSdk $fb) {
// . . .
}
}
// Or even a closure
Route::get('/facebook/login', function(Moomak\LaravelFacebookSdk\LaravelFacebookSdk $fb) {
// . . .
});
$login_link = $fb->getLoginUrl(['email', 'user_status'], 'https://exmaple.com/facebook/callback');
// Or, if you want to default to the callback URL set in the config
$login_link = $fb->getLoginUrl(['email', 'user_status']);
Route::get('/facebook/callback', function(Moomak\LaravelFacebookSdk\LaravelFacebookSdk $fb) {
try {
$token = $fb->getAccessTokenFromRedirect();
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// Failed to obtain access token
dd($e->getMessage());
}
// $token will be null if the user denied the request
if (! $token) {
// User denied the request
}
});
Route::get('/facebook/javascript', function(Moomak\LaravelFacebookSdk\LaravelFacebookSdk $fb) {
try {
$token = $fb->getJavaScriptHelper()->getAccessToken();
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// Failed to obtain access token
dd($e->getMessage());
}
// $token will be null if no cookie was set or no OAuth data
// was found in the cookie's signed request data
if (! $token) {
// User hasn't logged in using the JS SDK yet
}
});
Route::match(['get', 'post'], '/facebook/canvas', function(Moomak\LaravelFacebookSdk\LaravelFacebookSdk $fb) {
try {
$token = $fb->getCanvasHelper()->getAccessToken();
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// Failed to obtain access token
dd($e->getMessage());
}
// $token will be null if the user hasn't authenticated your app yet
if (! $token) {
// . . .
}
});
Route::match(['get', 'post'], '/facebook/page-tab', function(Moomak\LaravelFacebookSdk\LaravelFacebookSdk $fb) {
try {
$token = $fb->getPageTabHelper()->getAccessToken();
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// Failed to obtain access token
dd($e->getMessage());
}
// $token will be null if the user hasn't authenticated your app yet
if (! $token) {
// . . .
}
});
$rerequest_link = $fb->getReRequestUrl(['email'], 'https://exmaple.com/facebook/login');
// Or, if you want to default to the callback URL set in the config
$rerequest_link = $fb->getReRequestUrl(['email']);
$re_authentication_link = $fb->getReAuthenticationUrl(['email'], 'https://exmaple.com/facebook/login');
// Or, if you want to default to the callback URL set in the config
$re_authentication_link = $fb->getReAuthenticationUrl(['email']);
// Or without permissions
$re_authentication_link = $fb->getReAuthenticationUrl();
use Moomak\LaravelFacebookSdk\SyncableGraphNodeTrait;
class Event extends Eloquent {
use SyncableGraphNodeTrait;
}
$response = $fb->get('/some-event-id?fields=id,name');
$eventNode = $response->getGraphEvent();
// A method called createOrUpdateGraphNode() on the `Event` eloquent model
// will create the event if it does not exist or it will update the existing
// record based on the ID from Facebook.
$event = Event::createOrUpdateGraphNode($eventNode);
use Moomak\LaravelFacebookSdk\SyncableGraphNodeTrait;
class User extends Eloquent implements UserInterface
{
use SyncableGraphNodeTrait;
protected static $graph_node_field_aliases = [
'id' => 'facebook_user_id',
'name' => 'full_name',
'graph_node_field_name' => 'database_column_name',
];
}
use Moomak\LaravelFacebookSdk\SyncableGraphNodeTrait;
class Event extends Eloquent
{
use SyncableGraphNodeTrait;
protected static $graph_node_fillable_fields = ['id', 'name', 'start_time'];
}
use Moomak\LaravelFacebookSdk\SyncableGraphNodeTrait;
class Event extends Eloquent
{
use SyncableGraphNodeTrait;
protected static $graph_node_field_aliases = [
'id' => 'facebook_id',
'place.location.city' => 'city',
'place.location.state' => 'state',
'place.location.country' => 'country',
];
}
use Moomak\LaravelFacebookSdk\SyncableGraphNodeTrait;
class Event extends Eloquent
{
use SyncableGraphNodeTrait;
protected static $graph_node_date_time_to_string_format = 'c'; # ISO 8601 date
}
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddFacebookColumnsToUsersTable extends Migration
{
public function up()
{
Schema::table('users', function(Blueprint $table)
{
// If the primary id in your you user table is different than the Facebook id
// Make sure it's an unsigned() bigInteger()
$table->bigInteger('facebook_user_id')->unsigned()->index();
// Normally you won't need to store the access token in the database
$table->string('access_token')->nullable();
});
}
public function down()
{
Schema::table('users', function(Blueprint $table)
{
$table->dropColumn(
'facebook_user_id',
'access_token'
);
});
}
}
# User.php
use Moomak\LaravelFacebookSdk\SyncableGraphNodeTrait;
class User extends Eloquent implements UserInterface {
use SyncableGraphNodeTrait;
protected $hidden = ['access_token'];
}
class FacebookController {
public function getUserInfo(Moomak\LaravelFacebookSdk\LaravelFacebookSdk $fb) {
try {
$response = $fb->get('/me?fields=id,name,email');
} catch (Facebook\Exceptions\FacebookSDKException $e) {
dd($e->getMessage());
}
// Convert the response to a `Facebook/GraphNodes/GraphUser` collection
$facebook_user = $response->getGraphUser();
// Create the user if it does not exist or update the existing entry.
// This will only work if you've added the SyncableGraphNodeTrait to your User model.
$user = App\User::createOrUpdateGraphNode($facebook_user);
// Log the user into Laravel
Auth::login($user);
}
}
Route::get('/example', function(Moomak\LaravelFacebookSdk\LaravelFacebookSdk $fb) {
// All the possible configuration options are available here
$fb2 = $fb->newInstance([
'app_id' => env('FACEBOOK_APP_ID2'),
'app_secret' => env('FACEBOOK_APP_SECRET2'),
'default_graph_version' => 'v2.10',
// . . .
]);
});
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'facebook/canvas',
'facebook/page-tab',
// ... insert all your canvas endpoints here
];
}
namespace App\Http\Middleware;
use Closure;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
use Illuminate\Session\TokenMismatchException;
class VerifyCsrfToken extends BaseVerifier
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*
* @throws TokenMismatchException
*/
public function handle($request, Closure $next)
{
if ($this->isReading($request) || $this->excludedRoutes($request) || $this->tokensMatch($request)) {
return $this->addCookieToResponse($request, $next($request));
}
throw new TokenMismatchException;
}
/**
* Ignore CSRF on these routes.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
private function excludedRoutes($request)
{
$routes = [
'my-app/canvas',
'my-app/page-tab',
// ... insert all your canvas endpoints here
];
foreach($routes as $route){
if ($request->is($route)) {
return true;
}
}
return false;
}
}
bash
$ php artisan migrate
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.