2

A few days ago I decided to write a short script. It makes two GET requestes to an ASP server, because I want to get the sessionId variable and the viewstate variable. I decided to use fsockopen() and it works until the first request! After that it crashes and there's a Bad request error.

Here's the code:

<?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);
}

?>

I run the script on a local server, server2go. I don't know why it doesn't work... Can you help me please?

4

1 回答 1

1

Use curl

$url = "http://www.atb.bergamo.it/ITA/Default.aspx?SEZ=2&PAG=38&MOD=LINTRV";
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec ($ch);

curl_close($ch);

preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~',$html,$viewstate);


var_dump(file_get_contents($ckfile)); <--- cookie
var_dump($viewstate[1]);              <--- View State
于 2012-11-30T08:06:11.523 回答