0
votes

I'm currently trying to integrate the lastest Facebook php sdk into a Phalcon project but I'm not having much luck.

I can get the SDK to work in a standalone project but the exact same code fails when integrated into a Phalcon project (either as a service or directly in a Controller).

The issue seems to be that the facebook redirect helper creates a "state" property which is appended to a loginUrl and then stored in a session. When a user is redirected back to my site after signing in, it checks this property against a querystring value. The state property is only generated and stored whenever you display the login url via the redirectHelpers getLoginUrl() method. Somehow, when I integrate this in Phalcon the session variable and the $_GET parameter never seem to match up. The simple example which works is as follows

 // lots of requires
 Facebook\FacebookSession::setDefaultApplication($appId,$secret);
 $helper = new Facebook\FacebookRedirectLoginHelper('http://'.$_SERVER['HTTP_HOST'] .'/');
 // see if a existing session exists
 if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
  // create new session from saved access_token
      $session = new FacebookSession( $_SESSION['fb_token'] );

  // validate the access_token to make sure it's still valid
  try {
         if ( !$session->validate() ) {
         $session = null;
   }
  } catch ( Exception $e ) {
    // catch any exceptions
        $session = null;
    }
   }  // end if isset($_SESSION)

 if ( !isset( $session ) || $session === null ) {
  // no session exists

  try {
          $session = $helper->getSessionFromRedirect();
   } catch( FacebookRequestException $ex ) {
    // When Facebook returns an error
     // handle this better in production code
      print_r( $ex );
    } catch( Exception $ex ) {
    // When validation fails or other local issues
     // handle this better in production code
       print_r( $ex );
          }
       }

   // see if we have a session
   if ( isset( $session ) ) {

    // save the session
     $_SESSION['fb_token'] = $session->getToken();
     // create a session using saved token or the new one we generated at login
       $session = new FacebookSession( $session->getToken() );

        // graph api request for user data
        $request = new FacebookRequest( $session, 'GET', '/me' );
         $response = $request->execute();
          // get response
        $graphObject = $response->getGraphObject()->asArray();

         // print profile data
            echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';

              // print logout url using session and redirect_uri (logout.php page should destroy the session)
               echo '<a href="' . $helper->getLogoutUrl( $session, 'http://yourwebsite.com/app/logout.php' ) . '">Logout</a>';
      } else {
    // show login url
      echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends' ) ) . '">Login</a>'; // this line would generate a new state
     }

When I try using this exact same code in a controller in a phalcon project (or by setting "$me" up in the $di), the state check always fails even though I'm not generating a new login url. The only other difference is that in the simple project I require all the facebook files using require_once but in the Phalcon project I use

$loader->registerNamespaces(
    array(
      "Facebook"    => __DIR__ . '/../../vendor/facebook/php-sdk-v4/src/Facebook/'
     )
  );

but replacing that with the requires doesn't seem to have an effect.

Anyone got any clues?

2

2 Answers

0
votes

I've got it working by using registerClasses not registerNamespaces - but using v3.2.x so not a v4.

$loader->regeisterNamespaces( /* Projects' classes */ )
    ->registerClasses(array(
        'Facebook' => __DIR__ . '/../../vendor/facebook/php-sdk/src/facebook.php'
    ))
    ->register();

ps.: Im using composer to load FacebookSDK so I have different path than yours.

Then I'm using it in Controller ordinary without using $di,

protected function getFacebook()
{
    if (!$this->facebook) {
        $this->facebook = new Facebook(array(
            'appId' => $this->config->social->facebook->appId,
            'secret' => $this->config->social->facebook->secret,
            'fileUpload' => false,
            'allowSignedRequest' => false,
        ));
    }
    return $this->facebook;
}
0
votes

I've worked this one out. The issue was that the browser was automatically making a request to /favicon.ico as well,as I didn't have a favicon.ico this then rendered the default indexAction again and as such this was causing the getLoginUrl() method to fire again generating a new state. The simple fix is to just create a favicon, or define the error handling route for files not there (I was just using the boilerplate from the phalcon dev tools initially)