December 2, 2015 12:21 pm

How to Login with Facebook API SDK v5 in PHP

Previously we wrote many article on Facebook login and now they are not working due to updates in Facebook API so I am writing on latest SDK v5 how to login with Facebook using PHP SDK v5 which is the latest version and I suggest you all to update your old Facebook login integration to the latest version.How to Login with Facebook API SDK v5 in PHP

Let’s Start

Make New Application on Facebook:

1. Visit https://developers.facebook.com/apps and click + Create New App.

create facebook application

Click on website.

create facebook application name

Add application name and click Create New Facebook App ID.

create facebook application select category

Select category and click on Create App ID. On next window click Skip Quick Start.

It will take you to the app dashboard now select settings and click on +Add Platform. Select website in add platform popup.

create facebook application select platform

Add your website link and fill form and save like below image.

create facebook application form saved

By default your app enabled for sandbox mode you need to change it to live click on Status & Review.

create facebook application make app live

That’s it App settings done now come to the coding.

First of all download SDK from here or you can download our demo code which contain SDK as well.

Now create a file index.php and add a facebook login button link to fblogin.php page and create a callback.php page to show information after authenticate.

index.php

It will show a login with Facebook image.

fblogin.php

This page verify your Facebook application credentials and redirect you to facebook.com for login. You need to change your application id and application secret.

Once you allow all permission it will redirect to callback.php page and show your user’s data.

callback.php

That’s it for codding end now you can integrate it and run latest version of Facebook login.

