繁体   English   中英

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

[英]Making POST request using PHP Curl not working

我已经阅读了此解决方案,但我认为这不是我需要的- 使用Curl发出POST请求,但无法正常工作

我会从api.php发布它,但是时间很长。

http://pastebin.com/8srk4nUu

这是来自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,但没有任何区别

查看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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM