CyberBoy
CyberBoy

Reputation: 753

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. Code is live at http://computerinfo.in/school/test2.php. Kindly tell me where the problem is or how we can be troubleshoot?

Upvotes: 3

Views: 2708

Answers (1)

Kypros
Kypros

Reputation: 2986

Removing that extra parameter

'FrontPage_Form1' => 'Submit',

from your $fields array should make for a valid request. 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.

Upvotes: 3

Related Questions