Facebook Login: How to combine JavaScript with PHP SDK?

后端 未结 5 1514
温柔的废话
温柔的废话 2020-12-08 15:04

How do I go about combining examples from both

https://github.com/facebook/php-sdk/blob/master/examples/example.php and https://github.com/facebook/connect-js/blob/m

相关标签:
5条回答
  • 2020-12-08 15:52

    I just use the Facebook login button - https://developers.facebook.com/docs/reference/plugins/login/

    Once the user has used that to login (if they weren't already) and grant access to your app (site), the PHP-SDK should have no trouble using the Facebook session and APIs.

    Update

    I just tried this with the new version 3.0 PHP SDK and it is definitely not working. Found this on the developer blog

    If you are using the JavaScript SDK for login in conjunction with the PHP SDK, you will want to wait for the JavaScript SDK upgrade (coming in 4 weeks). Version 3.0.0 of the PHP SDK won’t cooperate with the current JavaScript SDK due to the cookie format changing.

    In other words, use the v2 PHP SDK until the JS SDK is updated.

    0 讨论(0)
  • 2020-12-08 15:59

    Not sure if you got this working. The current JS API has a configuration to save the cookie so the server may read it:

    FB.init({
        appId  : 'YOUR APP ID',
        cookie : true
    });
    

    Now, in PHP, you can do the following to use the session from the JS:

    // Read the cookie created by the JS API
    $cookie = preg_replace("/^\"|\"$/i", "", $_COOKIE['fbs_' . FB_APP_ID]);
    parse_str($cookie, $data);
    
    // Startup the Facebook object
    $fb = new Facebook(array(
        'appId'  => FB_APP_ID,
        'secret' => FB_APP_SECRET
    ));
    
    // Say we are using the token from the JS
    $fb->setAccessToken($data['access_token']);
    
    // It should work now
    var_dump($fb->getUser());
    

    Hope it helped.

    0 讨论(0)
  • 2020-12-08 16:00

    After long hours of searching, I found this fixed my problem where the PHP SDK did not pick up the Facebook session. Apparently, the Javascript SDK is not setting the session the right way. Add this to your auth.login eventlistener:

    FB.Event.subscribe('auth.login', function(response) {
        FB._oauth = false;
        FB.Cookie.setEnabled(true);
        FB.Auth.setSession(response.authResponse, response.status);
        FB._oauth = true;
        window.location.reload();
    });
    

    Also, make sure you set the App Domain to match your cookie domain. After fixing these items, it finally worked.

    Edit: This morning, everything was working fine. When I was testing some more just now, I found that I had to edit base_facebook.php to make sure the requests to Facebook would not time-out. My guess is that because I am testing on my local machine, the connection would not be fast enough to make it in time. This explains the strange behaviour where sometimes it would and sometimes it would not work.

    Alter the connecttimeout in the $CURL_OPTS array on line 128:

    public static $CURL_OPTS = array(
        CURLOPT_CONNECTTIMEOUT => 20,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 60,
        CURLOPT_USERAGENT      => 'facebook-php-3.1',
    );
    
    0 讨论(0)
  • 2020-12-08 16:04

    In version 4 of the SDK it works automatically with the JavaScript SDK. You just need to use FacebookJavaScriptLoginHelper:

    $helper = new FacebookJavaScriptLoginHelper(); // Get access token from JS SDK
    try {
        $session = $helper->getSession();
    } catch(FacebookRequestException $e) {
        // When Facebook returns an error
        echo "Exception occured, code: " . $e->getCode();
        echo " with message: " . $e->getMessage();
    } catch(\Exception $e) {
        // When validation fails or other local issues
        echo "Exception occured, code: " . $e->getCode();
        echo " with message: " . $e->getMessage();
    }
    
    // If logged in
    if($session) {
        // Get user profile
        try {
            $user_profile = (new FacebookRequest(
                $session, 'GET', '/me'
            ))->execute()->getGraphObject(GraphUser::className());
    
            print_r($user_profile);
    
        } catch(FacebookRequestException $e) {
    
            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();
        }
    }
    

    No need to mess around with cookies!

    0 讨论(0)
  • 2020-12-08 16:07

    it's sound like facebook didn't fix this problem at the right time(as they talk https://developers.facebook.com/blog/post/503).

    this is newest infomation about js sdk https://developers.facebook.com/blog/post/525/

    0 讨论(0)
提交回复
热议问题