Asking for permission using new PHP SDK (3.X.X)

后端 未结 2 1191
孤独总比滥情好
孤独总比滥情好 2021-01-31 12:20

How can I ask for permissions using new PHP SDK? I don\'t want to use the graph api and parse the url all the time. When the application is opened it should automatically ask fo

2条回答
  •  面向向阳花
    2021-01-31 12:37

    Here's how i'm doing it with the latest PHP SDK (3.0.1)

    // init new facebook class instance with app info (taken from the DB)
    $facebook = new Facebook(array(
        'appId' => 'YOUR APP ID',
        'secret' => 'YOUR APP SECRET'
    ));
    // get user UID
    $fb_user_id = $facebook->getUser();
    
        // get the url where to redirect the user
    $location = "". $facebook->getLoginUrl(array('scope' => 'publish_stream, email'));
    
    // check if we have valid user
    if ($fb_user_id) {
        try {
            // Proceed knowing you have a logged in user who's authenticated.
            $fb_user_profile = $facebook->api('/me');   
    
        } catch (FacebookApiException $e) {
            $fb_user_id = NULL;
            // seems we don't have enough permissions
            // we use javascript to redirect user instead of header() due to Facebook bug
            print '';
    
            // kill the code so nothing else will happen before user gives us permissions
            die();
        }
    
    } else {
        // seems our user hasn't logged in, redirect him to a FB login page
    
        print '';
    
        // kill the code so nothing else will happen before user gives us permissions
        die();
    }
    
    // at this point we have an logged in user who has given permissions to our APP
    // basic user info can be fetched easily
    print "Welcome to my app". $fb_user_profile['name'];
    

提交回复
热议问题