3
我有兩個php文件:facebook.php(用於檢索用戶數據)和MyClass.php以提供restfull服務。 facebook.php是在另一個類中包含一個包含use命令的類
<?php
require_once('../libs/facebook/src/Facebook/HttpClients/FacebookHttpable.php');
require_once('../libs/facebook/src/Facebook/HttpClients/FacebookCurl.php');
require_once('../libs/facebook/src/Facebook/HttpClients/FacebookCurlHttpClient.php');
require_once('../libs/facebook/src/Facebook/Entities/AccessToken.php');
require_once('../libs/facebook/src/Facebook/Entities/SignedRequest.php');
require_once('../libs/facebook/src/Facebook/FacebookSession.php');
require_once('../libs/facebook/src/Facebook/FacebookRedirectLoginHelper.php');
require_once('../libs/facebook/src/Facebook/FacebookRequest.php');
require_once('../libs/facebook/src/Facebook/FacebookResponse.php');
require_once('../libs/facebook/src/Facebook/FacebookSDKException.php');
require_once('../libs/facebook/src/Facebook/FacebookRequestException.php');
require_once('../libs/facebook/src/Facebook/FacebookOtherException.php');
require_once('../libs/facebook/src/Facebook/FacebookAuthorizationException.php');
require_once('../libs/facebook/src/Facebook/GraphObject.php');
require_once('../libs/facebook/src/Facebook/GraphSessionInfo.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;
function getUserData($token){
// init app with app id and secret
FacebookSession::setDefaultApplication(FB_APP_ID,FB_APP_SECRET);
// If you already have a valid access token:
$session = new FacebookSession($token); // 'access-token'
/*
// If you're making app-level requests:
$session = FacebookSession::newAppSession();
*/
// To validate the session:
try {
$session->validate();
} catch (FacebookRequestException $ex) {
// Session not valid, Graph API returned an exception with the reason.
echo $ex->getMessage();
return false;
} catch (\Exception $ex) {
// Graph API returned info, but it may mismatch the current app or have expired.
echo $ex->getMessage();
return false;
}
if($session) {
try {
$user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject()->asArray(); //(GraphUser::className());
$id = $user_profile['id'];
$user_profile['image'] = 'https://graph.facebook.com/'.$id.'/picture?width=200';
// print profile data
echo '<pre>' . print_r($user_profile, 1) . '</pre>';
return $user_profile;
// print logout url using session and redirect_uri (logout.php page should destroy the session)
//echo '<a href="' . $helper->getLogoutUrl($session, 'http://yourwebsite.com/app/logout.php') . '">Logout</a>';
//echo "Name: " . $user_profile->getName();
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
return false;
}
}
}
?>
和MyClass.php是
<?php
/**
* Class to manage user data
*
*
*
*
*/
class DbHandler {
function __construct() {
include_once '../../../include/Facebook.php';
}
/**
* Get user data from facebook
* @param user key
* @return user data if exist else false
*/
public function facebookSignIn($token){
$fbUserData = getUserData($token);
if($fbUserData == false) echo 'no user found';
else print_r($fbUserData);
}
}
?>
線(include_once '../../../include/Facebook.php')是正確的,因爲 如果我註釋掉這些代碼行的腳本工作
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;
可能是我不明白的命令「使用」 問題出在哪裏? 感謝您的關注。