简体   繁体   中英

Facebook PHP API/App: iFrame

I have a simple App that used to work in the old API. It's an iFrame app and I can't get the user session with the new API no matter what I try. Here is the code I have now:

<?php
error_reporting(E_ALL);

require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'xxxx',
  'secret' => 'xxxx',
  'cookie' => true,
));

$session = $facebook->getSession();
if ( !$session )
{
 //echo "Failed. No session.<br />";

    $url = $facebook->getLoginUrl(array(
               'canvas' => 1,
               'fbconnect' => 0,
               'next'=>'http://www.facebook.com',
               'cancel_url' => 'http://www.facebook.com'
           ));

    //echo "<script type='text/javascript'>top.location.href = '$url';</script>";
    echo "<a href='".$url."' target='_blank'>First time using this App? Click here to connect it to your Facebook.</a>";

}
else {

    try {

        $uid = $facebook->getUser();
        $me = $facebook->api('/me');

        $updated = date("l, F j, Y", strtotime($me['updated_time']));

        echo "Hello " . $me['name'] . "<br />";
        echo "You last updated your profile on " . $updated;

    } catch (FacebookApiException $e) {

        echo "Error:" . print_r($e, true);

    }
}

No matter what I try, $session is never returned (after clicking the link). I am using the correct appID/secret/facebook.php but it always falls into the !$session if path. What am I doing wrong?

$facebook->getLogoutUrl();

This might help

https://github.com/facebook/php-sdk/tree/v3.1.1

The version of the PHP API that I am using that I downloaded a few days ago does not have $facebook->getSession()

So depending on the version you are using (assuming older versions had a getSession, I dont know I just started using the API) it may not have getSession anymore.

What the example that came with the api showed was using ->getUser to attempt to get a user object and checking if that was valid, then attempting to get the users profile via information to verify the connection is valid.

Hope that helps.

Jason Penick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM