问题
I am using Yii2 Framework and I have to get Facebook friend list in web-service, for this i am following this steps: FB FriendList
Tried this:
<?php
use yii\Facebook\Facebook; // facebook class
public function getFbUserListFB()
{
$fb = new Facebook([
'app_id' => 'appID',
'app_secret' => 'appSECRET',
'default_graph_version' => 'v2.3'
]);
$response = $fb->get('/me');
print_r($response);
}
?>
but getting this error:
{
"name": "PHP Fatal Error",
"message": "Class 'yii\Facebook\Facebook' not found",
"code": 1,
"type": "yii\base\ErrorException",
"file": "/var/www/html/PROJECT/api/modules/v1/models/FILENAME.php",
"line": 19,
"stack-trace": [
"#0 [internal function]: yii\base\ErrorHandler->handleFatalError()",
"#1 {main}"
]
}
Facebook sdk path is:
/var/www/html/PROJECTNAME/vendor/facebook/php-sdk-v4/src/Facebook/<all-files-and-folder>
回答1:
Hi same error i faced in yii2, i solved like below..,
this code is in view, also no need to use namespace for sdk,
$fb = new \Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v3.0',
'display' => 'popup',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email','manage_pages','instagram_basic','instagram_manage_insights']; // Optional permissions
$loginUrl = $helper->getLoginUrl('https://xxxxxxxxxxxxxxxxxxxx/site/contact/', $permissions);
******login button
<a class="btn btn-sm-connect instagram" href="<?= $loginUrl?>" onClick="MyWindow=window.open('<?= $loginUrl?>','MyWindow',width=600,height=300); return false;>
MyWindow=window.open( --> this for open in a pop up model )
*** controller action code
public function actionContact() {
$fb = new \Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v3.0',
]);
$helper = $fb->getRedirectLoginHelper();
// echo "<pre>";
// print_r($helper); exit;
try {
$accessToken = $helper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (!isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// // Logged in
// echo '<h3>Access Token</h3>';
// var_dump($accessToken->getValue());
$token = $accessToken->getValue();
// echo $token;
//curl request to fb
$pageModel = new Pages();
$followersModel = new Followers();
$curl = new curl\Curl();
$response = $curl->setGetParams([
// 'fields' => 'fan_count,name',
'access_token' => $token
])
->get('https://graph.facebook.com/v3.0/me/accounts');
$pages = json_decode($response);
$pagesList = json_decode(json_encode($pages), True);
$serviceProvider = 'instagram';
.... remaining u can take care of it...
回答2:
In config, add the module:
'modules' => [
'facebook' => [
'class' => '\facebook\Module'
]
],
来源:https://stackoverflow.com/questions/33235223/get-facebook-friend-list-in-yii2-framework