好吧,我有这个问题。我需要通过php cUrl从此site恢复XML,但要访问该页面,您需要接受这些条件。该表单设置会话cookie以便您查看页面。 请注意,网址中的日期将始终为今天。
所以,基于我在这里找到的各种答案,我试过这个:
$date = str_replace('-', '', $date);
$url = sprintf('/It/WebServerDataStore/MGP_Prezzi/%sMGPPrezzi.xml', $date);
$referer = 'http://www.mercatoelettrico.org/It/Tools/Accessodati.aspx';
$cookie_url = $referer.'?ReturnUrl='.urlencode($url);
$fields = array(
'__EVENTARGUMENT' => '',
'__EVENTTARGET' => '',
'__EVENTVALIDATION' => 'URcvJT7S63dA1s-LdHHlLeO8gsGKud_gH2eqN_7x2Ie6_-UzuvpCNUMasCGtb99phVmf0yd092c2jZxkBS1THCExM6f9n5r1dTeObSF1pzw_E05e0',
'__LASTFOCUS' => '',
'__PREVIOUSPAGE' => '',
'__SCROLLPOSITIONX' => '0',
'__SCROLLPOSITIONY' => '0',
'__VIEWSTATE' => '/wEPDwULLTIwNTEyNDQzNzQPZBYCZg9kFgICAw9kFgICAQ9kFgQCCw9kFgJmD2QWAmYPZBYCAgkPD2QWAh4Kb25rZXlwcmVzcwUccmV0dXJuIGludmlhUFdEKHRoaXMsZXZlbnQpO2QCEw9kFgICAQ8PFgIeDU9uQ2xpZW50Q2xpY2sFJmphdmFzY3JpcHQ6d2luZG93Lm9wZW4oJz9zdGFtcGE9dHJ1ZScpZGQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgUFDGN0bDAwJEltYWdlMQUSY3RsMDAkSW1hZ2VCdXR0b24xBSBjdGwwMCRDb250ZW50UGxhY2VIb2xkZXIxJHN0YW1wYQUkY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyMSRDQkFjY2V0dG8xBSRjdGwwMCRDb250ZW50UGxhY2VIb2xkZXIxJENCQWNjZXR0bzKRA5Gv2mMoekFfniqEnQ4Ab7DzaQ==',
'ctl00$ContentPlaceHolder1$Button1' => 'Accetto',
'ctl00$ContentPlaceHolder1$CBAccetto1' => 'on',
'ctl00$ContentPlaceHolder1$CBAccetto2' => 'on',
'ctl00$Password' => '',
'ctl00$UserName' => '',
'ctl00$tbTitolo' => 'cerca nel sito'
);
//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
$cookie_file = getcwd().'/files/cookie.txt';
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_COOKIESESSION,true);
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);
curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_REFERER, $referer);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
但是我得到一个只显示运行时错误的页面。 这些字段是条件页面发送的字段(我使用Firebug Net面板得到它们),每次接受条款时我都检查过它们是一样的。
有人可以指出我做错了吗?