title: How to get user's data programatically, via facebook authentication?
body: <p>I am able to manually get the user's data, but not programatically.</p>
  
  <p>Using the server side code, as it is, from - <a href="https://developers.facebook.com/docs/authentication/" rel="nofollow">https://developers.facebook.com/docs/authentication/</a></p>
  
  <pre><code> &lt;?php 
  
   $app_id = "MY_APP_ID";
   $app_secret = "MY_APP_SECRET";
   $my_url = "ADDRESS_OF_CURRENT_PAGE";
  
   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 . "&amp;redirect_uri=" . urlencode($my_url) . "&amp;state="
       . $_SESSION['state'];
  
     echo("&lt;script&gt; top.location.href='" . $dialog_url . "'&lt;/script&gt;");
   }
  
   if($_REQUEST['state'] == $_SESSION['state']) {
     $token_url = "https://graph.facebook.com/oauth/access_token?"
     . "client_id=" . $app_id . "&amp;redirect_uri=" . urlencode($my_url)
     . "&amp;client_secret=" . $app_secret . "&amp;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-&gt;name);
   }
   else {
     echo("The state does not match. You may be a victim of CSRF.");
   }
  
   ?&gt;
  </code></pre>
  
  <p>The output is only Hello, the user name does not show up!</p>
  
  <p>I think the problem is with the file_get_contents(), as echo-ing $response has no output, where as $token_url has the appropriate value.</p>
  
  <pre><code>   $response = @file_get_contents($token_url);
  </code></pre>
  
  <ul>
  <li>PHP version = 5.2.9</li>
  <li>Error reporting is off, but including this in the code - error_reporting(E_ALL); gives no output.</li>
  </ul>
  
  <p><strong>UPDATE</strong> - So, I tried this after 6 months, and it worked. The only thing that has changed since then is my hosting. I was using HostBig before. <strong>Lesson</strong> - Don't depend on $1/month hosting services, they can't be trusted.</p>