POST后从页面获取HTML

时间:2014-01-14 10:59:55

标签: php jquery html symfony post

我想使用Symfony2的DOMCrawler从页面中提取数据。这是我想要从中获取数据的页面: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' => ''
));

$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);

但是html仍然没有改变,它仍然是没有帖子的第一页..

更新2:这就是我现在所拥有的:

$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 :(得分:2)

您必须使用file_get_contents的上下文参数并传递stream context object来发送帖子请求。

$post = http_build_query(array(
    'ctl00$ContentPlaceHolder1$ddlGeslacht' => '...',
    'ctl00$ContentPlaceHolder1$ddlReeks' => '...',
    // ...
));

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

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