目前我有两个 php 脚本:index.php 和 upload.php
在我的 index.php 中,我创建了一个会话并保存了访问令牌:
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
在我的另一个脚本中:upload.php 我正在尝试制作一个 facebook api,但我正在努力创建一个会话。我这样做了:
if ( isset( $_SESSION['fb_token'] ) ) {
// create FB session from Access Token
$session = new FacebookSession( $_SESSION['fb_token'] );
// the rest of your code here...
} else {
// redirect back to index for login
header( 'Location: index.php' );
}
但它只是将我重定向回 index.php。我不明白为什么,因为我确实在 index.php 中创建了变量...
我还尝试使用 GET 方法从我的 URL 中获取代码...
$token = $_GET['code'];
$session = new FacebookSession( $token );
但它说变量是未定义的......即使“代码”显示在我的 URL 中......我也做了 isset 但它返回 false ..所以我完全不确定为什么我的变量没有设置。有人可以解释一下并帮助我吗?我是 php 新手,所以整天都在这上面,我敢肯定这可能是一个小问题?我之前从我的另一个简单项目中从 URL 中检索了用户 ID,但从未遇到过这个问题......谢谢