0

我想通过 CURL 登录 Facebook。并通过 CURL 授权我的应用程序。

几天前,Facebook 改变了登录程序。我的代码无法再登录。但是我在修复它时遇到了一些问题。请帮我解决这个问题。

以下是我的原始代码。

<?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->find('input[name=locale]', 0);
    $locale = $data->value;
    //echo "locale = $locale\n";
    $data = $html->find('input[name=lsd]', 0);
    $lsd = $data->value;
    //echo "lsd = $lsd\n";
    $data = $html->find('input[name=lgnrnd]', 0);
    $lgnrnd = $data->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&persistent=1timezone=-480&locale=$locale&email=$username&pass=$password&lsd=$lsd&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->curl_get("https://www.facebook.com/dialog/oauth?client_id=" . self::appId . "&redirect_uri=" . self::redirect_uri . "&response_type=token", $cookie_file);
    //echo $curlData;

    $headers = http_parse_headers($curlData);
    print_r($headers);
    $app_approval = false;
    foreach($headers as $k => $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->find('input[name=encoded_state]', 0);
        $encoded_state = $data->value;

        $data = $html->find('input[name=fb_dtsg]', 0);
        $fb_dtsg = $data->value;

        $data = $html->find('input[name=read]', 0);
        $read = $data->value;

        $data = $html->find('input[name=redirect_uri]', 0);
        $redirect_uri = $data->value;

        $data = $html->find('input[name=app_id]', 0);
        $app_id = $data->value;

        $data = $html->find('input[name=return_foramt]', 0);
        $return_format = $data->value;

        $data = $html->find('input[name=write]', 0);
        $write = $data->value;

        $curlData =$this->curl_post("https://www.facebook.com/dialog/oauth/read", 
        "__CONFIRM__=1&app_id=$app_id&display=page&encoded_state=$encoded_state&fb_dtsg=$fb_dtsg&from_post=1&read=$read&redirect_uri=$redirect_uri&return_format=$return_format&write=$write", $cookie_file);
    }

    $headers = http_parse_headers($curlData);
    print_r($headers);
    foreach($headers as $k => $v) {
        if(strtolower($k) == 'location') {
            $s = $v;
            break;
        }
    }

    $access_token = substr($s, strpos($s, '=')+1, strpos($s, '&e')-strpos($s, '=')-1);

    unlink($cookie_file);
    return $access_token;

 }
}
4

1 回答 1

0

我也在做同样的事情。我的代码很简单,看一下。

$login_email = 'ACCOUNTEMAIL';
$login_pass = 'ACCOUNTPASSWORD';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.facebook.com/login.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com");
$page = curl_exec($ch);

以上工作正常登录用户。但现在我正在尝试获取访问令牌但没有成功。

于 2013-09-23T23:39:11.363 回答