0

我正在尝试编写一个小的控制台 PHP 程序,它可以从我的 facebook 帐户中提取一些最近的帖子数据。我试图组装一个刮板,但由于某种原因它没有返回任何数据?

我在想,也许 facebook 以某种方式检测到我试图抓取它或其他什么?这是我正在使用的代码。

<?php 

function insearch($string, $start, $end)
{
    $result = array();
$string = " ".$string;
$offset = 0;
while(true)
{
    $ini = strpos($string,$start,$offset);
    if ($ini == 0)
        break;
        $ini += strlen($start);
        $len = strpos($string,$end,$ini) - $ini;
        $result[] = substr($string,$ini,$len);
        $offset = $ini+$len;
    }
return $result[0];
}

$username = '[phonenumber]';
$password = '[password]';
$url = 'https://m.facebook.com/login.php';
$ch=login();
$html=downloadUrl('https://m.facebook.com/home.php?sk=h_chr', $ch);
echo $html;

function downloadUrl($Url, $ch){
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_REFERER, "https://m.facebook.com/");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);
    return $output;
}

function login(){
    $home = file_get_contents("https://m.facebook.com/?_rdr");
    $lsd = insearch($home, '<input type="hidden" name="lsd" value="', '" autocomplete="off" />');
echo "lsd $lsd";
$charset = insearch($home, '<input type="hidden" name="charset_test" value="', '" />');
echo "charset $charset";
$mts = insearch($home, 'f" name="m_ts" value="', '" />');
echo "mts $mts";
$li = insearch($home, ' name="li" value="', '" />');
echo "li $li";
global $url,$username,$password;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); //login URL
    curl_setopt ($ch, CURLOPT_POST, 1);
    $postData="lsd=$lsd
    &charset_test=$charset
        &version=1
        &ajax=0
        &width=0
    &pxr=0
    &gps=0
    &m_ts=$mts
    &li=$li
            &signup_layout=layout%7Cbottom_clean%7C%7Cwider_form%7C%7Cprmnt_btn%7Cspecial%7C%7Cst%7Ccreate%7C%7Cheader_crt_acct_button%7C%7Chdbtn_color%7Cgreen%7C%7Csignupinstr%7C%7Claunched_Mar3
    &email=$username
    &pass=$password
&login=Log+In";
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $store = curl_exec ($ch);
echo $store;
    return $ch;
}
?> 
4

1 回答 1

0

您是否正在访问移动版本,但您的用户代理是 Windows NT 6.1。首先更改您的用户代理!或者更改正在访问的 facebook 版本。;)

于 2014-04-16T16:54:21.983 回答