Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用cURL和PHP登录Facebook_Php_Facebook_Curl_Web Crawler_Screen Scraping - Fatal编程技术网

使用cURL和PHP登录Facebook

使用cURL和PHP登录Facebook,php,facebook,curl,web-crawler,screen-scraping,Php,Facebook,Curl,Web Crawler,Screen Scraping,我正在尝试使用curl访问facebook登录页面。我的目的是登录facebook,然后做一些调整。由于最新的限制,我没有使用facebook API。。。我需要在帖子上刮掉评论,而这仅仅通过使用API是不可能的 以下是我的一些代码: curl_setopt($ch, CURLOPT_URL,"https://web.facebook.com"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $response = curl_exec($ch); c

我正在尝试使用curl访问facebook登录页面。我的目的是登录facebook,然后做一些调整。由于最新的限制,我没有使用facebook API。。。我需要在帖子上刮掉评论,而这仅仅通过使用API是不可能的

以下是我的一些代码:

curl_setopt($ch, CURLOPT_URL,"https://web.facebook.com");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
我希望它重定向到登录页面,然后当用户填写登录表单时,我会获取凭据并使用它们重定向到主页并开始抓取

无论如何,这就是我得到的:

(顺便说一句,我是该程序的作者)登录facebook发送消息。可以找到登录代码,登录过程在构造函数中完成

但其要点是,您需要首先执行GET请求,以获取cookie和csrf令牌以及一些东西,从lgoin表单中解析这些内容,然后将其发布回
应用程序/x-www-form-urlcoded
post请求,以及特定于cookie会话的登录url的用户名和密码,您还必须从第一个GET请求中接收的html中解析出的url

使用一个用户代理意味着你有糟糕的javascript支持(因为实际上,在PHP中,你没有),这也是你最感兴趣的,代码中使用的一个例子是“Mozilla/5.0(BlackBerry;U;BlackBerry 9300;en)AppleWebKit/534.8+(KHTML,比如Gecko)Version/6.0.0.570 Mobile Safari/534.8+”(又名旧黑莓手机)

  • 现在,如果您使用smartphone用户代理,它有时可能会要求您安装一个smartphone应用程序,如果您遇到这个问题,它将不允许您完成登录,直到您回答“是”或“否”,所以您需要添加代码来检测该问题,如果存在,您可以使用XPath
    “//a[contains]来检测该问题(@href,/login/save device/cancel/)]“
    和protip,一种确认您已成功登录的好方法,是查找注销按钮,在XPath中类似于
    //a[包含(@href,/logout.php”)]
守则中最相关的部分是:

function __construct() {
    $this->recipientID = \MsgMe\getUserOption ( 'Facebook', 'recipientID', NULL );
    if (NULL === $this->recipientID) {
        throw new \Exception ( 'Error: cannot find [Facebook] recipientID option!' );
    }
    $this->email = \MsgMe\getUserOption ( 'Facebook', 'email', NULL );
    if (NULL === $this->email) {
        throw new \Exception ( 'Error: cannot find [Facebook] email option!' );
    }
    $this->password = \MsgMe\getUserOption ( 'Facebook', 'password', NULL );
    if (NULL === $this->password) {
        throw new \Exception ( 'Error: cannot find [Facebook] password option!' );
    }
    $this->hc = new \hhb_curl ();
    $hc = &$this->hc;
    $hc->_setComfortableOptions ();
    $hc->setopt_array ( array (
            CURLOPT_USERAGENT => 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9300; en) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.570 Mobile Safari/534.8+',
            CURLOPT_HTTPHEADER => array (
                    'accept-language:en-US,en;q=0.8' 
            ) 
    ) );
    $hc->exec ( 'https://m.facebook.com/' );
    // \hhb_var_dump ( $hc->getStdErr (), $hc->getStdOut () ) & die ();
    $domd = @\DOMDocument::loadHTML ( $hc->getResponseBody () );    
    $form = (\MsgMe\tools\getDOMDocumentFormInputs ( $domd, true )) ['login_form'];
    $url = $domd->getElementsByTagName ( "form" )->item ( 0 )->getAttribute ( "action" );
    $postfields = (function () use (&$form): array {
        $ret = array ();
        foreach ( $form as $input ) {
            $ret [$input->getAttribute ( "name" )] = $input->getAttribute ( "value" );
        }
        return $ret;
    });
    $postfields = $postfields (); // sorry about that, eclipse can't handle IIFE syntax.
    assert ( array_key_exists ( 'email', $postfields ) );
    assert ( array_key_exists ( 'pass', $postfields ) );
    $postfields ['email'] = $this->email;
    $postfields ['pass'] = $this->password;
    $hc->setopt_array ( array (
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query ( $postfields ),
            CURLOPT_HTTPHEADER => array (
                    'accept-language:en-US,en;q=0.8' 
            ) 
    ) );
    // \hhb_var_dump ($postfields ) & die ();
    $hc->exec ( $url );
    // \hhb_var_dump ( $hc->getStdErr (), $hc->getStdOut () ) & die ();

    $domd = @\DOMDocument::loadHTML ( $hc->getResponseBody () );
    $xp = new \DOMXPath ( $domd );
    $InstallFacebookAppRequest = $xp->query ( "//a[contains(@href,'/login/save-device/cancel/')]" );
    if ($InstallFacebookAppRequest->length > 0) {
        // not all accounts get this, but some do, not sure why, anyway, if this exist, fb is asking "ey wanna install the fb app instead of using the website?"
        // and won't let you proceed further until you say yes or no. so we say no.
        $url = 'https://m.facebook.com' . $InstallFacebookAppRequest->item ( 0 )->getAttribute ( "href" );
        $hc->exec ( $url );
        $domd = @\DOMDocument::loadHTML ( $hc->getResponseBody () );
        $xp = new \DOMXPath ( $domd );
    }
    unset ( $InstallFacebookAppRequest, $url );
    $urlinfo = parse_url ( $hc->getinfo ( CURLINFO_EFFECTIVE_URL ) );
    $a = $xp->query ( '//a[contains(@href,"/logout.php")]' );
    if ($a->length < 1) {
        $debuginfo = $hc->getStdErr () . $hc->getStdOut ();
        $tmp = tmpfile ();
        fwrite ( $tmp, $debuginfo );
        $debuginfourl = shell_exec ( "cat " . escapeshellarg ( stream_get_meta_data ( $tmp ) ['uri'] ) . " | pastebinit" );
        fclose ( $tmp );
        throw new \RuntimeException ( 'failed to login to facebook! apparently... cannot find the logout url!  debuginfo url: ' . $debuginfourl );
    }
    $a = $a->item ( 0 );
    $url = $urlinfo ['scheme'] . '://' . $urlinfo ['host'] . $a->getAttribute ( "href" );
    $this->logoutUrl = $url;
    // all initialized, ready to sendMessage();
}
函数uuu构造(){
$this->recipientID=\MsgMe\getUserOption('Facebook','recipientID',NULL);
如果(NULL==$this->recipientID){
抛出新的\异常('Error:找不到[Facebook]recipientID选项!');
}
$this->email=\MsgMe\getUserOption('Facebook','email',NULL);
如果(NULL==$this->email){
抛出新的\异常('Error:找不到[Facebook]电子邮件选项!');
}
$this->password=\MsgMe\getUserOption('Facebook','password',NULL);
如果(NULL==$this->password){
抛出新\异常('Error:找不到[Facebook]密码选项!');
}
$this->hc=new\hhb_curl();
$hc=&$this->hc;
$hc->_设置舒适选项();
$hc->setopt_数组(数组)(
CURLOPT_USERAGENT=>“Mozilla/5.0(BlackBerry;U;BlackBerry 9300;en)AppleWebKit/534.8+(KHTML,类似Gecko)版本/6.0.0.570 Mobile Safari/534.8+”,
CURLOPT_HTTPHEADER=>数组(
'接受语言:en-US,en;q=0.8'
) 
) );
$hc->exec($hc)https://m.facebook.com/' );
//\hhb_var_dump($hc->getStdErr(),$hc->getStdOut())和die();
$domd=@\DOMDocument::loadHTML($hc->getResponseBody());
$form=(\MsgMe\tools\getDOMDocumentFormInputs($domd,true))['login\u form'];
$url=$domd->getElementsByTagName(“表单”)->item(0)->getAttribute(“操作”);
$postfields=(函数()使用(&$form):数组{
$ret=array();
foreach($formas$input){
$ret[$input->getAttribute(“名称”)]=$input->getAttribute(“值”);
}
返回$ret;
});
$postfields=$postfields();//很抱歉,eclipse无法处理IIFE语法。
断言(数组键存在('email',$postfields));
断言(数组键存在('pass',$postfields));
$postfields['email']=$this->email;
$postfields['pass']=$this->password;
$hc->setopt_数组(数组)(
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>http_build_query($POSTFIELDS),
CURLOPT_HTTPHEADER=>数组(
'接受语言:en-US,en;q=0.8'
) 
) );
//\hhb_var_dump($postfields)和die();
$hc->exec($url);
//\hhb_var_dump($hc->getStdErr(),$hc->getStdOut())和die();
$domd=@\DOMDocument::loadHTML($hc->getResponseBody());
$xp=new\DOMXPath($domd);
$InstallFacebookAppRequest=$xp->query(//a[包含(@href,/login/save device/cancel/)];
如果($InstallFacebookAppRequest->length>0){
//并不是所有的帐户都会收到,但有些帐户会收到,不知道为什么,无论如何,如果存在这种情况,fb会问“你想安装fb应用程序而不是使用网站吗?”
//在你说“是”或“否”之前不会让你继续前进,所以我们说“不”。
$url='1https://m.facebook.com'.$InstallFacebookAppRequest->item(0)->getAttribute(“href”);
$hc->exec($url);
$domd=@\DOMDocument::loadHTML($hc->getResponseBody());
$xp=new\DOMXPath($domd);
}
未设置($InstallFacebookAppRequest,$url);
$urlinfo=parse_url($hc->getinfo(CURLINFO_-EFFECTIVE_-url));
$a=$xp->query('//a[包含(@href,“/logout.php”)]);
如果($a->长度<1){
$debuginfo=$hc->getStdErr()。$hc->getStdOut();
$tmp=tmpfile();
fwrite($tmp,$debuginfo);
$debuginfourl=shell_exec(“cat”).escapeshellarg(stream_get_meta_data($tmp)['uri'])。“| pastbinit”);
fclose($tmp);
抛出new\RuntimeException('登录facebook失败!显然…找不到注销url!debuginfo url:'。$debuginfourl);
}
$a=$a->项目(0);
$url=$urlinfo['scheme'].://'.$urlinfo['host'.$a->getAttribute(“href”);
$this->logoutUrl=$url;