2015-09-22 131 views
2

我試圖用PHP cURL從www.mackolik.com獲取數據。其實,我可以從幾乎所有的頁面中獲取數據,但本頁除外:http://www.mackolik.com/AjaxHandlers/TeamHandler.aspx?command=teamtabs&id=591&seasonName=2015/2016&type=2PHP cURL 403禁止的錯誤(Mackolik.com)

此頁有統計的隊伍。我需要這個頁面。我如何獲取此頁面的數據?你可以幫我嗎?

您可以輸入此頁面: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); 

回答

3

這很容易,只要在遠程應用程序不需要會話cookie或者類似的東西。

您的具體情況下,遠程服務器檢查是否的Referer HTTP標頭存在,如果是這樣,應該與內部主機終端匹配(如單擊按鈕上「İstatistik」和發射AJAX REQ)。
此外請求方法是GET,而不是POST。
即使不是嚴格要求,也可能是一個好主意,就像發送原始HTTP請求一樣發送「X-Requested-With」。

$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請求的樣子:

enter image description here

+0

感謝你這麼多! –

+0

@BatuhanKök嘿,我只是從Firefox網絡檢查員添加圖像。 – felipsmartins