开发者

Getting FaceBook Account Info with PHP After FB Authentication on Website

I'm reading through the Facebook Connect guide and have managed to get the authentication feature running, so that a user can register for a website via their FaceBook accounts. On the registration page, users can enter their FB account info (if they're not alrady signed in) and after confirming the form is posted to a processing page. On the processing page I want to grab their username and e-mail from their FaceBook account and enter it into a MyS开发者_运维百科QL table using PHP, but I'm not sure how to get the info. I tried var_dump($_POST) to see what was being passed, but I don't see the info I need. How can I do this? Thanks.


In the authentication document there's a code sample that will walk you through the whole process and retrieve the user's info at the end:

<?php 

   $app_id = "YOUR_APP_ID";
   $app_secret = "YOUR_APP_SECRET";
   $my_url = "YOUR_URL";

   session_start();
   $code = $_REQUEST["code"];

   if(empty($code)) {
     $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
     $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
       . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
       . $_SESSION['state'];

     echo("<script> top.location.href='" . $dialog_url . "'</script>");
   }

   if($_REQUEST['state'] == $_SESSION['state']) {
     $token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
       . "&client_secret=" . $app_secret . "&code=" . $code;

     $response = file_get_contents($token_url);
     $params = null;
     parse_str($response, $params);

     $graph_url = "https://graph.facebook.com/me?access_token=" 
       . $params['access_token'];

     $user = json_decode(file_get_contents($graph_url));
     echo("Hello " . $user->name);
   }
   else {
     echo("The state does not match. You may be a victim of CSRF.");
   }

 ?>

Important notes:

  1. Since this is not a canvas app you don't need to use Javascript redirection (I guess you already figured out the authentication part)
  2. You may need to add the scope parameter and request extra permissions (email..etc) based on your requirements
  3. After that, and with a valid access_token you will be able to retrieve the info you want:

    $graph_url = "https://graph.facebook.com/me?access_token=" 
           . $params['access_token'];
    


Are you using Facebook PHP SDK? What you need to do first is to create the login URL:

$url = $facebook->getLoginUrl(array(
            'canvas' => 0,
            'fbconnect' => 1,
            'req_perms' => 'read_stream, publish_stream, email' 
));

With this you are getting permission to read the email, stream and publish on their walls.

After you successfully logged, you can get all the user information using:

$userData = $facebook->api('/me'); // get logged user data
print_r($userData); // print everthing

and then manipulate the data using the $userData array.

If you have any doubts don't hesitate to ask.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