How to get Users Email Id in Facebook application using PHP?

前端 未结 3 1479
感情败类
感情败类 2020-12-28 23:04

I have created a simple Facebook application in PHP, that greets user by there user name, I also want there Email id, to be displayed. But i am not able to do that. the code

3条回答
  •  不思量自难忘°
    2020-12-28 23:49

    Thanks for all the help. I finally figure out how to do that.

    first, I need to get the access token, NOT the user session one, but the one for admin.

    So, here is the code:

    $url = 'https://graph.facebook.com/oauth/access_token';
    $ch = curl_init();
    $params = array(
                    'grant_type'=>'client_credentials',
                    'client_id'=>$appId,
                    'client_secret'=>$secret,
                    );
    
    $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
    // set URL and other appropriate options
    curl_setopt_array($ch, $opts);
    curl_setopt($ch, CURLOPT_URL, "$url");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    // grab URL and pass it to the browser
    $adminAccessToken = curl_exec($ch);
    // close cURL resource, and free up system resources
    curl_close($ch);
    

    And then, I can use the facebook API to get any user who accept the application and the extended permission like that:

    $fql    =   "select name, hometown_location, sex, pic_square, email from user where uid=1000000001";
        $param  =   array(
           'method'     => 'fql.query',
            'query'     => $fql,
            'access_token' =>$adminAccessToken ,
          'callback'    => ''
        );
    
        $fqlResult2   =   $facebook->api($param);
    

    with the access token, i can also post things onto user's wall. :)

提交回复
热议问题