gpt4 book ai didi

php - 如何在 PHP 中将值传递给 FacebookRedirectLoginHelper

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:40:46 25 4
gpt4 key购买 nike

我使用 Facebook PHP SDK 并尝试将 token 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 );  

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

$value = $_GET['value'];

我也使用 session 来发送值,但它不起作用。如何将值发送到 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());
}

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 ?>

最佳答案

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

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

第 1 页:

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

第 2 页:

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

这应该可行,请告诉我它是否适合您。

关于php - 如何在 PHP 中将值传递给 FacebookRedirectLoginHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37005821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com