简体   繁体   English

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

[英]Access Denied: Using cURL php to fetch the content of a page

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

?>

Code given above sends post request to the link http://cbseresults.nic.in/class12/cbse122014_total.asp and it should show the result of given roll-number( regno ) but it showing access denied . 上面给出的代码将post请求发送到链接http://cbseresults.nic.in/class12/cbse122014_total.asp ,它应该显示给定roll-number( regno )的结果,但它显示access denied Code is live at http://computerinfo.in/school/test2.php . 代码现在在http://computerinfo.in/school/test2.php上 Kindly tell me where the problem is or how we can be troubleshoot? 请告诉我问题出在哪里或我们如何排除故障?

Removing that extra parameter 删除该额外参数

'FrontPage_Form1' => 'Submit', 'FrontPage_Form1'=>'提交',

from your $fields array should make for a valid request. 来自你的$fields数组应该提供有效的请求。 Your array should be: 你的数组应该是:

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

You also need to specify the referrer, ie: 您还需要指定引用者,即:

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

Note: 注意:

In general I'd suggest you consult with them before trying it out since it seems they are poorly trying to disallow external requests like yours and you could be breaking laws or disclaimers of usage through this. 一般情况下,我建议你在尝试之前咨询他们,因为他们似乎很难拒绝像你这样的外部请求,你可能会破坏法律或免责声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM