ott
ott

Reputation: 149

Making POST request using PHP Curl not working

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

Upvotes: 0

Views: 4067

Answers (1)

vp_arth
vp_arth

Reputation: 14992

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;

Upvotes: 1

Related Questions