Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Facebook SDK v5 for PHP with Phalcon

Hello community,

I have to integrate Facebook login in to my project. Im doing an REST Api on backend and looking for "tips" how to do it correctly .. I have found docs on FB developer page, but would be great if some1 allready did this integration to give some tips...

Thanks



60.9k
Accepted
answer
edited Feb '16

Hi, i had to do this few weeks ago and I used this:

https://github.com/thephpleague/oauth2-facebook

total time to make it work ( without facebook app registration crap ) : 20 mins

here are my functions:

Lets create a static function in our Utils class that will generate the return URL.

public static function getFacebookLoginUrl(){

            $provider = \Phalcon\Di::getDefault()->get('facebook');

            $authUrl = $provider->getAuthorizationUrl([
                'scope' => ['email', 'public_profile']
            ]);

            \Phalcon\Di::getDefault()->get('session')->set('oauth2state',  $provider->getState());

            return $authUrl;
        }

Lets register the service that will contain our app data: ( in your boostrap.php or so )

        $this->di->setShared('facebook', function() {
            return new \League\OAuth2\Client\Provider\Facebook([
                'clientId'          => 'xxxxxx',
                'clientSecret'      => 'xxxxxx',
                'redirectUri'       => 'http://xxxxxxt/users/facebook/auth',
                'graphApiVersion'   => 'v2.5',
            ]);
        });

and this is my contoller that recieves the ruturn URL... ( basiccaly after the user enters his creadentials in facebook and agree with terms etc...

        class FacebookController extends Controller {

    public function authAction(){

        if (isset($_GET["error_code"]) && $_GET["error_code"] != ""){
            $this->view->pick('login');
        } else {
            $provider = $this->di->get('facebook');

            $token = $provider->getAccessToken('authorization_code', [
                'code' => $_GET['code']
            ]);

            $user = $provider->getResourceOwner($token);

            $provider->getLongLivedAccessToken($token);

            $this->di->get('auth')->authenticateOrCreateFacebookUser($user->toArray());
        }
    }
}

and finally lets put the button:

<a href="<?php echo \CommonClasses\Utils\Main::getFacebookLoginUrl(); ?>" class="fa fa-3x fa-facebook-square"></a>

//this calls the first function that i wrote



8.6k
edited Feb '16

Hey man, thx for helping ... So you made a service with second block of code from abovet? Right ? .. Third one goes in FacebookController..



60.9k
edited Feb '16

i edited my post. Let me know if you need more info



17.3k

Hello,

where can i implement the static function? Don't understand what "our Utils class" is :-)

I am using Vokuro.

Rgds Stefan



60.9k
$loader->register("myapp\Utils", "app/Utils");

//file app/utils/Utils.php

class Utils .....

simple put the class wherever you want, or add it in controller. Donest really matters