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

前端 未结 3 1475
感情败类
感情败类 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:36

    You need to ask for extended permissions when the user authorizes your application, so you can have access to the user email.

    Here's an example using the new PHP SDK and the Graph API:

     APP_ID,
                'secret' => APP_SECRET));
    
    $user = $facebook->getUser();
    $session = $facebook->getSession();
    
    // in case we don't have a valid session, we redirect asking for email extended permissions
    if ($user == null || $session == null) {
      $params = array();
      $params["canvas"] = "1";
      $params["fbconnect"] = "0";
      $params["next"] = CANVAS_URL;
      $params["req_perms"] = "email";
    
      $loginUrl = $facebook->getLoginUrl($params);
    
      echo '';
      exit();
    }
    
    // get user email via the new graph api, using the fql.query method
    $url = "https://api.facebook.com/method/fql.query";
    $url .= "?access_token=" . $session['access_token'];
    $url .= "&query=SELECT email FROM user WHERE uid={$user}";
    $userData = simplexml_load_file($url);
    $userEmail = $userData->user->email;
    
    echo 'The user ID is: ' . $user;
    echo 'The user name is: ';
    echo 'The user email is: ' . $userEmail;
    

提交回复
热议问题