dongmu6225 2014-04-11 12:59
浏览 232
已采纳

使用PHP Curl发出POST请求不起作用

I've read through this for a solution but don't think it's what I need - Making POST request using Curl, not working

this it from api.php I would post but it's long.

http://pastebin.com/8srk4nUu

this is from 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);

I don't know why it wont connect, other than http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi doesn't exist that I can see, but I tried with http://www.indianrail.gov.in/cgi_bin/inet_pnstat_cgi_24335.cgi but it didn't make any difference

  • 写回答

1条回答 默认 最新

  • douxu9707 2014-04-11 13:13
    关注

    look at http_build_query, it better when manually string building
    Also you can use $_REQUEST array for both GET and POST queries...

    Here a snippet for post request:

    <?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"
    

    Also I like to make queries without curl:

    <?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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!