PNR Status Script using PHP, jQuery and Ajax
Table of Contents
PNR Status Script:
Update: Demo may not work as expected, because the testing key provided by the “railwayapi.com” got expired.
You can create your own key and substitute that in the script and run to get the expected results / PNR status.
Are you planning to develop any PNR alert, status website or any mobile apps ? (or) planning to integrate the PNR status script to your existing website. ?
We have provided here how to get the PNR status programmatically using third party API’s and provided the complete source code of the PNR Status Script.
we have developed PNR status script using PHP, jQuery and Ajax and also, JSON and Bootstrap 3.
PNR Status Free API’s:
2. Rail PNR API
3. Railway API
4. Mashape
<div> <figure><a href="http://pnr.ngdeveloper.com" target="_blank" rel="noopener"><img class="alignright size-full wp-image-2470" src="http://www.ngdeveloper.com/wp-content/uploads/2014/11/demo1.png" alt="demo" width="181" height="63"></a></figure><div style="float: right;"></div> <figure><a href="http://ngdeveloper.com/subscriptions/index.php?postDwnId=4" target="_blank" rel="noopener"><img class="alignright wp-image-2473 size-full" src="http://www.ngdeveloper.com/wp-content/uploads/2014/11/download1.png" alt="download" width="181" height="63"></a></figure><div style="float: right;"></div> </div>
PNR status using Java Program, now you can create your own API for pnr status,
Requirement:
Railway API Key [You can register here and get the API key to your email.]
Railway API :
REST API ENDPOINT:
http://api.railwayapi.com/pnr_status/pnr/<pnr_no>/apikey/ENTER_YOUR_API_KEY_HERE/
REST API ENDPOINT with sample PNR and API KEY:
Your final railwayapi will look similar to this:
http://api.railwayapi.com/pnr_status/pnr/4728629631/apikey/7pqu19289191/
- 7pqu19289191 – Railway API key and
- 4728629631 – 10 digit PNR number
Required Imports:
Bootstrap JS and CSS [We have used Bootstrap to make it responsive].
BlockUI.js [to block the UI when AJAX method called and to display the Please Wait… message]
HTML for PNR Text box and search button
<div class="search"> <div class="container"> <div class="row"> <div class="col-md-10 col-md-offset-1 search-header"> <div class="form-section searchDiv"> <div class="row"> <div class="col-md-10"> <div class="form-group"> <input type="text" class="form-control" id="PNR_NUM" placeholder="Enter the 10digit PNR Number" > </div> </div> <div class="col-md-2"> <button type="button" class="btn btn-default btn-primary" onclick="loadPNRStatus();">Get PNR Status</button> </div> </div> </div> </div> </div> </div> </div>
Scripts:
This is where we have main business logic to pass the 10digit PNR Number through AJAX call to the Railway API and get the PNR status without refreshing the page.
AJAX call script to the End point:
function loadPNRStatus(){ var searchN = $('#PNR_NUM').val(); if(searchN !=""){ $.ajax( { type: "POST", url: "getPNRStatus.php", data:{pnrQ: searchN }, beforeSend: function() { $.blockUI({ css: { border: 'none', padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: .5, color: '#fff' } }); }, success:function(htmlResp) { $('div.pnrStatusLoad').html(htmlResp); $.unblockUI(); } }); } }
Validation script:
This below script accepts only the numbers and on enter to starts fetching the PNR status.
// if enter key pressed after pasting/typing PNR status Ajax method will be called $('#PNR_NUM').on('keypress', function (event) { if(event.which === 13){ loadPNRStatus(); } // return false if user enters other than numbers if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) { //display error message return false; } });
getPNRStatus.php: To get the current status of the given PNR.
<?php $json = ''; if(isset($_POST['pnrQ'])){ $searchParam = $_POST['pnrQ']; $replacedStr = str_replace(" ","",$searchParam); $url = 'http://api.railwayapi.com/pnr_status/pnr/'.$replacedStr.'/apikey/YOUR_RAILWAY_API_KEY/'; $content = file_get_contents($url); $json = json_decode($content, true); }else{ echo "something went wrong, please notify to admin [Naveen@ngdeveloper.com]"; } $dateOfJourney = $json['doj']; $pnr = $json['pnr']; $fromStation = $json['from_station']['name']; $fromStationCd = $json['from_station']['code']; $toStation = $json['to_station']['name']; $toStationCd = $json['to_station']['code']; $trainNum = $json['train_num']; $trainName = $json['train_name']; $currStatus = $json['passengers'][0]['current_status']; $bookingStatus = $json['passengers'][0]['booking_status']; $isChartPrep = $json['chart_prepared']; $isChartPrepd = ($isChartPrep=='N') ? 'NO' : 'YES'; $cls = $json['class']; $totPass = $json['total_passengers']; ?> <div class="col-md-6 col-md-offset-3"> <div class="row"> <li class="col-md-6 list-group-item " style="font-size:20px;text-align:center;"> PNR </li> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $pnr; ?></li> </div> <div class="row"> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> Date of Journey</li> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $dateOfJourney; ?></li> </div> <div class="row"> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> From </li> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $fromStation; ?> [<?php echo $fromStationCd; ?>]</li> </div> <div class="row"> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> TO</li> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $toStation; ?> [<?php echo $toStationCd; ?>]</li> </div> <div class="row"> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> Train Number</li> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $trainNum; ?></li> </div> <div class="row"> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> Train Name</li> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $trainName; ?></li> </div> <div class="row"> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> Booking Status</li> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $bookingStatus; ?></li> </div> <div class="row"> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;"> Current Status</li> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $currStatus; ?></li> </div> <div class="row"> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;">Chart Prepared</li> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $isChartPrepd; ?></li> </div> <div class="row"> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;">Class</li> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $cls; ?></li> </div> <div class="row"> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;">Total Passengers</li> <li class="col-md-6 list-group-item" style="font-size:20px;text-align:center;padding-left:30px;"><?php echo $totPass; ?></li> </div> </div>
Styles to make the page more neat:
<style type="text/css"> @media (max-width: 480px) { body { padding-bottom: 40px; } .carousel { height: 200px !important; margin-bottom: 60px; } .carousel .item { height: 200px !important; background-color: #777; } .carousel-inner > .item > img { position: absolute; top: 0; left: 0; min-width: 100%; height: 200px !important; } .search { margin-top: -25%; } .search .form-section{ background:rgba(0,0,0,0.6); border: 2px solid #414141; border-radius: 5px; padding: 10px; } } body { padding-bottom: 40px; } .carousel { height: 400px; margin-bottom: 60px; } .carousel .item { height: 400px; background-color: #777; } .carousel-inner > .item > img { position: absolute; top: 0; left: 0; min-width: 100%; height: 400px; } .search { margin-top: -25%; } .search .form-section{ background:rgba(0,0,0,0.6); border: 2px solid #414141; border-radius: 5px; padding: 10px; } </style>
undefined offset on line 20 and 21
elaborate clearly to understand and help you in better way….
just wondering if this is possible using jsonp or $.getJSON
this code not working for me
http://friendsvella.com/PNRStatus/
I downloaded the code and inserted my own api key in the php file and ran the index.html file. Once i enter the pnr no and press ‘Get PNR Status’ the page only shows ‘Please Wait..’ and doesn’t proceed further. Any idea ?
Please check the same in http://pnr.ngdeveloper.com/ and let me know whether it’s working or not with some sample PNR number.
Actually it was a silly mistake, i forgot to run it in xampp. It’s working fine now. My bad. Thank you very much 🙂
can you provide me whole text file of this……….
Hi I already shared all the files in the download[zip file], can you let me know what issue you are facing to help you better.
i am working on a project as described below:
Build an App or Web service to integrate any available map service (such as Google Maps, Bing map, Wiki Map etc) with Indian Rail Enquiry service. The following objectives are to be obtained:
i) A user can specify a Source place and Destination Place on the map. Your system should display the list of trains between the two places. If the places chosen do not have a train source or destination then use the nearest place that has a rail station. You can directly show the result from http://www.indianrail.gov.in
ii) Given a train number the route of the train should be shown on the map. The name of the source and destination with the departure and arrival time also to be shown.
i have done the google map part but stuck on the second part the train number one…………as i dont know know much about scripting and api key usage……….. can you help me with this………..
Hey I am trouble using the api.. what I have to write in place of name_to_code I am always getting a 404 error?
If you are facing any issue with the API[Railway API], kindly reach them and get it resolved. If any issue with the downloaded script, let me know to resolve it.
Hi Naveen
Very nice tutorial. I use this in my website http://www.statushub.in/check-pnr-status/
Could u explain any other tutorials like seat availability or trains between stations because it has more than one input and more than one responses..
Unable to download the zip files please provide me the link
Issue resolved. You can download the zip files now.
SIR,
Help me to where i place this code and apikey in my site