Integrating Facebook Login into Laravel application

In this tutorial, we are going to create a Facebook login feature for Laravel 5 using Socialite package.

Note: In order to complete this tutorial, you should have a basic understanding of Laravel Framework.

Step 1: Installation and Configuration:

a) To get started, navigate to your Laravel installation and type the following command to install socialite

composer require laravel/socialite

Recommended: How to Install composer on Windows with XAMPP

b) In the next step, open to config/app.php and add the code given below to providers array.

socialite-facade

socialite-facade

c) Also, add the Socialite’s facade to aliases.

socialite

socialite

Socialite is now registered within your application.

Step 2: Create a Facebook App:

a) Visit Facebook Developer Page and create a new app.

Recommended: http://coffeecupweb.com/how-to-get-an-app-id-and-secret-key-from-facebook/

b) After creating the app, navigate to App > Settings > Add platform.

c) In Select Platform Popup, select Website. 

d) This will create a new Website Section on settings page. Within Website section look for Site URL textbox. In this textbox enter your websites URL.

e) And, copy the App ID and App Secret for your application.

Step 3: Add Facebook App ID and Secret to Your Application:

Navigate to app\services.php and the following details,

'facebook' => [
    'client_id' => 'facebook_app_id',
    'client_secret' => 'facebook_app_secret',
    'redirect' => 'redirect_url',
 ],

redirect is the URL where user will be redirected after authentication.

Step 4: Setup routes and controller methods

We are going to create to routes in our application, one for redirecting user to authentication page and second will be the callback where user will be redirected after authentication.

Define routes for laravel socialite

Define routes for laravel socialite

Note: Add these routes inside web middleware group in routes file.

Next, we need to create redirectToFacebook and handleCallback methods inside Auth\AuthController.php file.

AuthController Socialite

AuthController Socialite

Make sure, you have added socialite class reference at the top of the controller.

use Socialite;

Once you have user instance inside handleCallback, you can grab the details available within the instance and can save it to your database.

That’s it. In case you have any queries, please let me know.

How to Embed Facebook Status in WordPress

Facebook is the largest social networking website in the world, which has more than 1 billion active users across the globe.  Millions of exciting messages, photo and news shared on Facebook everyday.

Facebook allows you to embed this information directly into your website.

In this post, we are going to embed a Facebook status in out WordPress blog post. To embed a status into the blog post, follow the steps given below.

Laravel Tip : Rendering a View to a String

1. Click the date or time link which will take you to that page of that specific status

How to Embed Facebook Status Posts in WordPress

How to Embed Facebook Status Posts in WordPress

2. Copy the URL from the address bar.

How to Embed Facebook Status Posts in WordPress

How to Embed Facebook Status Posts in WordPress

3. Paste the URL inside you post editor

How to Embed Facebook Status Posts in WordPress

How to Embed Facebook Status Posts in WordPress

And that’s it.

Here’s an example of embedded status in WordPress blog post,

https://www.facebook.com/coffeecupweb/posts/1596784437264447

How To Get An App ID and Secret Key From Facebook

In this tutorial, we are going to create a new Facebook App in order to generate a Facebook App ID and Secret Key. To create a new Facebook App follow the steps given below,

Quick Tip: Changing the Laravel Redirect Location After Login

1. Register A New App:

To start the process Click Here. This will take you to the Facebook’s Developer page. You will be asked to login with your Facebook Account.

Once you’re logged in, you will see a webpage similar to one below,

create-facebook-app-step-01

To create a new app click on Add a New App button.

2. Select Platform For Your App:

After you click the Add a New App button, you will be asked to select a new platform for your app. You need to select website.

create-facebook-app-step-02

 

 

3. Create a New App ID:

After select the app platform, you will see the following page. Click on Skip and Create App ID link.

create-facebook-app-step-03

 

 

Then it will ask you for Display Name and select a category ( Recommended : “Apps for Pages” ) .

create-facebook-app-step-04

 

After you’re done with the requested fields, click on Create App ID button. This will task you to the app dashboard.

4. Retrieve the App ID and Secret Key:

create-facebook-app-step-05

 

On the dashboard, you are able to access the app ID and app secret, click on the show button to display app secret.

