我尝试使用PHP cURL从www.mackolik.com获取数据。实际上,我可以从除此页面之外的几乎所有页面获取数据:http://www.mackolik.com/AjaxHandlers/TeamHandler.aspx?command=teamtabs&id=591&seasonName=2015/2016&type=2
此页面包含团队统计信息。我需要这个页面。如何获取此页面的数据?你能救我吗?
您可以进入此页面:http://www.mackolik.com/Takim/591/Amedspor然后点击“İstatistik”(英文统计信息)。我想要这部分内容。希望你能理解我的问题......
我的代码:
function poster($url,$fields_string){
$ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_COOKIE, 'NID=67=pdjIQN5CUKVn0bRgAlqitBk7WHVivLsbLcr7QOWMn35Pq03N1WMy6kxYBPORtaQUPQrfMK4Yo0vVz8tH97ejX3q7P2lNuPjTOhwqaI2bXCgPGSDKkdFoiYIqXubR0cTJ48hIAaKQqiQi_lpoe6edhMglvOO9ynw; PREF=ID=52aa671013493765:U=0cfb5c96530d04e3:FF=0:LD=en:TM=1370266105:LM=1370341612:GM=1:S=Kcc6KUnZwWfy3cOl; OTZ=1800625_34_34__34_; S=talkgadget=38GaRzFbruDPtFjrghEtRw; SID=DQAAALoAAADHyIbtG3J_u2hwNi4N6UQWgXlwOAQL58VRB_0xQYbDiL2HA5zvefboor5YVmHc8Zt5lcA0LCd2Riv4WsW53ZbNCv8Qu_THhIvtRgdEZfgk26LrKmObye1wU62jESQoNdbapFAfEH_IGHSIA0ZKsZrHiWLGVpujKyUvHHGsZc_XZm4Z4tb2bbYWWYAv02mw2njnf4jiKP2QTxnlnKFK77UvWn4FFcahe-XTk8Jlqblu66AlkTGMZpU0BDlYMValdnU; HSID=A6VT_ZJ0ZSm8NTdFf; SSID=A9_PWUXbZLazoEskE; APISID=RSS_BK5QSEmzBxlS/ApSt2fMy1g36vrYvk; SAPISID=ZIMOP9lJ_E8SLdkL/A32W20hPpwgd5Kg1J');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
$last = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return array($result,$last);
}
$a = poster("http://www.mackolik.com/Takim/591/Amedspor#ui-tabs-1");
$b = poster("http://www.mackolik.com/AjaxHandlers/TeamHandler.aspx?command=teamtabs&id=591&seasonName=2015/2016&type=2");
print_r($a);
print_r($b);
答案 0 :(得分:3)
只要远程应用程序不需要会话cookie或类似的东西,这很容易。
根据您的具体情况,远程服务器正在检查 Referer HTTP标头是否存在,如果存在,它应该与内部HOST端点匹配(例如单击按钮“İstatistik “并解雇ajax req) 请求方法也是 GET ,而不是POST 即使不是严格要求,也可以发送“X-Requested-With”,就像原始的HTTP请求一样。
$http_headers = array(
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:40.0)'
. 'Gecko/20100101 Firefox/40.0',
'Accept: */*',
'X-Requested-With: XMLHttpRequest',
'Referer: http://www.mackolik.com/Takim/591/Amedspor', # IMPORTANT
'Accept-Language: pt-BR,en-US;q=0.7,en;q=0.3',
);
$url = 'http://www.mackolik.com/AjaxHandlers/TeamHandler.aspx';
$url .= '?command=teamtabs&id=591&seasonName=2015/2016&type=2';
$opener = curl_init($url);
curl_setopt_array($opener, array(
CURLOPT_HTTPHEADER => $http_headers,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_RETURNTRANSFER => true,
));
$content = curl_exec($opener);
curl_close($opener);
echo $content;
请参阅HTTP请求: