我正在更新我的应用程序以使用新的PHP SDK,但我遇到了这个问题: 我想在一个单独的PHP文件中做Facebook的东西,以保持干净。 例如,这是我的fb.php页面:
<?php
require_once('sdk/autoload.php');
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication(...,...);
// 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;
}
}
// 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() );
//logged in
} else {
//user is not logged in
?>
<script>window.location="...link to login page...";</script>
<?php
exit;
}
使用Facebook会话的页面可能如下所示(testing.php):
<?php
require_once('fb.php');
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();
echo "Welcome, ".$graphObject['first_name'];
返回此错误:
致命错误:未找到“FacebookRequest”类
当我将use Facebook\FacebookRequest;
放在testing.php中时,代码工作正常,但我不想这样做,因为这是将所有Facebook会话内容放在单独的fb.php文件中的重点。 ..
这是什么原因,是否有解决方法如何将我的所有Facebook内容保存在单独的文件中?
谢谢!