title: Use CURL to Login Facebook and do take access token for my app directly
body: <p>I want to login to Facebook by CURL.
  And give authorization to my app by CURL,too.</p>
  
  <p>Few days ago, Facebook changed the login procedure.
  My code can't login any more.
  But I get some problem on fix it.
  Please help me to solve this problem.</p>
  
  <p>Following is my code original.</p>
  
  <pre><code>&lt;?php
    require_once 'simple_html_dom.php';
    Class Fb_login {    
  
    const appId = xxx;
    const redirect_uri = 'url';
  
    function curl_post($url, $post_data, $cookie_file)
    {
      $curl = curl_init ();
      curl_setopt ( $curl, CURLOPT_URL, $url );
      curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
      curl_setopt ( $curl, CURLOPT_POST, 1 );
      curl_setopt ( $curl, CURLOPT_POSTFIELDS, $post_data);
      curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
      curl_setopt ( $curl, CURLOPT_ENCODING, "" );
      curl_setopt ( $curl, CURLOPT_COOKIEFILE, $cookie_file );
      curl_setopt ( $curl, CURLOPT_COOKIEJAR, $cookie_file );
      curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );
      curl_setopt ( $curl, CURLOPT_HEADER, 1 );   // return header info
      $curlData = curl_exec ( $curl );
      curl_close ( $curl );
      return $curlData;
    }
  
    function curl_get($url, $cookie_file)
    {
      $curl = curl_init ();
      curl_setopt ( $curl, CURLOPT_URL, $url );
      curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
      curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
      curl_setopt ( $curl, CURLOPT_ENCODING, "" );
      curl_setopt ( $curl, CURLOPT_COOKIEFILE, $cookie_file );
      curl_setopt ( $curl, CURLOPT_COOKIEJAR, $cookie_file );
      curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );
      curl_setopt ( $curl, CURLOPT_HEADER, 1 );   // return header info
      $curlData = curl_exec ( $curl );
      curl_close ( $curl );
      return $curlData;
    }
  
    function login($username, $password)
    {
      $cookie_file = '/tmp/cookies_facebook.'.uniqid();
      $curl = curl_init ();
      curl_setopt ( $curl, CURLOPT_URL, "http://www.facebook.com" );
      curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 );
      curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
      curl_setopt ( $curl, CURLOPT_ENCODING, "" );
      curl_setopt ( $curl, CURLOPT_COOKIEJAR, $cookie_file );
      curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );
      $curlData = curl_exec ( $curl );
      curl_close ( $curl );
  
      $html = str_get_html($curlData);
  
      // do get some parameters for login to facebook
      $data = $html-&gt;find('input[name=locale]', 0);
      $locale = $data-&gt;value;
      //echo "locale = $locale\n";
      $data = $html-&gt;find('input[name=lsd]', 0);
      $lsd = $data-&gt;value;
      //echo "lsd = $lsd\n";
      $data = $html-&gt;find('input[name=lgnrnd]', 0);
      $lgnrnd = $data-&gt;value;
      //echo "lgnrnd = $lgnrnd\n";
  
      // do login to facebook
      $curl = curl_init ();
      curl_setopt ( $curl, CURLOPT_URL, "https://login.facebook.com/login.php?login_attempt=1" );
      curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 );
      curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
      curl_setopt ( $curl, CURLOPT_POST, 1 );
      curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
      curl_setopt ( $curl, CURLOPT_POSTFIELDS, "default_persistent=1&amp;persistent=1timezone=-480&amp;locale=$locale&amp;email=$username&amp;pass=$password&amp;lsd=$lsd&amp;lgnrnd=$lgnrnd" );
      curl_setopt ( $curl, CURLOPT_ENCODING, "" );
      curl_setopt ( $curl, CURLOPT_COOKIEFILE, $cookie_file );
      curl_setopt ( $curl, CURLOPT_COOKIEJAR, $cookie_file );
      curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );
      curl_setopt ( $curl, CURLOPT_HEADER, 1 );   // return header info
      $curlData = curl_exec ( $curl );
      curl_close ( $curl );
  
      // Get access token
      $curlData = $this-&gt;curl_get("https://www.facebook.com/dialog/oauth?client_id=" . self::appId . "&amp;redirect_uri=" . self::redirect_uri . "&amp;response_type=token", $cookie_file);
      //echo $curlData;
  
      $headers = http_parse_headers($curlData);
      print_r($headers);
      $app_approval = false;
      foreach($headers as $k =&gt; $v) {
          if(strtolower($k) == 'location') {
              $s = $v;
              $app_approval = true;
              break;
          }
      }
  
      // APP approval
      if(!$app_approval) {
          $html = str_get_html($curlData);
          // do get some parameters
          $data = $html-&gt;find('input[name=encoded_state]', 0);
          $encoded_state = $data-&gt;value;
  
          $data = $html-&gt;find('input[name=fb_dtsg]', 0);
          $fb_dtsg = $data-&gt;value;
  
          $data = $html-&gt;find('input[name=read]', 0);
          $read = $data-&gt;value;
  
          $data = $html-&gt;find('input[name=redirect_uri]', 0);
          $redirect_uri = $data-&gt;value;
  
          $data = $html-&gt;find('input[name=app_id]', 0);
          $app_id = $data-&gt;value;
  
          $data = $html-&gt;find('input[name=return_foramt]', 0);
          $return_format = $data-&gt;value;
  
          $data = $html-&gt;find('input[name=write]', 0);
          $write = $data-&gt;value;
  
          $curlData =$this-&gt;curl_post("https://www.facebook.com/dialog/oauth/read", 
          "__CONFIRM__=1&amp;app_id=$app_id&amp;display=page&amp;encoded_state=$encoded_state&amp;fb_dtsg=$fb_dtsg&amp;from_post=1&amp;read=$read&amp;redirect_uri=$redirect_uri&amp;return_format=$return_format&amp;write=$write", $cookie_file);
      }
  
      $headers = http_parse_headers($curlData);
      print_r($headers);
      foreach($headers as $k =&gt; $v) {
          if(strtolower($k) == 'location') {
              $s = $v;
              break;
          }
      }
  
      $access_token = substr($s, strpos($s, '=')+1, strpos($s, '&amp;e')-strpos($s, '=')-1);
  
      unlink($cookie_file);
      return $access_token;
  
   }
  }
  </code></pre>