Advertisement
otfromtot

Untitled

Apr 10th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.02 KB | None | 0 0
  1. //create array of data to be posted
  2. $post_data['lccp_pnrno1'] = $pnt_no;
  3. $post_data['submit'] = "Wait For PNR Enquiry!";
  4. //traverse array and prepare data for posting (key1=value1)
  5. foreach ( $post_data as $key => $value) {
  6.     $post_items[] = $key . '=' . $value;
  7. }
  8. //create the final string to be posted using implode()
  9. $post_string = implode ('&', $post_items);
  10.  
  11. //create cURL connection
  12. $curl_connection =
  13.   curl_init('http://www.indianrail.gov.in/cgi_bin/inet_pnstat_cgi_24335.cgi');
  14. //set options
  15. curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
  16. curl_setopt($curl_connection, CURLOPT_USERAGENT,
  17.   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
  18. curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
  19. curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
  20. curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
  21. //set data to be posted
  22. curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
  23. //perform our request
  24. $result = curl_exec($curl_connection);
  25. //show information regarding the request
  26. //print_r(curl_getinfo($curl_connection));
  27. //echo curl_errno($curl_connection) . '-' .
  28. //                curl_error($curl_connection);
  29. //close the connection
  30. curl_close($curl_connection);
  31.  
  32. $matches = array();
  33. preg_match_all('/<TD class="table_border_both">(.*)<\/TD>/',$result,$matches);
  34. //var_dump($matches);
  35. $resultVal = array(
  36.     'status'    =>    "INVALID",
  37.     'data'      =>    array()              
  38. );
  39.  
  40. if (count($matches)>1&&count($matches[1])>8) {
  41.  $arr = $matches[1];
  42.  $i=0;
  43.  $j=0;
  44.  $tmpValue =array(
  45.           "pnr" => $pnt_no,
  46.           "train_name" => "",
  47.           "train_number" => "",
  48.           "from" => "",
  49.           "to" => "",
  50.           "reservedto" => "",
  51.           "board" => "",
  52.           "class" => "",
  53.           "travel_date" => "",
  54.           "passenger" => array()
  55.  );
  56.  
  57.  $tmpValue['train_number'] = $arr[0];
  58.  $tmpValue['train_name'] = $arr[1];
  59.  $tmpValue['travel_date'] = $arr[2];
  60.  $tmpValue['from'] = $arr[3];
  61.  $tmpValue['to'] = $arr[4];
  62.  $tmpValue['reservedto'] = $arr[5];
  63.  $tmpValue['board'] = $arr[6];
  64.  $tmpValue['class'] = $arr[7];
  65.  $stnum="";
  66.  foreach ($arr as $value) {
  67.  
  68.   $i++;
  69.   if($i>8){
  70.    $value=trim(preg_replace('/<B>/', '', $value));
  71.    $value=trim(preg_replace('/<\/B>/', '', $value));
  72.    
  73.    $ck=$i%3;
  74.     if($ck==1){    
  75.      $stnum = $value;
  76.     }
  77.     else if($ck==2) {
  78.       array_push($tmpValue["passenger"],array(
  79.            "seat_number" => $stnum,
  80.            "status" => $value
  81.         ));
  82.     }
  83.   }
  84.  }
  85.  $resultVal['data'] = $tmpValue;
  86.  $resultVal['status'] = 'OK';
  87. }
  88.  
  89. function array2json($arr) {
  90.     if(function_exists('json_encode')) return json_encode($arr); //Lastest versions of PHP already has this functionality.
  91.     $parts = array();
  92.     $is_list = false;
  93.  
  94.     //Find out if the given array is a numerical array
  95.     $keys = array_keys($arr);
  96.     $max_length = count($arr)-1;
  97.     if(($keys[0] == 0) and ($keys[$max_length] == $max_length)) {//See if the first key is 0 and last key is length - 1
  98.         $is_list = true;
  99.         for($i=0; $i<count($keys); $i++) { //See if each key correspondes to its position
  100.             if($i != $keys[$i]) { //A key fails at position check.
  101.                 $is_list = false; //It is an associative array.
  102.                 break;
  103.             }
  104.         }
  105.     }
  106.  
  107.     foreach($arr as $key=>$value) {
  108.         if(is_array($value)) { //Custom handling for arrays
  109.             if($is_list) $parts[] = array2json($value); /* :RECURSION: */
  110.             else $parts[] = '"' . $key . '":' . array2json($value); /* :RECURSION: */
  111.         } else {
  112.             $str = '';
  113.             if(!$is_list) $str = '"' . $key . '":';
  114.  
  115.             //Custom handling for multiple data types
  116.             if(is_numeric($value)) $str .= $value; //Numbers
  117.             elseif($value === false) $str .= 'false'; //The booleans
  118.             elseif($value === true) $str .= 'true';
  119.             else $str .= '"' . addslashes($value) . '"'; //All other things
  120.             // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
  121.  
  122.             $parts[] = $str;
  123.         }
  124.     }
  125.     $json = implode(',',$parts);
  126.      
  127.     if($is_list) return '[' . $json . ']';//Return numerical JSON
  128.     return '{' . $json . '}';//Return associative JSON
  129. }
  130. // function defination to convert array to xml
  131. function array2xml($student_info, &$xml_student_info) {
  132.     foreach($student_info as $key => $value) {
  133.         if(is_array($value)) {
  134.             if(!is_numeric($key)){
  135.                 $subnode = $xml_student_info->addChild("$key");
  136.                 array2xml($value, $subnode);
  137.             }
  138.             else{
  139.                 array2xml($value, $xml_student_info);
  140.             }
  141.         }
  142.         else {
  143.             $xml_student_info->addChild("$key","$value");
  144.         }
  145.     }
  146. }
  147.  
  148. if($rtype=='XML'){
  149. $xmlresult = new SimpleXMLElement("<?xml version=\"1.0\"?><result></result>");
  150. array2xml($resultVal,$xmlresult);
  151. echo $xmlresult->asXML();
  152. }
  153. else
  154. echo array2json($resultVal);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement