0

我想编写一个脚本来解析我大学的时间表并将其保存到磁盘。我使用 curl 来做到这一点。这里是时间表的主要链接,如果在浏览器中打开它,我可以看到内容,但如果我尝试在 curl 中打开它,我会失败 =(

这是php脚本的源代码:

<?


    $url = "http://cist.kture.kharkov.ua/ias/app/tt/f?p=778:201:128623920522090:::201:P201_FIRST_DATE,P201_LAST_DATE,P201_GROUP,P201_POTOK:01.02.2012,30.07.2012,2423461,0:";

    $ch = curl_init();


    $cookieFile = tempnam ( dirname(__FILE__) . "/cookies/", 'cookie-' );



    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile );
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile );

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );

    $ua = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.26 Safari/535.11";


    //$headers = array('HOST: cist.kture.kharkov.ua','CONNECTION: keep-alive','CACHE_CONTROL: max-age=0','USER_AGENT: Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.26 Safari/535.11','ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','REFERER: http://google.com.ua',  'ACCEPT_ENCODING: gzip,deflate,sdch','ACCEPT_LANGUAGE: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4','ACCEPT_CHARSET: windows-1251,utf-8;q=0.7,*;q=0.3');
    //curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_USERAGENT, $ua);

    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    $info = curl_getinfo($ch);

    $counter = 0;
    while($info['redirect_url']!= "")
    {
        echo "url => ". $url."<br />\n";
        echo "redirect => ". $info['redirect_url']."<br /><br />\n";

        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_URL, $info['redirect_url']);
        $url = $info['redirect_url'];
        $data = curl_exec($ch);


        $info = curl_getinfo($ch);

        $counter++;

        if($counter>100)
            break;
    }



        foreach ($info as $key => $value) {
            echo $key . " -> ".$value."<br />\n";
        }           
    $html = htmlspecialchars($data);
    echo "<pre>$html</pre>";

    echo $cont;?>

结果我有一个空页面:(请帮助我。

4

1 回答 1

1

您提供的链接具有 302 重定向,因此您不会获得时间表,您必须遵循重定向才能获得它。

更具体地说,查看“位置”标题,并使用该位置。

于 2012-06-05T14:43:25.030 回答