How to Get Facebook Like Count With PHP? (Updated)

This is an updated and working code snippet which can be used to retrieve Facebook like count of a specific page. To use this code snippet you’ll need a Facebook App ID and Secret Key.

In order to get an App ID and Secret Key, you have to create a new Facebook application.

Quick Tip : Extract the archive contents with PHP

Here’s a Quick Tutorial Which help you with that,

http://coffeecupweb.com/how-to-get-an-app-id-and-secret-key-from-facebook/

To get Facebook page like count use the following code.

//Get Facebook Likes Count of a page
function fbLikeCount($id,$appid,$appsecret){
	//Construct a Facebook URL
	$json_url ='https://graph.facebook.com/'.$id.'?access_token='.$appid.'|'.$appsecret;
	$json = file_get_contents($json_url);
	$json_output = json_decode($json);
 
	//Extract the likes count from the JSON object
	if($json_output->likes){
		return $likes = $json_output->likes;
	}else{
		return 0;
	}
}
//This Will return like count of CoffeeCupWeb Facebook page
echo fbLikeCount('coffeecupweb','__my_app_id__','__my_secret_key__');

To extract the like count you need to page id or name, app id and secret key.

Facebook Graph API 2.4 Update:

The above code is working for apps below 2.4 version, but for apps above 2.4 you need to use following code.

//Get Facebook Likes Count of a page
function fbLikeCount($id,$appid,$appsecret){
	//Construct a Facebook URL
	$json_url ='https://graph.facebook.com/'.$id.'?access_token='.$appid.'|'.$appsecret.'&fields=likes';
	$json = file_get_contents($json_url);
	$json_output = json_decode($json);

	//Extract the likes count from the JSON object
	if($json_output->likes){
		return $likes = $json_output->likes;
	}else{
		return 0;
	}
}
//This Will return like count of CoffeeCupWeb Facebook page
echo fbLikeCount('coffeecupweb','__my_app_id__','__my_app_secret__');

As you can see, with Graph API 2.4 we need to pass name of fields we need to access. Apart from likes you can also access,

  1. id
  2. about
  3. birthday
  4. category
  5. likes
  6. link
  7. name
  8. talking_about_count
  9. username
  10. website
  11. were_here_count

How to get Facebook likes count of a page using Graph API

Due to change in Facebook API this code snippet doesn’t work any longer. Click Here for solution.

Facebook’s graph API is the simplest way to get the like count of a Facebook page. Using the following code snippet you can grab the like count of a particular Facebook page and display it on your website.

All you need to do is pass Username or id of the page,

//Get Facebook Likes Count of a page
function fbLikeCount($id){
	//Construct a Facebook URL
	$json_url ='https://graph.facebook.com/'.$id.'';
	$json = file_get_contents($json_url);
	$json_output = json_decode($json);
 
	//Extract the likes count from the JSON object
	if($json_output->likes){
		return $likes = $json_output->likes;
	}else{
		return 0;
	}
}
//This Will return like count of CoffeeCupWeb's Facebook page
echo fbLikeCount('coffeecupweb');

Useful Read: How to Add the Facebook Like Box / Fan Box in WordPress

How to add a Facebook Like Box to your WordPress blog without a plugin

Facebook Like box is a widget provided by Facebook which allows you to attract and increase more Page likes from your own website. In this tutorial, I will show you ho you can add a Like Box to your WordPress blog or Website without using any plugin.

Adding a Facebook Like Box is quite simple:

Step 1: Go to Facebook Developer’s Like Box Page

Step 2: Type the URL of your Facebook page inside “Facebook Page URL” input box as show in the figure.

how-to-add-a-facebook-like-box-to-your-wordpress-blog-without-a-plugin-step-1

Step 3: Click on Get Code button, select the IFRAME tab and copy the code

Step 4: Now, Login to you WordPress dashboard, go to Appearance » Widgets and drag and drop the Text Widget into your WordPress sidebar.

Step 5: Paste the IFRAME code inside your Text Widget and click on Save Changes button. That’s it.

Note:The Facebook Like Box feature is designed only for Facebook Pages. It will not work with personal profile.