Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Facebook应用程序-自动登录以便更新状态(php SDK)_Php_Facebook Graph Api_Login - Fatal编程技术网

Facebook应用程序-自动登录以便更新状态(php SDK)

Facebook应用程序-自动登录以便更新状态(php SDK),php,facebook-graph-api,login,Php,Facebook Graph Api,Login,我已经建立了一个应用程序,允许用户在“外部”更新他们的状态。它使用PHPSDK 一旦注册用户最初通过应用程序授权其facebook帐户,是否可以自动登录?我不想让他们每次都登录新的会话。我想,一旦他们第一次通过身份验证,他们总是连接,而不是要求多次登录。可能是将访问令牌存储在数据库中,然后使用它以某种方式进行连接。这可能吗?我注意到其他应用程序也有这样做,但不知道它们是如何做到这一点的,例如ping.fm 下面是我到目前为止所做的,但这需要用户在会话到期时选择“登录”链接 //instantia

我已经建立了一个应用程序,允许用户在“外部”更新他们的状态。它使用PHPSDK

一旦注册用户最初通过应用程序授权其facebook帐户,是否可以自动登录?我不想让他们每次都登录新的会话。我想,一旦他们第一次通过身份验证,他们总是连接,而不是要求多次登录。可能是将访问令牌存储在数据库中,然后使用它以某种方式进行连接。这可能吗?我注意到其他应用程序也有这样做,但不知道它们是如何做到这一点的,例如ping.fm

下面是我到目前为止所做的,但这需要用户在会话到期时选择“登录”链接

//instantiate the Facebook library with the APP ID and APP SECRET
$facebook = new Facebook(array(
    'appId' => 'appid',
    'secret' => 'secret',
    'cookie' => true
));

//Get the FB UID of the currently logged in user
$user = $facebook->getUser();

//if the user has already allowed the application, you'll be able to get his/her FB UID
if($user) {
    //start the session if needed
    if( session_id() ) {

    } else {
        session_start();
    }

    //do stuff when already logged in

    //get the user's access token
    $access_token = $facebook->getAccessToken();
    //check permissions list
    $permissions_list = $facebook->api(
        '/me/permissions',
        'GET',
        array(
            'access_token' => $access_token
        )
    );

    //check if the permissions we need have been allowed by the user
    //if not then redirect them again to facebook's permissions page
    $permissions_needed = array('publish_stream', 'read_stream', 'offline_access');
    foreach($permissions_needed as $perm) {
        if( !isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1 ) {
            $login_url_params = array(
                'scope' => 'publish_stream,read_stream,offline_access',
                'fbconnect' =>  1,
                'display'   =>  "page",
                'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
            );
            $login_url = $facebook->getLoginUrl($login_url_params);
            header("Location: {$login_url}");
            exit();
        }
    }

    //if the user has allowed all the permissions we need,
    //get the information about the pages that he or she managers
    $accounts = $facebook->api(
        '/me',
        'GET',
        array(
            'access_token' => $access_token
        )
    );

    //save the information inside the session
    $_SESSION['access_token'] = $access_token;
    $_SESSION['accounts'] = $accounts['data'];

    $facebookAuth = TRUE;
} else {
    //if not, let's redirect to the ALLOW page so we can get access
    //Create a login URL using the Facebook library's getLoginUrl() method
    $login_url_params = array(
        'scope' => 'publish_stream,read_stream,offline_access',
        'fbconnect' =>  1,
        'display'   =>  "page",
        'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
    );
    $login_url = $facebook->getLoginUrl($login_url_params);

    //redirect to the login URL on facebook
    $facebook_login = $login_url;
}

if (isset($_GET['submit'])){

    if ($_GET['facebook']){

            //get the info from the form
            $parameters = array(
                'message' => $_GET['message']/*,
                'picture' => $_POST['picture'],
                'link' => $_POST['link'],
                'name' => $_POST['name'],
                'caption' => $_POST['caption'],
                'description' => $_POST['description']*/
            );

            //add the access token to it
            $parameters['access_token'] = $_SESSION['active']['access_token'];

            //build and call our Graph API request
            $newpost = $facebook->api(
                '/me/feed',
                'POST',
                $parameters
            );

            if ($newpost){
                echo 'posted to facebook';  
            } else {
                echo 'not posted to facebook :0(  ';    
            }
    }

我没有检查ping.fm是如何实现的。
但在查看此处的扩展权限列表后:

xmpp_登录(提供与Facebook聊天功能集成的应用程序,允许用户登录。)
否则,我认为这是不可能的。
即使是桌面应用程序也需要将用户重定向到浏览器进行身份验证。
这也可能取决于用户是否在登录时标记了“让我登录”复选框。
由于您具有脱机访问权限,因此无论用户是否登录,您基本上都可以执行这些操作