-1

I want to make a POST request to get schedule of a train number from this page: http://www.indianrail.gov.in/train_Schedule.html

I'm using this code but it is NOT working. The resulting error shown in attachment below. What could be the problem?
PS: I've got the cgi path and arguments as in $data using Firebug

$data = array('lccp_trnname' => '14553', 'getIt' => 'Get Schedule');

    $ch = curl_init();
    $useragent= "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1";
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent); // set user agent
    curl_setopt($ch, CURLOPT_URL, 'http://www.indianrail.gov.in/cgi_bin/inet_trnnum_cgi.cgi');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

    $txResult = curl_exec($ch);

    print "$txResult\n";

enter image description here:

3
  • indianrail.gov.in use in your site is illegal .and your URL is also wrong.FAILED IN CHK ENV 0 erro ris there Commented Mar 14, 2012 at 10:43
  • Added referer ("indianrail.gov.in/train_Schedule.html") to your code too and this does not seem to help. Can't see any form values missed... they have put protection in place to stop external requests though. I would recommend finding a different (more open) data source. Commented Mar 14, 2012 at 10:47
  • @Ing got it; please see below. Commented Mar 14, 2012 at 11:04

2 Answers 2

1

Assuming you have permission to programatically consume the data on that site......

The problem is the way you pass the post data to cURL.

CURLOPT_POSTFIELDS

The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, files thats passed to this option with the @ prefix must be in array form to work.

http://php.net/manual/en/function.curl-setopt.php

Try encoding the post data yourself and passing it as a string:

$data = array('lccp_trnname' => '14553', 'getIt' => 'Get Schedule');

$fields_string = '';
foreach($data as $key=>$value) { $fields_string .= urlencode($key).'='.urlencode($value).'&'; }
rtrim($fields_string,'&');

Then set the option:

curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
Sign up to request clarification or add additional context in comments.

Comments

0

You need two things:

a referrer
an "application/x-www-form-urlencoded" POST data

$params = 'lccp_trnname='.urlencode('14553').'&getIt='.urlencode('Get Schedule');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch,CURLOPT_REFERER, "http://www.indianrail.gov.in/train_Schedule.html");

1 Comment

Brilliant! The third line is not required. The first line did it. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.