1
+ <?php namespace Swarajsaaj \Pnr ;
2
+
3
+ class Pnr {
4
+
5
+
6
+
7
+ function array2json ($ arr ) {
8
+ if (function_exists ('json_encode ' )) return json_encode ($ arr ); //Lastest versions of PHP already has this functionality.
9
+ $ parts = array ();
10
+ $ is_list = false ;
11
+
12
+ //Find out if the given array is a numerical array
13
+ $ keys = array_keys ($ arr );
14
+ $ max_length = count ($ arr )-1 ;
15
+ if (($ keys [0 ] == 0 ) and ($ keys [$ max_length ] == $ max_length )) {//See if the first key is 0 and last key is length - 1
16
+ $ is_list = true ;
17
+ for ($ i =0 ; $ i <count ($ keys ); $ i ++) { //See if each key correspondes to its position
18
+ if ($ i != $ keys [$ i ]) { //A key fails at position check.
19
+ $ is_list = false ; //It is an associative array.
20
+ break ;
21
+ }
22
+ }
23
+ }
24
+
25
+ foreach ($ arr as $ key =>$ value ) {
26
+ if (is_array ($ value )) { //Custom handling for arrays
27
+ if ($ is_list ) $ parts [] = array2json ($ value ); /* :RECURSION: */
28
+ else $ parts [] = '" ' . $ key . '": ' . array2json ($ value ); /* :RECURSION: */
29
+ } else {
30
+ $ str = '' ;
31
+ if (!$ is_list ) $ str = '" ' . $ key . '": ' ;
32
+
33
+ //Custom handling for multiple data types
34
+ if (is_numeric ($ value )) $ str .= $ value ; //Numbers
35
+ elseif ($ value === false ) $ str .= 'false ' ; //The booleans
36
+ elseif ($ value === true ) $ str .= 'true ' ;
37
+ else $ str .= '" ' . addslashes ($ value ) . '" ' ; //All other things
38
+ // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
39
+
40
+ $ parts [] = $ str ;
41
+ }
42
+ }
43
+ $ json = implode (', ' ,$ parts );
44
+
45
+ if ($ is_list ) return '[ ' . $ json . '] ' ;//Return numerical JSON
46
+ return '{ ' . $ json . '} ' ;//Return associative JSON
47
+ }
48
+
49
+
50
+
51
+ //Function to Retrieve the data
52
+ function makeWebCall ($ urlto ,$ postData = null ,$ refer =null ){
53
+
54
+ //create cURL connection
55
+ $ curl_connection = curl_init ($ urlto );
56
+ //set options
57
+ curl_setopt ($ curl_connection , CURLOPT_CONNECTTIMEOUT , 30 );
58
+ curl_setopt ($ curl_connection , CURLOPT_USERAGENT ,
59
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) " );
60
+ curl_setopt ($ curl_connection , CURLOPT_RETURNTRANSFER , true );
61
+ curl_setopt ($ curl_connection , CURLOPT_SSL_VERIFYPEER , false );
62
+ curl_setopt ($ curl_connection , CURLOPT_FOLLOWLOCATION , true );
63
+ //if
64
+ if (isset ($ postData )){
65
+ //set data to be posted
66
+ curl_setopt ($ curl_connection , CURLOPT_POST ,true );
67
+ curl_setopt ($ curl_connection , CURLOPT_POSTFIELDS , $ postData );
68
+ }
69
+
70
+
71
+ if (isset ($ refer )){
72
+ //set referer
73
+ curl_setopt ($ curl_connection , CURLOPT_REFERER , $ refer );
74
+ }
75
+ //perform our request
76
+ $ result = curl_exec ($ curl_connection );
77
+ // Debug -Data
78
+ //show information regarding the request
79
+ //var_dump(curl_getinfo($curl_connection));
80
+ // echo curl_errno($curl_connection) . '-' .
81
+ // curl_error($curl_connection);
82
+
83
+ //close the connection
84
+ curl_close ($ curl_connection );
85
+ return $ result ;
86
+ }
87
+
88
+
89
+ // Function to construct the post request
90
+ function createPostString ($ postArray ){
91
+ //traverse array and prepare data for posting (key1=value1)
92
+ foreach ( $ postArray as $ key => $ value ) {
93
+ $ post_items [] = $ key . '= ' . $ value ;
94
+ }
95
+ //create the final string to be posted using implode()
96
+ return implode ('& ' , $ post_items );
97
+ }
98
+
99
+
100
+ function request ($ pnr )
101
+ {
102
+
103
+ // KEY Datatype Mandatory Description
104
+ //-------------------------------------------------------
105
+ // pnrno Integer(10) true PNR number to be fetched 10 digit
106
+ // rtype String(XML/JSON) false Return type format
107
+ // callback String false Support for JSONP only supported for GET
108
+ $ pnt_no = $ pnr ;
109
+
110
+ $ url_captch = 'http://www.indianrail.gov.in/pnr_Enq.html ' ;
111
+ $ url_pnr = 'http://www.indianrail.gov.in/cgi_bin/inet_pnstat_cgi_10521.cgi ' ;
112
+ // Submit the captcha and PNR
113
+ //create array of data to be posted
114
+ $ post_data ['lccp_pnrno1 ' ] = $ pnt_no ;
115
+ $ post_data ['lccp_cap_val ' ] = 12345 ; //dummy captcha
116
+ $ post_data ['lccp_capinp_val ' ] = 12345 ;
117
+ $ post_data ['submit ' ] = "Get Status " ;
118
+ $ post_string = $ this ->createPostString ($ post_data );
119
+
120
+ $ result = $ this ->makeWebCall ($ url_pnr ,$ post_string ,$ url_captch );
121
+
122
+ //Debug
123
+ //var_dump($result);
124
+ // Parse Logic
125
+ // I have not used DOM lib it is simple regEx parse.
126
+ //Change here when the Page layout of the page changes.
127
+ $ matches = array ();
128
+ preg_match_all ('/<td class="table_border_both">(.*)<\/td>/i ' ,$ result ,$ matches );
129
+ //DEBUG
130
+ //var_dump($matches);
131
+ $ resultVal = array (
132
+ 'status ' => "INVALID " ,
133
+ 'data ' => array ()
134
+ );
135
+
136
+ if (count ($ matches )>1 &&count ($ matches [1 ])>8 ) {
137
+ $ arr = $ matches [1 ];
138
+ $ i =0 ;
139
+ $ j =0 ;
140
+ $ tmpValue =array (
141
+ "pnr " => $ pnt_no ,
142
+ "train_name " => "" ,
143
+ "train_number " => "" ,
144
+ "from " => "" ,
145
+ "to " => "" ,
146
+ "reservedto " => "" ,
147
+ "board " => "" ,
148
+ "class " => "" ,
149
+ "travel_date " => "" ,
150
+ "passenger " => array ()
151
+ );
152
+
153
+ $ tmpValue ['train_number ' ] = $ arr [0 ];
154
+ $ tmpValue ['train_name ' ] = $ arr [1 ];
155
+ $ tmpValue ['travel_date ' ] = $ arr [2 ];
156
+ $ tmpValue ['from ' ] = $ arr [3 ];
157
+ $ tmpValue ['to ' ] = $ arr [4 ];
158
+ $ tmpValue ['reservedto ' ] = $ arr [5 ];
159
+ $ tmpValue ['board ' ] = $ arr [6 ];
160
+ $ tmpValue ['class ' ] = $ arr [7 ];
161
+ $ stnum ="" ;
162
+ foreach ($ arr as $ value ) {
163
+
164
+ $ i ++;
165
+ if ($ i >8 ){
166
+ $ value =trim (preg_replace ('/<B>/ ' , '' , $ value ));
167
+ $ value =trim (preg_replace ('/<\/B>/ ' , '' , $ value ));
168
+
169
+ $ ck =$ i %3 ;
170
+ if ($ ck ==1 ){
171
+ $ stnum = $ value ;
172
+ }
173
+ else if ($ ck ==2 ) {
174
+ array_push ($ tmpValue ["passenger " ],array (
175
+ "seat_number " => $ stnum ,
176
+ "status " => $ value
177
+ ));
178
+ }
179
+ }
180
+ }
181
+ $ resultVal ['data ' ] = $ tmpValue ;
182
+ $ resultVal ['status ' ] = 'OK ' ;
183
+ }
184
+
185
+
186
+ $ jsondata = $ this ->array2json ($ resultVal );
187
+ if (array_key_exists ('callback ' , $ _GET )){
188
+
189
+ header ('Content-Type: text/javascript; charset=utf8 ' );
190
+ header ('Access-Control-Max-Age: 3628800 ' );
191
+ header ('Access-Control-Allow-Methods: GET, POST, PUT, DELETE ' );
192
+
193
+ $ callback = $ _GET ['callback ' ];
194
+ echo $ callback .'( ' .$ jsondata .'); ' ;
195
+
196
+ }else {
197
+ // normal JSON string
198
+ header ('Content-Type: application/json; charset=utf8 ' );
199
+
200
+ return $ jsondata ;
201
+ }
202
+
203
+
204
+
205
+ }
206
+
207
+ }
0 commit comments