2
幾天前我決定寫一個簡短的腳本。它向ASP服務器發出兩個GET請求,因爲我想獲得變量sessionId變量和視圖狀態變量。 我決定使用fsockopen(),它的工作原理直到第一個請求!之後,它崩潰,並有一個錯誤的請求錯誤。如何使用PHP在ASP服務器上獲取網頁?
下面的代碼:
<?php
$fp = fsockopen("www.atb.bergamo.it", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET /ITA/Default.aspx?SEZ=2&PAG=38&MOD=LINTRV HTTP/1.1\r\n";
$out .= "Host: www.atb.bergamo.it\r\n";
$out .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2\r\n";
$out .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$out .= "Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3\r\n";
$out .= "Accept-Encoding: gzip, deflate\r\n";
$out .= "Connection: keep-alive\r\n\r\n";
fwrite($fp, $out);
$header = '';
do{
$header .= fgets($fp, 128);
}while(strpos($header, "\r\n\r\n") == false);
list(,$sessione) = explode('ASP.NET_SessionId=', $header);
list($sessione) = explode('; path=/; HttpOnly', $sessione);
$cookie = "Cookie: ASP.NET_SessionId=".$sessione."; READONLY=MA==; __utma=".$_COOKIE['__utma']."; __utmb=".$_COOKIE['__utmb']."; __utmc=".$_COOKIE['__utmc']."; __utmz=".$_COOKIE['__utmz']."; HstCfa1041967=".$_COOKIE['HstCfa1041967']."; HstCla1041967=".$_COOKIE['HstCla1041967']."; HstCmu1041967=".$_COOKIE['HstCmu1041967']."; HstPn1041967=".$_COOKIE['HstPn1041967']."; HstPt1041967=".$_COOKIE['HstPt1041967']."; HstCnv1041967=".$_COOKIE['HstCnv1041967']."; HstCns1041967=".$_COOKIE['HstCns1041967']."; c_ref_1041967=http%3A%2F%2Fwww.google.it%2Furl%3Fsa%3Dt%26rct%3Dj%26q%3Datb%2520orari%26source%3Dweb%26cd%3D1%26sqi%3D2%26ved%3D0CC4QFjAA%26url%3Dhttp%253A%252F%252Fwww.atb.bergamo.it%252FITA%252FDefault.aspx%253FSEZ%253D2%2526PAG%253D38%2526MOD%253DLINTRV%26ei%3DxZi3UNK4AojysgburIGoCg%26usg%3DAFQjCNFKu0Tuffg6uZVhC1akh3TKqesWcQ;"; // __atuvc=27%7C48
$cookie = str_replace("utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(none)", "utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=atb", $cookie);
echo "</br></br></br></br> ci sono </br></br></br></br>";
$out = "GET /ITA/Default.aspx?SEZ=2&PAG=38&MOD=LINTRV HTTP/1.1\r\n";
$out .= "Host: www.atb.bergamo.it\r\n";
$out .= "Host: www.atb.bergamo.it\r\n";
$out .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2\r\n";
$out .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$out .= "Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3\r\n";
$out .= "Accept-Encoding: gzip, deflate\r\n";
$out .= "Connection: keep-alive\r\n";
$out .= $cookie."\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
//die($header."</br></br></br></br>".$sessione);
fclose($fp);
}
>
我在本地服務器上運行腳本,server2go。我不知道爲什麼它不工作... 你能幫我嗎?
考慮使用[GOUTTE](https://github.com/fabpot/Goutte),這是一個PHP web的刮庫。 – SDC