Reputation: 65
I am trying to use cURL to get train information from http://www.indianrail.gov.in/know_Station_Code.html .
I have the following PHP code :
<?php
$fields = array(
'lccp_src_stncode_dis'=>'MANGAPATNAM-+MUM',
'lccp_src_stncode'=>'MUM',
'lccp_dstn_stncode_dis'=>'AMBALA+CITY-+UBC',
'lccp_dstn_stncode'=>'UBC',
'lccp_classopt'=>'SL',
'lccp_day'=>'17',
'lccp_month'=>'8',
'CurrentMonth'=>'7',
'CurrentDate'=>'17',
'CurrentYear'=>'2015'
);
$fields_string = ''; //defining an empty string
foreach($fields as $key=>$value) {
$temp = $key.'='.$value.'&';
$fields_string.$temp;
}
rtrim($fields_string,'&'); //removing the last '&' from the generated string
$curl = curl_init('http://www.indianrail.gov.in/cgi_bin/inet_srcdest_cgi_date.cgi');
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$fields_string);
$result = curl_exec($curl);
var_dump($result);
?>
The problem is that I get this output on my browser window :
boolean false
I tried using var_dump(curl_error($curl))
and I got the following output :
string 'Empty reply from server' (length=23)
Thanks for any help.
Upvotes: 1
Views: 2796
Reputation: 3771
Solution :
$fields = array(
'lccp_src_stncode_dis'=>'MANGAPATNAM-+MUM',
'lccp_src_stncode'=>'MUM',
'lccp_dstn_stncode_dis?'=>'AMBALA+CITY-+UBC',
'lccp_dstn_stncode'=>'UBC',
'lccp_classopt'=>'SL',
'lccp_day'=>'17',
'lccp_month'=>'8',
'CurrentMonth'=>'7',
'CurrentDate'=>'17',
'CurrentYear'=>'2015'
);
$fields_string = ''; //defining an empty string
foreach($fields as $key=>$value) {
$temp = $key.'='.urlencode($value).'&'; // urlencode
$fields_string.= $temp; // equal sign
}
rtrim($fields_string, '&');
$header = array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Accept-Language: en-us;q=0.8,en;q=0.6',
'Content-Type: application/x-www-form-urlencoded'
);
$curl = curl_init('http://www.indianrail.gov.in/cgi_bin/inet_srcdest_cgi_date.cgi');
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_HTTPHEADER,$header); //server require
curl_setopt($curl, CURLOPT_REFERER, 'http://www.indianrail.gov.in/know_Station_Code.html'); //server require
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 6.1; en; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13'); //server require
curl_setopt($curl,CURLOPT_POSTFIELDS,$fields_string);
$result = curl_exec($curl);
//close connection
curl_close($curl);
echo $result;
Upvotes: 2
Reputation: 318
There are a few ways to solve this problem.
1. cURL:
I believe what you were missing is http_build_query()
. This internally converts an array into a HTTP query format. Like this:
$fields = array(
'lccp_src_stncode_dis'=>'MANGAPATNAM-+MUM',
'lccp_src_stncode'=>'MUM',
'lccp_dstn_stncode_dis'=>'AMBALA+CITY-+UBC',
'lccp_dstn_stncode'=>'UBC',
'lccp_classopt'=>'SL',
'lccp_day'=>'17',
'lccp_month'=>'8',
'CurrentMonth'=>'7',
'CurrentDate'=>'17',
'CurrentYear'=>'2015'
);
$post_fields = http_build_query($fields);
$ch = curl_init($API_URL);
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_TIMEOUT, 30);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
curl_setopt($ch, CURLOPT_ENCODING , "gzip, deflate");
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
print_r(response);
2. Postman
This is one of the best tools for the problem you have. here is how this works:
Install Postman on your chrome browser. you can get it from the chrome store. Get the postman Interceptor from the same chrome store.
Once you have these two, follow these steps to get your PHP code for all the requests your browser makes:
1) Turn on the Interceptor
2) Open the Postman App:
3) Turn on the request capturing feature
4) Make your call on the Indian railways website
5) You'll get your Entire call, including headers and the post fields capturen in postman.
6) Click on Generate Code button and choose PHP -> cURL. You'll get th PHP code that makes the exact same request as the browser.
You can copy this code into the clipboard and use it as you wish.
3. Use a Library
There are libraries that you can use which handles all the errors. Guzzle is one such framework. You can find it's documentation here
Hope it helps! :)
Upvotes: 2