我正在尝试使用cURL从http://www.indianrail.gov.in/know_Station_Code.html获取列车信息。
我有以下PHP代码:
<?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);
?>
问题是我在浏览器窗口中输出了这个输出:
boolean false
我尝试使用var_dump(curl_error($curl))
并获得以下输出:
string 'Empty reply from server' (length=23)
感谢您的帮助。
答案 0 :(得分:2)
有几种方法可以解决这个问题。
<强> 1。卷曲:强>
我相信你遗失的是http_build_query()
。这在内部将数组转换为HTTP查询格式。像这样:
$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。邮差强>
这是解决您遇到此问题的最佳工具之一。这是如何工作的:
在Chrome浏览器上安装Postman。你可以得到它https://github.com/jaredhanson/passport-instagram/blob/master/examples/login/app.js。 获得邮差拦截器from the chrome store。
获得这两个后,请按照以下步骤获取浏览器发出的所有请求的PHP代码:
1)打开拦截器
2)打开Postman App:
3)启用请求捕获功能
4)在印度铁路网站上打电话
5)您将收到整个电话,包括邮件中的标题和邮件字段。
6)点击生成代码按钮,然后选择PHP - &gt;卷曲。您将获得与浏览器完全相同的PHP代码。
您可以将此代码复制到剪贴板中并根据需要使用它。
第3。使用资料库
您可以使用哪些库来处理所有错误。 from the same chrome store就是这样一个框架。您可以找到它的文档Guzzle
希望它有所帮助! :)
答案 1 :(得分:2)
解决方案:
$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;