我正在尝试编写一个小程序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;
}
?>
答案 0 :(得分:0)
您是否正在访问移动版本,但您的用户代理是Windows NT 6.1。 首先更改您的用户代理!或者更改正在访问的facebook的版本。 ;)