title: how to get valid fb access_token using php curl?
body: <p>I am working on a project which require an access token of the user using curl. 
  The user will enter his username and password. he will be logged in to facebook using curl. The login part works. I also gets a token that has the pipline in it like: xxxxxxx|xxxxxxxxxxxxxxxxx, but i want an access token to post to the logged in user wall.</p>
  
  <p>The code that i am using is:</p>
  
  <pre><code>// your facebook credentials
  $username = "xxxxxxxxxxxxxxxx";
  $password = "xxxxxxxx";
  
  // access to facebook home page (to get the cookies)
  $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, getcwd () . '/cookies_facebook.cookie' );
  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 );
  
  // do get some parameters for login to facebook
  $charsetTest = substr ( $curlData, strpos ( $curlData, "name=\"charset_test\"" ) );
  $charsetTest = substr ( $charsetTest, strpos ( $charsetTest, "value=" ) + 7 );
  $charsetTest = substr ( $charsetTest, 0, strpos ( $charsetTest, "\"" ) );
  
  $locale = substr ( $curlData, strpos ( $curlData, "name=\"locale\"" ) );
  $locale = substr ( $locale, strpos ( $locale, "value=" ) + 7 );
  $locale = substr ( $locale, 0, strpos ( $locale, "\"" ) );
  
  $lsd = substr ( $curlData, strpos ( $curlData, "name=\"locale\"" ) );
  $lsd = substr ( $lsd, strpos ( $lsd, "value=" ) + 7 );
  $lsd = substr ( $lsd, 0, strpos ( $lsd, "\"" ) );
  
  // 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, "charset_test=" . $charsetTest . "&amp;locale=" . $locale . "&amp;non_com_login=&amp;email=" . $username . "&amp;pass=" . $password . "&amp;charset_test=" . $charsetTest . "&amp;lsd=" . $lsd );
  curl_setopt ( $curl, CURLOPT_ENCODING, "" );
  curl_setopt ( $curl, CURLOPT_COOKIEFILE, getcwd () . '/cookies_facebook.cookie' );
  curl_setopt ( $curl, CURLOPT_COOKIEJAR, getcwd () . '/cookies_facebook.cookie' );
  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)" );
  
  
  
  $url = "https://graph.facebook.com/oauth/access_token";
  $postString = "client_id=XXXXXXXX&amp;client_secret=XXXXXXXXXXXXXXX&amp;type=client_cred";
  
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_FAILONERROR, 1);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
  $access_token = str_replace( "access_token=", "", curl_exec($curl) );
  
  echo $access_token;
  </code></pre>