0

I am trying to get Facebook profile picture . I am using the following with my value in place of ######### . After running the PHP i get a message "Hello". I want that i get my name also with it like "Hellow Aditya" along with my profile picture. what should i edit to get that ? Also would it be possible to save the profile picture on the sever itself?

<?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.");
   }

?>
Yahoo
  • 4,093
  • 17
  • 59
  • 85

1 Answers1

1

You can get it this way: http://graph.facebook.com/[USER ID]/picture

blaff
  • 304
  • 1
  • 5
  • @blaff- Thanks for the fast response. I could not understand how can i execute it from the code above ? Where should i put the code ? – Yahoo Apr 20 '12 at 07:47
  • you can use it like this: – Yan Berk Apr 20 '12 at 09:48
  • @Yan - Hellow :) When i run the above code i get just the word "Hello" but according to the code i think i should get "Hellow Aditya(USERNAME)" BuT this isnt happening. When i paste - http://graph.facebook.com/me/picture?type=large I get token error 2500 message": "An active access token must be used to query information about the current user.", Could you help me in this please – Yahoo Apr 20 '12 at 11:52