title: Facebook Connect and Codeigniter - HTTP/1.0 400 Bad Request
body: <p>i'm struggling really hard to implement Facebook connect into my codeigniter application - I have one issue right now. After downloading the PHP SDK from here: <a href="https://github.com/facebook/php-sdk/" rel="nofollow">https://github.com/facebook/php-sdk/</a> I copied the /src files into my CI libraries (as everyone do : )). </p>
  
  <p>Now I want to authenticate my users using this method: </p>
  
  <p><a href="https://developers.facebook.com/docs/authentication/server-side/" rel="nofollow">https://developers.facebook.com/docs/authentication/server-side/</a></p>
  
  <p>My controller is here (I think the problem is at the end, but pasted full file, cause i'm not sure):</p>
  
  <pre><code>&lt;?php
  
  class Fb25 extends CI_Controller {
  
  function index()
  {
              $app_id     = "MY APP ID";
      $app_secret = "MY SECRET KEY";
      $site_url   = "http://devlocalhost.com/";
  
  $this-&gt;load-&gt;library('facebook',array('appId' =&gt; $app_id, 'secret' =&gt; $app_secret));
  
      $user = $this-&gt;facebook-&gt;getUser();
      $data['user'] = $user;
  
  if($user){
  
      $data['user_profile'] = $this-&gt;facebook-&gt;api('/me');    
      $user_profile = $data['user_profile'];
  
  } else{
  
      $user = NULL;
  
  }       
  
  if($user){
  // Get logout URL
  $data['logoutUrl'] = $this-&gt;facebook-&gt;getLogoutUrl(array(
      'next'  =&gt; 'http://devlocalhost.com/', // URL to which to redirect the user after logging out
      ));
  }else{
  // Get login URL
      $data['loginUrl'] = $this-&gt;facebook-&gt;getLoginUrl(array(
      'scope'     =&gt; 'email', // Permissions to request from the user
      'redirect_uri'  =&gt; 'http://devlocalhost.com/fb25/good', // URL to redirect the user to once the login/authorization process is complete.
      ));
  }
  
  if($user){
  
  
  // Save your method calls into an array
  $data['queries'] = array(
      array('method' =&gt; 'GET', 'relative_url' =&gt; '/'.$user),
      array('method' =&gt; 'GET', 'relative_url' =&gt; '/'.$user.'/home?limit=50'),
      array('method' =&gt; 'GET', 'relative_url' =&gt; '/'.$user.'/friends'),
      array('method' =&gt; 'GET', 'relative_url' =&gt; '/'.$user.'/photos?limit=6'),
      );
  
  // POST your queries to the batch endpoint on the graph.
      $batchResponse = $facebook-&gt;api('?batch='.json_encode($queries), 'POST');
  
  
  //Return values are indexed in order of the original array, content is in ['body'] as a JSON
  //string. Decode for use as a PHP array.
  $data['user_info']      = json_decode($batchResponse[0]['body'], TRUE);
  $data['feed']           = json_decode($batchResponse[1]['body'], TRUE);
  $data['friends_list']   = json_decode($batchResponse[2]['body'], TRUE);
  $data['photos']         = json_decode($batchResponse[3]['body'], TRUE);
  
  
      }
      echo 'user_info: '.$data['user_info'];
      print_r($data);
  
      $this-&gt;load-&gt;view('fb25_view', $data);
  }
  
  public function good(){
  
  $app_id     = "MY APP ID";
      $app_secret = "MY SECRET KEY";
      $site_url   = "http://devlocalhost.com/";
  
  
  echo '&lt;br/&gt;&lt;br/&gt;SESJA:&lt;br/&gt;';
  print_r($_SESSION);
  echo '&lt;br/&gt;&lt;br/&gt;GET:&lt;br/&gt;';
  print_r($_GET);
  echo '&lt;br/&gt;&lt;br/&gt;REQUEST:&lt;br/&gt;';
  print_r($_REQUEST);
      echo '&lt;br/&gt;&lt;br/&gt;najs&lt;br/&gt;&lt;br/&gt;';
  
  
  
  
     session_start();
  
    $code = $_GET["code"];
  
    //@@@@@@IMO PROBLEM IS SOMEWHERE HERE: 
  
  
   $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);
  
  
  
  }
  }
  ?&gt;
  </code></pre>
  
  <p>My problem is: I'm pressing the login button and gets into facebook to authenticate myself, clicking "go to app" and want to go back into my website - it says:</p>
  
  <pre><code>Message: file_get_contents(https://graph.facebook.com/oauth/access_token? client_id=487560264592073&amp;redirect_uri=http%3A%2F%2Fdevlocalhost.com%2F&amp;client_secret=mysecretcode&amp;code=AQAgnmNWwDub9yaRB6Vf73gg8Xvwo8KhIM077lB67_bu1Z3rAvyk3Ckl54qK7hh9o3VkG0rFIBTfRXwtrSBFVWEpqYfm1o7e5CQg3jVctq-EE1ZxWrgWrfesLpQ2oF3wlmEMb5o6ORobGmibT06kqe5f2N0ch4kSYBJ4SiTcdV-612fGOJHGcipeyU_GJJ0Jvsg) 
  
  [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request 
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Look here, please:</p>
  
  <p>I added function into your controller:</p>
  
  <pre><code>public function link(){
  $fb_usr = $this-&gt;fb_connect-&gt;user;
   $firstname = $fb_usr['first_name']; 
  
  $my_url="http://devlocal.pl";
  echo 'user_id';
  
  
   $token_url = "https://graph.facebook.com/oauth/access_token?"
     . "client_id=myclientid&amp;redirect_uri=" . urlencode($my_url)
     . "&amp;client_secret=mysecretcode&amp;code=" . $_GET['code'];
  
      redirect($token_url);
  </code></pre>
  
  <p>}</p>
  
  <p>To get my access token - without access token I can't get my username..
  I made your loginbyfacebook function redirecting into link function. And it doesn't show the fb username.</p>
  
  <p>When I'm redirecting to my token_url It shows: </p>
  
  <pre><code>{
  "error": {
    "message": "Error validating verification code.",
    "type": "OAuthException",
    "code": 100
     }
  }
  </code></pre>