1

I am using a API provided by this website

http://pnrapi.alagu.net/

By using this API, we can get PNR status of our indian railways.

I am using CURL to make a call and get the page content which is something like this, in an array format:

Array ( [url] => http://pnrapi.alagu.net/api/v1.0/pnr/4563869832 [content_type] => application/json;charset=utf-8 [http_code] => 200 [header_size] => 185 [request_size] => 130 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 2.906 [namelookup_time] => 0 [connect_time] => 0.312 [pretransfer_time] => 0.312 [size_upload] => 0 [size_download] => 548 [speed_download] => 188 [speed_upload] => 0 [download_content_length] => 548 [upload_content_length] => 0 [starttransfer_time] => 2.906 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => 50.57.204.234 [primary_port] => 80 [local_ip] => 192.168.1.10 [local_port] => 60105 [redirect_url] => [errno] => 0 [errmsg] => [content] => {"status":"OK","data":{"train_number":"16178","chart_prepared":false,"pnr_number":"4563869832","train_name":"ROCKFORT EXPRES","travel_date":{"timestamp":1369506600,"date":"26-5-2013"},"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},"class":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}} )

but when I go to the URL http://pnrapi.alagu.net/api/v1.0/pnr/4563869832 , it gives me output as shown below:

 {"status":"OK","data":{"train_number":"16178","chart_prepared":false,"pnr_number":"4563869832","train_name":"ROCKFORT EXPRES","travel_date":{"timestamp":1369506600,"date":"26-5-2013"},"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},"class":"2A","passenger":[{"seat_number":"W/L  39,RLGN","status":"W/L  27"}]}}

Now, it seems that output on my web page with curl have got some extra text which is in the start as you can see both the outputs above.

Well, my question is, how can I get the values from above array.

I am talking about the array output which I'm getting on my page using CURL, which looks like this:

Array ( 
[url] => http://pnrapi.alagu.net/api/v1.0/pnr/4563869832 
[content_type] =>     application/json;charset=utf-8 
[http_code] => 200 
[header_size] => 185 
[request_size] =>     130 
[filetime] => -1 
[ssl_verify_result] => 0 
[redirect_count] => 0 
[total_time] => 2.906     
[namelookup_time] => 0 
[connect_time] => 0.312 
[pretransfer_time] => 0.312 
[size_upload] => 0 
[size_download] => 548 
[speed_download] => 188 
[speed_upload] => 0 
[download_content_length] => 548 
[upload_content_length] => 0 
[starttransfer_time] => 2.906 
[redirect_time] => 0 
[certinfo] => Array ( ) 
[primary_ip] => 50.57.204.234 
[primary_port] => 80 
[local_ip] => 192.168.1.10 
[local_port] => 60105 
[redirect_url] => 
[errno] => 0 
[errmsg] => [content] => {"status":"OK","data":{"train_number":"16178","chart_prepared":false,"pnr_number":"4563869832","train_name":"ROCKFORT EXPRES","travel_date":{"timestamp":1369506600,"date":"26-5-2013"},"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},"class":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}} )

Code in my PHP page is:

    <?php
function get_web_page( $url )
{
$options = array(
    CURLOPT_RETURNTRANSFER => true,     // return web page
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_ENCODING       => "",       // handle all encodings
    CURLOPT_USERAGENT      => "spider", // who am i
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    CURLOPT_TIMEOUT        => 120,      // timeout on response
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
);

$ch      = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err     = curl_errno( $ch );
$errmsg  = curl_error( $ch );
$header  = curl_getinfo( $ch );
curl_close( $ch );

$header['errno']   = $err;
$header['errmsg']  = $errmsg;
$header['content'] = $content;
return $header;
}
$pnr = get_web_page('http://pnrapi.alagu.net/api/v1.0/pnr/4563869832');
echo "<code>";
print_r($pnr);
echo "</code>";
?>

I only need the values under "content" which are train number, train name, travel date etc.

So, what would be best way to extract this information into each variable?

Like I want it like this:

$train_no   = [some code];
$train_name = [some_code];

and so on...

Thanks in advance.

I tried this:

echo $pnr['content'];

and the output I got is:

{"status":"OK",    
"data":"train_number":"16178",
"chart_prepared":false,
"pnr_number":"4563869832",
"train_name":"ROCKFORT EXPRES",
"travel_date":{"timestamp":1369506600,"date":"26-5-2013"},
"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},
"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},
"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},
"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},
"class":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}}  

Now can any one give me an idea about how can I fetch unique values from above array?

2 Answers 2

1

I'm not sure where the JSON string is. But let's say it's the $pnr variable.

$json = json_decode($pnr, true);
$train_no = $json["data"]["train_number"];
$train_name = $json["data"]["train_name"];

Updated: If you don't need all the other things you can do something like the following:

$npr = file_get_contents(url);

and then run the code above.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, it gives me this warning: Warning: json_decode() expects parameter 1 to be string,
Updated my response. I'm not entirely sure what you need the get_web_page for.
Thank You BjarkeHS, Your code works perfect to some extent but Now I got this: Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\PNR\index.php on line 52 Any idea?
Cheers, I found out the solutions with minor tweak to your code. i tried this: $json = json_decode($pnr,true); Thank you so much! You are the man ;)
0

You're looking through the header, where you should be looking at the content. Return $content instead in your function and then you can parse out the response:

function get_web_page( $url ) {
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = json_decode( curl_exec( $ch ) );

    curl_close( $ch );

    return array(
        'train_no' => $content->data->train_number,
        'train_name' => $content->data->train_name,
    );
}

$pnr = get_web_page('http://pnrapi.alagu.net/api/v1.0/pnr/4563869832');

echo "<pre>" . print_r($pnr, true) . "</pre>";

2 Comments

You can also greatly reduce your options unless you need them for some reason- the only one you actually need is CURLOPT_RETURNTRANSFER.
Thank you very much jterry for your suggestions, I have removed all options except CURL_RETURNTRANSFER But I'm afraid, I'm still getting this error: ( ! ) Warning: json_decode() expects parameter 1 to be string, array given in C:\wamp\www\PNR\index.php on line 55 Call Stack # Time Memory Function Location 1 0.0011 144848 {main}( ) ..\index.php:0 2 3.2365 145616 json_decode ( ) ..\index.php:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.