<?PHP
//set POST variables
$url = 'http://cbseresults.nic.in/class12/cbse122014_total.asp';
$fields = array(
'regno' => '6600001',
'B1' => 'Submit',
'FrontPage_Form1' => 'Submit',
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string[]= $key.'='.$value;}
$fields_items = implode ('&', $fields_string);
//open connection
$ch = curl_init();
//fname%3Dasdf%26lname%3Dsdafasdf%26lolz%3DSubmit
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_items);
//curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
//curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36');
//execute post
$result = curl_exec($ch);
echo "<br>Field Item= "."<br>";
echo $result."=Result"."</br>";
echo curl_errno($ch) . '<br>' .
curl_error($ch);
//close connection
curl_close($ch);
?>
上面给出的代码将发布请求发送到链接http://cbseresults.nic.in/class12/cbse122014_total.asp,它应显示给定的roll-number(regno)的结果,但显示访问被拒绝.
代码位于http://computerinfo.in/school/test2.php.
请告诉我问题出在哪里或如何解决?
解决方法:
删除额外的参数
‘FrontPage_Form1’ => ‘Submit’,
从$fields数组中获取有效请求.您的数组应为:
$fields = array(
'regno' => '6600001',
'B1' => 'Submit'
);
您还需要指定引荐来源网址,即:
curl_setopt($ch, CURLOPT_REFERER, 'http://cbseresults.nic.in/class12/cbse122014_total.htm');
注意:
通常,我建议您在尝试之前先咨询他们,因为似乎他们在极力地拒绝像您这样的外部请求,您可能因此违反了法律或免责声明.