unable to pass the values to api pattern and get the response..

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1046 Views - Last Post: 14 December 2015 - 02:36 AM Rate Topic: -----

#1 livetrainstatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-December 15

unable to pass the values to api pattern and get the response..

Posted 12 December 2015 - 06:55 AM

Hi Friends,

I have two pages in my website index.php and trailtatus.php,when i pass the value from drop down to api pattern using php and json,it shows the trailstatus.php is empty page..

when i choose the train number and date in index.php then i press submit button it shows the empty result in the trailstatus.php page..kindly help me to resolve the problem.
our API Pattern will be lookks the below format:
API Pattern:
http://api.railwayap...apikey/<apikey/
index.php
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">

    <title>PNR Status </title>
  <head>
<body>
	<center>
<form action="trailstatus.php" method="get">
<tr>
<td>	Train Number:</td>
<td><input type="text" name ="train number"> </td> </br>
</tr>
<tr> 
<td> Select Date:</td>
<td><select>
  <option value="today"><?php echo date('Ymd', strtotime("today")); ?></option>
<option value="tomorrow"><?php echo date('Ymd', strtotime("tomorrow")); ?></option>
<option value="yesterday"><?php echo date('Ymd', strtotime("yesterday")); ?></option>
</select></td>
</tr>
<tr>
<td></br>
Submit:
</td>
<td><input type="submit" name ="submit">
</td>
</form>
</center>
  </body>
</html>



