简体   繁体   中英

How to configure php sdk 4 in Yii

I am new in fbdevelopment, so I downloaded php sdk 4 and extated in component with name facebook-sdk and then I configured in main.php as

'import'=>array(
    'application.models.*',
    'application.components.*',
    'application.components.facebook-sdk.*',

Then in my site controller I want to call:

$session = new FacebookSession($_POST['accessToken']); 

But even thought I have an access token, it returns:

include(FacebookSession.php): failed to open stream: No such file or directory 

where we have to configuration php-sdk 4 in yii

I had this problem like you and it took me hours to debug. Finally, I found that I missed the namespace "Facebook\\" before the class name. Here is my code it works well:

        require_once 'facebook-php-sdk/autoload.php';       
        require_once 'facebook-php-sdk/src/Facebook/FacebookSession.php';
        require_once 'facebook-php-sdk/src/Facebook/FacebookRequest.php';
        require_once 'facebook-php-sdk/src/Facebook/GraphObject.php';
        require_once 'facebook-php-sdk/src/Facebook/GraphUser.php';
        require_once 'facebook-php-sdk/src/Facebook/FacebookSDKException.php';
        require_once 'facebook-php-sdk/src/Facebook/FacebookRequestException.php';

        Facebook\FacebookSession::setDefaultApplication(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET);
        $helper = new Facebook\FacebookRedirectLoginHelper('http://mywebsite.com');
        try {
            $session = $helper->getSessionFromRedirect();                   
        } catch(Facebook\FacebookRequestException $ex) {
            // When Facebook returns an error
        } catch(\Exception $ex) {
            // When validation fails or other local issues
        }

        if ( isset($session) ) {
            // Logged in
            $me = (new Facebook\FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
            var_dump($me);
        }
        else {
             $loginUrl = $helper->getLoginUrl();
             header("Location: ".$loginUrl); exit;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM