如何从Facebook获取个人资料图片

时间:2012-04-20 07:40:44

标签: php facebook authentication fbml fbconnect

我正在尝试获取Facebook个人资料图片。我使用以下值代替#########。运行PHP后,我收到一条消息“Hello”。 我希望我的名字也像“Hellow Aditya”和我的个人资料图片一样。我该怎么编辑才能搞定?是否可以将配置文件图片保存在服务器本身上?

<?php 

   $app_id = ###############;
   $app_secret = "#################";
   $my_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.");
   }

?>

1 个答案:

答案 0 :(得分:1)

你可以这样得到它: http://graph.facebook.com/ [美国身份证] /图片