如何在PHP中将值传递给FacebookRedirectLoginHelper

jas*_*son 5 php facebook-php-sdk

我使用Facebook PHP SDK并尝试将令牌ID从一个页面发送到另一个页面.我使用此代码 - http://www.krizna.com/demo/login-with-facebook-using-php/.我用

$helper = new FacebookRedirectLoginHelper('http://www.krizna.com/fbconfig.php' );

我试过像这样发送价值:

$helper = new FacebookRedirectLoginHelper('http://www.krizna.com/fbconfig.php?value='.$value );  
Run Code Online (Sandbox Code Playgroud)

但是fbconfig.php当我尝试时,我没有获得文件中的价值:

$value = $_GET['value'];
Run Code Online (Sandbox Code Playgroud)

我也使用会话发送值但它不起作用.如何向FacebookRedirectLoginHelper(fbconfig.php)发送值?

fbconfig.php

<?php

session_start();

$value = $_GET['value_new'];

$fb = new \Facebook\Facebook([
    'app_id' => $config->facebook->app_id,
    'app_secret' => $config->facebook->app_secret
]);

$helper = $fb->getRedirectLoginHelper();
try {
    if ($access_token = $helper->getAccessToken()) {

        try {
            // Returns a `Facebook\FacebookResponse` object with the requested fields
            $response = $fb->get('/me?fields=name,id,email,picture', $access_token);
            $user = $response->getGraphUser();
            $fbid = $user->getId();            // To Get Facebook ID
            $fbfullname = $user->getName();    // To Get Facebook full name
            $femail = $graphObject->getEmail();// To Get Facebook email ID
            $_SESSION['FBID'] = $fbid;
            $_SESSION['FULLNAME'] = $fbfullname;
            $_SESSION['EMAIL'] = $femail;
            //Then do whatever you want with that data

            $value = $_SESSION['value'];

            header("Location: index.php?value_new=$value");

            } catch (\Facebook\Exceptions\FacebookResponseException $e) {
            error_log('Graph returned an error: ' . $e->getMessage());
        } catch (\Facebook\Exceptions\FacebookSDKException $e) {
            error_log('Facebook SDK returned an error: ' . $e->getMessage());
        }
    }
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    error_log('Graph returned an error: ' . $e->getMessage());
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    error_log('Facebook SDK returned an error: ' . $e->getMessage());
}
Run Code Online (Sandbox Code Playgroud)

的index.php

<?php if (isset($_SESSION['FBID'])): ?><!--  After user login  -->
<div class="container">
    <h1>value <?php $value_new = $_GET['value_new']; echo $value_new; ?></h1>
</div>
<? endif ?>
Run Code Online (Sandbox Code Playgroud)

Web*_*eng 2

您不应该尝试通过查询字符串/$_GET[]超全局传递 facebook 访问令牌。你应该使用facebook推荐的,也就是superglobal $_SESSION[]

由于您尝试将访问令牌从一个页面传递到另一个页面,因此我们在第一页上调用访问令牌$access_token。要将其传递到另一个页面,请执行以下操作:

第 1 页:

<?php 
  session_start(); //the very top of your document
  // ... other code ...
  $_SESSION['access_token'] = $access_token;
  // ... other code ...
?>
Run Code Online (Sandbox Code Playgroud)

第2页:

<?php 
  session_start(); //the very top of your document
  // ... other code ...
  $access_token = $_SESSION['access_token'];
  // ... other code ...
?>
Run Code Online (Sandbox Code Playgroud)

这应该有效,请告诉我它是否适合您。