trailstatus.php
<!DOCTYPE html
         <html
         <body
         <?php
         $json = '';
         if(isset($_GET['trainnumber'] && isset($_GET['doj'] )){
         $searchParam1 = $_GET['trainnumber'];
         $searchParam2 = $_GET['doj'];
         $replacedStr = str_replace(" ","",$searchParam1);
         $replacedStr1 = str_replace(" ","",$searchParam2);
         $url = 'http://api.railwayapi.com/live/train/' $replacedStr.'/doj/' $replacedStr1.'/apikey/****/';
         $content = file_get_contents($url);
         $json = json_decode($content, true);
         }else{
          echo "something went wrong, please notify to admin [[email protected]]";
         }
         $station = $json['route'] ['station'];
         $scharr = $json['route'] ['scharr'];
         $actarr = $json['route'] ['actarr']; 
         $status= $json['route'] ['has_departed'] [0] ['latemin'];
         ?>
		 <table>
  <tr>
    <th>station</th>
    <th>Sch.Arrival</th>
    <th>Act.Arrival</th>
    <th>Status/delay</th>

  </tr>
  <tr>
    <td><?php echo $station;?></td>
    <td><?php echo $scharr;?></td>
    <td><?php echo $actarr;?></td>
    <td><?php echo $status;?></td>
  </tr>
</table>
         </body
         </html

Thanks
Sathya

This post has been edited by Dormilich: 12 December 2015 - 07:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: unable to pass the values to api pattern and get the response..

#2 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,102
  • Joined: 12-December 12

Re: unable to pass the values to api pattern and get the response..

Posted 12 December 2015 - 08:15 AM

$_GET['trainnumber'] Your element name has a space in it. It shouldn't, don't attempt to include a space in an element name.

$_GET['doj'] There is no form element of this name.

Your select has no name so no value will be submitted for it.

This HTML is broken:
<!DOCTYPE html
         <html
         <body

as it is at the end.

$json['route'] ['station'] I would not put spaces here. It may work, but there is no reason to do this.

This post has been edited by andrewsw: 12 December 2015 - 08:17 AM

Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,102
  • Joined: 12-December 12

Re: unable to pass the values to api pattern and get the response..

Posted 12 December 2015 - 08:22 AM

Note also that the center tag is obsolete, use CSS instead.
Was This Post Helpful? 1
  • +
  • -

#4 livetrainstatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-December 15

Re: unable to pass the values to api pattern and get the response..

Posted 12 December 2015 - 08:38 AM

Hi Andrews
I will update all the things what you have mentioned.thanks for your valuable time to answer my queries.
This is the best forum for discussion.
I will update and share you when it's working fine
Was This Post Helpful? 0
  • +
  • -

#5 livetrainstatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-December 15

Re: unable to pass the values to api pattern and get the response..

Posted 12 December 2015 - 10:05 PM

I have updated everything what you people have said but i am getting the error in logs as

Quote

PHP Parse error: syntax error, unexpected '$replacedStr'


still the next page trailstatus.php showing empty only..i have updated the trainnumber and doj field properly and both the places given same name only...
Was This Post Helpful? 0
  • +
  • -

#6 livetrainstatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-December 15

Re: unable to pass the values to api pattern and get the response..

Posted 12 December 2015 - 10:44 PM

i have updated the concatenate things too..but now next page showing the results as

Quote

something went wrong, please notify to admin [[email protected]]
station Sch.Arrival Act.Arrival Status/delay


i dont know whats the problem but error logs shows as

Quote

Uninitialized string offset: 0


if u know about the problem kindly help me tp resolve this.
Was This Post Helpful? 0
  • +
  • -

#7 livetrainstatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-December 15

Re: unable to pass the values to api pattern and get the response..

Posted 13 December 2015 - 01:51 AM

updated code :

when i enter the train number and doj then i pressed submit it shows only the below message..i am not getting the results.kindly help me to resolve this problem.

index.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any
other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">

    <title>PNR Status </title>
  <head>
<body>
    <center>
<form action="livetrainstatus.php" method="post">
<tr>
<td>    Train Number:</td>
<td><input type="text" name ="trainnumber"> </td> </br>
</tr>
<tr>
<td> Select Date:</td>
<td><select name="doj">
  <option value="today"><?php echo date('Ymd', strtotime("today")); ?></option>
<option value="tomorrow"><?php echo date('Ymd',
strtotime("tomorrow")); ?></option>
<option value="yesterday"><?php echo date('Ymd',
strtotime("yesterday")); ?></option>
</select></td>
</tr>
<tr>
<td></br>
Submit:
</td>
<td><input type="submit" value ="submit">
</td>
</form>
</center>
  </body>
</html>


livetrainstatus.php

<!DOCTYPE html>
<html>
 <head>
 <title>train status time table</title>
 </head>
<body>
<?php
$json = '';
if(isset($_GET['trainnumber']) && (isset($_GET['doj'] ) ))
{
$searchParam1 = $_GET['trainnumber'];
$searchParam2 = $_GET['doj'];
$replacedStr = str_replace(" ","",$searchParam1);
$replacedStr1 = str_replace(" ","",$searchParam2);
$url ="http://api.railwayapi.com/live/train/' .$replacedStr.'/doj/'
.$replacedStr1.'/apikey/****/";
$content = file_get_contents($url);
$json = json_decode($content, true);
}

else{
 echo "something went wrong, please notify to admin
[[email protected]]";
}
$station = $json['route']['station'];
$scharr = $json['route']['scharr'];
$actarr = $json['route']['actarr'];
?>
<table>
  <tr>
    <th>station</th>
    <th>Sch.Arrival</th>
    <th>Act.Arrival</th>
    <th>Status/delay</th>

  </tr>
  <tr>
    <td><?php echo $station;?></td>
    <td><?php echo $scharr;?></td>
    <td><?php echo $actarr;?></td>
  </tr>
</table>
</body>
</html>


PHP Error logs getting the error as :

Quote

"[Sun Dec 13 07:40:40 2015] [warn] [client 116.50.59.180] mod_fcgid:
stderr: PHP Notice: Uninitialized string offset: 0 in
/var/www/vhosts/livetrainrunningstatus.co.in/httpdocs/livetrainstatus.php
on line 25, referer: http://livetrainrunningstatus.co.in/ "


Error message displayed as the bloew after the form submission

Quote

something went wrong, please notify to admin [[email protected]]
station Sch.Arrival Act.Arrival Status/delay

This post has been edited by Dormilich: 13 December 2015 - 05:35 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4230
  • View blog
  • Posts: 13,413
  • Joined: 08-June 10

Re: unable to pass the values to api pattern and get the response..

Posted 13 December 2015 - 06:33 AM

you submit the form using POST, but try to get the variables via GET.

the PHP error log comes from $json being a string (line #8) but being treated as array (line #18) which never happened due to the condition.

This post has been edited by Dormilich: 13 December 2015 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

#9 livetrainstatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-December 15

Re: unable to pass the values to api pattern and get the response..

Posted 13 December 2015 - 08:44 AM

Thanks for your help.i will update and share it here.
I dont know why it's treated as array.

I want it should treat as string.can you help me this?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4230
  • View blog
  • Posts: 13,413
  • Joined: 08-June 10

Re: unable to pass the values to api pattern and get the response..

Posted 13 December 2015 - 08:45 AM

Quote

I dont know why it's treated as array.

because you coded it that way.
Was This Post Helpful? 0
  • +
  • -

#11 livetrainstatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-December 15

Re: unable to pass the values to api pattern and get the response..

Posted 13 December 2015 - 11:05 PM

I have updated the form fields in index.php as GET method.
still i am getting the same error due to the string treated as array.any solution or suggestion pls how to make it work?
Was This Post Helpful? 0
  • +
  • -

#12 livetrainstatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-December 15

Re: unable to pass the values to api pattern and get the response..

Posted 13 December 2015 - 11:53 PM

Actually i have to get the values using the array and not the string ..it mistake by me.just now go through the json response..
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4230
  • View blog
  • Posts: 13,413
  • Joined: 08-June 10

Re: unable to pass the values to api pattern and get the response..

Posted 14 December 2015 - 01:08 AM

View Postlivetrainstatus, on 14 December 2015 - 07:05 AM, said:

still i am getting the same error due to the string treated as array.

then you still donít enter the if part of the condition.
Was This Post Helpful? 0
  • +
  • -

#14 livetrainstatus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-December 15

Re: unable to pass the values to api pattern and get the response..

Posted 14 December 2015 - 01:45 AM

Actually i have checked the person who gave the api request.the api request are processed perfectly but the problem is i am not used the proper parameters to get the required output from json response.

i dont know how to make the proper parameters for that..
the json response looks like this.

{
    "response_code": 200,
    "error": "",
    "position": "Train has reached Destination and late by 5 minutes.",
    "train_number": "12046",
    "route": [
        {
            "no": 1,
            "station": "CDG",
            "has_arrived": false,
            "has_departed": true,
            "day": 0,
            "distance": 0,
            "scharr": "Source",
            "schdep": "12:00",
            "actarr": "00:00",
            "actdep": "12:00",
            "scharr_date": "19 Nov 2015",
            "actarr_date": "19 Nov 2015",
            "latemin": 0
        },
        {
            "no": 2,
            "station": "UMB",
            "has_arrived": true,
            "has_departed": true,
            "day": 0,
            "distance": 67,
            "scharr": "12:40",
            "schdep": "12:42",
            "actarr": "12:40",
            "actdep": "12:42",
            "scharr_date": "19 Nov 2015",
            "actarr_date": "19 Nov 2015",
            "latemin": 0
        },
        {
            "no": 3,
            "station": "KUN",
            "has_arrived": true,
            "has_departed": true,
            "day": 0,
            "distance": 142,
            "scharr": "13:36",
            "schdep": "13:38",
            "actarr": "13:57",
            "actdep": "13:59",
            "scharr_date": "19 Nov 2015",
            "actarr_date": "19 Nov 2015",
            "latemin": 21
        },
        {
            "no": 4,
            "station": "NDLS",
            "has_arrived": true,
            "has_departed": false,
            "day": 0,
            "distance": 265,
            "scharr": "15:25",
            "schdep": "Destination",
            "actarr": "15:30",
            "actdep": "00:00",
            "scharr_date": "19 Nov 2015",
            "actarr_date": "19 Nov 2015",
            "latemin": 5
        }
    ]
}



kindly help me to solve this problem..
Thanks
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4230
  • View blog
  • Posts: 13,413
  • Joined: 08-June 10

Re: unable to pass the values to api pattern and get the response..

Posted 14 December 2015 - 01:49 AM

Quote

i dont know how to make the proper parameters for that..

http://www.railwayapi.com/tutorial/
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2