簡體   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