我已经仔细阅读了这个解决方案,但并不认为这是我需要的 - Making POST request using Curl, not working
这是来自api.php,我会发布,但它很长。
这是来自schedule.php
$pnt_no = isset($_POST['trnno'])? $_POST['trnno']:(isset($_GET['trnno'])?$_GET['trnno']:'');
$rtype = isset($_POST['rtype'])? $_POST['rtype']:(isset($_GET['rtype'])?$_GET['rtype']:'');
//create array of data to be posted
$post_data['lccp_trnname'] = $pnt_no;
$post_data['getIt'] = "Wait For PNR Enquiry!";
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection =
curl_init('http://www.indianrail.gov.in/cgi_bin/inet_trnnum_cgi.cgi');
//set option
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
//print_r(curl_getinfo($curl_connection));
//echo curl_errno($curl_connection) . '-' .
// curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
echo $result;
//$matches = array();
//preg_match_all('/<td>(.+?)<\/td>/',$result,$matches);
//var_dump($matches);
我不知道为什么它不能连接,除了http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi我不能存在,但是我尝试了http://www.indianrail.gov.in/cgi_bin/inet_pnstat_cgi_24335.cgi但它没有做到任何差异
答案 0 :(得分:1)
查看http_build_query,手动字符串构建时更好
您还可以使用$_REQUEST
数组进行GET和POST查询...
这是一个帖子请求的片段:
<?php
$post_data = array();
$pnt_no = isset($_REQUEST['trnno']) ? $_REQUEST['trnno']:'';
$rtype = isset($_REQUEST['rtype']) ? $_REQUEST['rtype']: '';
//create array of data to be posted
$post_data['lccp_trnname'] = $pnt_no;
$post_data['getIt'] = "Wait For PNR Enquiry!";
$url = 'http://www.indianrail.gov.in/cgi_bin/inet_trnnum_cgi.cgi';
$data = http_build_query($post_data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($post_data));
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
echo $result;//web page "Welcome to Indian Railway Passenger reservation Enquiry"
此外,我喜欢在没有卷曲的情况下进行查询:
<?php
$data = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data
)
);
$context = stream_context_create($data);
$result = file_get_contents($url, false, $context);
echo $result;