Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

php - Get facebook friend list in yii2 framework

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 yiiFacebookFacebook; // 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 'yiiFacebookFacebook' not found",
  "code": 1,
  "type": "yiiaseErrorException",
  "file": "/var/www/html/PROJECT/api/modules/v1/models/FILENAME.php",
  "line": 19,
  "stack-trace": [
    "#0 [internal function]: yiiaseErrorHandler->handleFatalError()",
    "#1 {main}"
  ]
}

Facebook sdk path is:

/var/www/html/PROJECTNAME/vendor/facebook/php-sdk-v4/src/Facebook/<all-files-and-folder>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 FacebookFacebook([
     '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 FacebookFacebook([
        '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 (FacebookExceptionsFacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch (FacebookExceptionsFacebookSDKException $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() . "
";
            echo "Error Code: " . $helper->getErrorCode() . "
";
            echo "Error Reason: " . $helper->getErrorReason() . "
";
            echo "Error Description: " . $helper->getErrorDescription() . "
";
        } 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 curlCurl();
    $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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...