Note: This version of the Facebook SDK for PHP requires PHP 5.5 or greater.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:
  • majeed

    hello my dear .. this not working ..error message.. Invalid App ID: {455718634633119}
    please help me

    • please remove { } brackets not required.

  • Sanjay Kumar Singh

    It is giving….
    Facebook SDK returned an error: Connection timed out after 10001 milliseconds

    • share link here please.

      • Sanjay Kumar Singh

        Thankx , finally i solved the error .

        • saurabh

          facing same error

        • Koushik Dutta

          how???pls help me

  • Dhanashri Bhirud-Fegde

    hello, its not working. error message Warning: Unexpected character in input: ” (ASCII=92) state=1 and Parse error: syntax error, unexpected T_STRING in
    plz help

    • Its in your php code error find that slash and remove it.

      • Dhanashri Bhirud-Fegde

        Its not my php code error. Error in fblogin.php page on line no 5.
        Plz help

      • Dhanashri Bhirud-Fegde

        Its not my php code error. Error in fblogin.php page on line no 5.
        Plz help

      • Dhanashri Bhirud-Fegde

        Its not my php code error. Error in fblogin.php page on line no 5.
        Plz help

      • Dhanashri Bhirud-Fegde

        Its not my php code error. Error in fblogin.php page on line no 5.
        Plz help

      • Dhanashri Bhirud-Fegde

        Its not my php code error. Error in fblogin.php page on line no 5.
        Plz help

      • Dhanashri Bhirud-Fegde

        Its not my php code error. Error in fblogin.php page on line no 5.
        Plz help

      • Dhanashri Bhirud-Fegde

        Its not my php code error. Error in fblogin.php page on line no 5.
        Plz help

      • Dhanashri Bhirud-Fegde

        Its not my php code error. Error in fblogin.php page on line no 5.
        Plz help

      • Dhanashri Bhirud-Fegde

        Its not my php code error. Error in fblogin.php page on line no 5.
        Plz help

      • Dhanashri Bhirud-Fegde

        Its not my php code error. Error in fblogin.php page on line no 5.
        Plz help

  • Gagandeep Sharma

    Hello, everytime i am getting error “Facebook SDK returned an error: Cross-site request forgery validation failed. Required param “state” missing.” but State is available in the get URL parameters while callback.

    • Gagandeep Sharma

      @huzoorbux:disqus

      Can you please suggest some solution?

      • please share code.

        • Gagandeep Sharma

          @huzoorbux:disqus … Code is exactly same as written here in tutorial. Not even one line is changed.

          • Just downloaded code and execute it its working perfect..

          • Gagandeep Sharma

            is it something to do with my App settings then ?? i have two links on same page to register and login and both are pointing to login facebook file.. Do you think that can create issues?

          • only follow instruction given in above tutorial

          • Gagandeep Sharma

            Followed same and one thing more that i am using without .php extension.. Will that be a problem ?? and how can i share the code with you..

          • Lucas De Oliveira Richter

            hi, i use this php with modifications like change to v2.7 and have same problem,
            i can log 20 30 times with no problem but sometimes show this:
            Facebook SDK returned an error: Cross-site request forgery validation failed. The “state” param from the URL and session do not match.

          • Lucas De Oliveira Richter

            on same pc, and user i try 30 log and logout: 29 ok 1 this problem

  • Luca Olivieri

    good Work thanks…. i have a question? can i ask permission and get account phone number ? or phone number it’s only for page informations?

  • Luca Olivieri

    good Work thanks…. i have a question? can i ask permission and get account phone number ? or phone number it’s only for page informations?

  • Luca Olivieri

    good Work thanks…. i have a question? can i ask permission and get account phone number ? or phone number it’s only for page informations?

  • Luca Olivieri

    good Work thanks…. i have a question? can i ask permission and get account phone number ? or phone number it’s only for page informations?

  • Gagandeep Sharma

    @huzoorbux:disqus
    I am getting

    Call to a member function getValue() on a non-object on $response = $fb->get(‘/me?fields=id,name,email,first_name,last_name’, $accessToken->getValue());

  • @huzoorbux:disqus
    Downloaded file not working. I haven’t replaced anything.

    Facebook SDK returned an error: couldn’t connect to host

    • Your server probably blocks outgoing CURL requests, contact your hosting provider.

  • Toqeer Khan

    Great work i downloaded That its work perfect Thanks …….

  • Manzoor Mustafa

    It is working perfectly. I can bet that there can’t be such beautiful tutorial any where else. Really it is awesome

  • Manzoor Mustafa

    It is working perfectly. I can bet that there can’t be such beautiful tutorial any where else. Really it is awesome

  • Manzoor Mustafa

    It is working perfectly. I can bet that there can’t be such beautiful tutorial any where else. Really it is awesome

  • Manzoor Mustafa

    It is working perfectly. I can bet that there can’t be such beautiful tutorial any where else. Really it is awesome

  • Gautam

    Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in D:wampwwwDemosfacebookLoginDemofblogin.php on line 4

  • Vishal Kumar

    Facebook SDK returned an error: Connection timed out after 10001 milliseconds

  • Soniya Kukreja

    nice tutorial

  • Soniya Kukreja

    nice!

  • Darío

    help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5

    • Svetlana Antonova

      This error appears because your PHP version is lower than 5.4. Facebook config array is written with square brackets. If you can’t upgrade PHP version solution I used is to replace “[xxxxx => yyyy]” array definition with “array(xxxx=>yyyy)” in your files as well as Facebook PHP SDK files.
      Facebook SDK 5.0 requires min PHP 5.4

  • Darío

    help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5

  • Darío

    help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5

  • Darío

    help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5

  • Darío

    help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5

  • Darío

    help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5

  • Renata

    Is it possible to capture my posts and hometown/location ?

    If I increment the code as:
    get(‘/me?fields=id,name,email,first_name,last_name,posts,location,hometown’)

    and :
    echo “Posts: “.$me->getProperty(‘posts’).””;

    Is it possible? Because I increment this and it did not work.

    Thanks!!

  • saurabh

    Facebook SDK returned an error: Connection timed out after 10001 milliseconds

    • Make sure that you are using php 5.5 or greater.

  • saurabh

    link -http://haridwaryatra.com/fb/

    Getting Error – Facebook SDK returned an error: Connection timed out after 10001 milliseconds

  • baljit

    Hello

    Hope you are doing great

    we are using facebook APi when i have click facebook login then redirect to my site its display error message “An Internal Error Has Occurred
    Error: An Internal Error Has Occurred.” and not $accessToken = $helper->getAccessToken(); print access token

    please let us know how can do it in cakephp 3.1

  • Koushik Dutta

    Hello sir,
    This bunch of code is not working it will show a error -> ” Facebook SDK returned an error: Connection timed out after 10001 milliseconds

    • Run this code on your server

      <?php
      echo file_get_contents(“https://graph.facebook.com”);
      ?>

      Show me output.

      • Koushik Dutta

        Sir
        this is the error that i am getting…

        • Your hosting block graph.facebook.com contact hosting provider.

      • Koushik Dutta

        sir this the eroor tht i have getting

  • Koushik Dutta

    And sir I am sure about my Php version is grater than 5.5..

    • Run this code on your server

      Show me outpu6t.

      • Koushik Dutta

        sir I am running this codes in my server.but i got the same error…pls help me

        • Your hosting block graph.facebook.com contact hosting provider.

        • Koushik Dutta

          Sir I will send screen shot of error..pls See

  • Koushik Dutta

    OK sir

  • clipfever

    How to get Profile url (Ex. fb.com/your_profile_user_name)

  • Naveen Yadav

    when I run this script code I got this error “Warning: Unexpected character in input: ” (ASCII=92) state=1…line no. 5″ there is “$fb = new FacebookFacebook([” where I mistake??
    also same line second error this
    “Parse error: syntax error, unexpected T_STRING” line no. 5

    • What PHP version you have installed?

      • Naveen Yadav

        PHP Version 5.2.17

        • PHP 5.5 or greater reqired.

          • Naveen Yadav

            is there any thing which I can do in 5.2.17 version

          • No new api sdk require phpp 5.6 minimum

  • Anil Singh

    Thanks for the great post.
    I am stuck as some issue here about how to pass the appsecret_proof param to the fb request. I have added a question with complete details here as well – http://stackoverflow.com/questions/40500252/passing-appsecret-proof-in-fb-request
    Can someone help me figure it out. Thanks.

  • Mary Smith

    Thanks for the great post. Have you developed code to post message, image on FB page using V5 sdk? If yes please share

  • Mary Smith

    Thanks for the great post. Have you developed code to post message, image on FB page using V5 sdk? If yes please share

  • Mary Smith

    Thanks for the great post. Have you developed code to post message, image on FB page using V5 sdk? If yes please share

  • Christopher Grigg

    Thanks that’s great, FB doco’s are not good!

  • Koushik Dutta

    Hello Sir,
    I wanna make a image gallery by using Facebook uploaded pictures.Sir would u like to help me out? please

  • Varun Nayyar

    Nice tutorial