拒绝访问:使用cURL php获取页面内容

时间:2014-11-08 00:00:07

标签: php curl

<?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,它应该显示给定滚动号码(regno)的结果,但它显示access denied。 代码在http://computerinfo.in/school/test2.php生效。 请告诉我问题出在哪里或我们如何排除故障?

1 个答案:

答案 0 :(得分:3)

删除额外参数

  

&#39; FrontPage_Form1&#39; =&GT; &#39;提交&#39;,

来自$fields数组的

应该提供有效请求。你的数组应该是:

$fields = array(
   'regno' => '6600001',
   'B1' => 'Submit'
);

您还需要指定引荐来源,即:

curl_setopt($ch, CURLOPT_REFERER, 'http://cbseresults.nic.in/class12/cbse122014_total.htm');

注意:

一般情况下,我建议您在尝试之前咨询他们,因为他们似乎很难拒绝像您这样的外部请求,而且您可能会违反法律或免责声明。