如何从站点获取__EVENTVALIDATION和__VIEWSTATE

时间:2014-01-15 11:57:43

标签: c# php asp.net symfony dom

我想从DOMCrawler为Symfony 2的页面中提取数据。这是我想要从中获取数据的页面:http://kovv.mavari.be/kalender.aspx

但是我想在帖子之后点击'zoek'(下拉列表中没有参数)。那是我想要的页面!起初我有:$html = file_get_contents("http://kovv.mavari.be/kalender.aspx");。但显然只会在没有帖子的情况下加载第一页。

这就是我现在所拥有的:

$post = http_build_query(array(
            'ctl00_ContentPlaceHolder1_ddlGeslacht' => 'Heren',
            'ctl00$ContentPlaceHolder1$ddlReeks'    => '',
            'ctl00_ContentPlaceHolder1_ddlDatum'    => '',
            'ctl00$ContentPlaceHolder1$btnZoek:zoek'
));

$options= array('http' => array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $post
));

$context  = stream_context_create($options);

$html = file_get_contents('http://kovv.mavari.be/kalender.aspx', false, $context);

但是从my other Stack Overflow topic,我了解到我还必须发送__EVENTVALIDATION和__VIEWSTATE。但我不知道得到它们。我该如何解决这个问题? (在Google上搜索的一些关键词也很棒!)

这就是我现在所拥有的:

$url = "http://kovv.mavari.be/kalender.aspx";
$regs = array();

$cookies = '../src/VolleyScout/VolleyScoutBundle/Resources/doc/cookie.txt';

// Regular expressions to parse out the special ASP.NET
// values for __VIEWSTATE and __EVENTVALIDATION
$regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i';
$regexEventVal  = '/__EVENTVALIDATION\" value=\"(.*)\"/i';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data=curl_exec($ch);

$viewstate = $this->regexExtract($data,$regexViewstate,$regs,1);
$eventval = $this->regexExtract($data, $regexEventVal,$regs,1);

$postData = '__VIEWSTATE='
            . rawurlencode($viewstate)
            . '&__EVENTVALIDATION='.rawurlencode($eventval)
            . '&ctl00_ContentPlaceHolder1_ddlGeslacht=Heren'
            . '&ctl00$ContentPlaceHolder1$ddlReeks'
            . '&ctl00_ContentPlaceHolder1_ddlDatum'
            . '&ctl00$ContentPlaceHolder1$btnZoek:zoek'
;

curl_setOpt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);

curl_setOpt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);

$data = curl_exec($ch);

echo $data;

curl_close($ch);

但是我仍然没有帖子得到这个页面,我错过了什么?

1 个答案:

答案 0 :(得分:1)

HTTP是一种无状态协议,这意味着客户端和服务器没有内置的方法来跟踪应用程序从一个请求到下一个请求的状态。已经发明了各种技术来规避诸如cookie之类的技术。 ViewState和事件验证是ASP.NET使用的两种技术,可以为网页提供状态感。

有关详细信息,请参阅此link