在PHP中解析json数组以获取所需的值

时间:2013-05-17 12:38:34

标签: api

我使用的是本网站提供的API

http://pnrapi.alagu.net/

通过使用此API,我们可以获得印度铁路的PNR状态。

我正在使用CURL进行调用,并以数组格式获取类似这样的页面内容:

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"}]}} )

但是当我转到网址http://pnrapi.alagu.net/api/v1.0/pnr/4563869832时,它会给我输出如下所示:

 {"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"}]}}

现在,似乎我的网页上有curl的输出有一些额外的文本,这是开头,因为你可以看到上面的两个输出。

好吧,我的问题是,如何从上面的数组中获取值。

我说的是使用CURL在我的页面上得到的数组输出,如下所示:

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"}]}} )

我的PHP页面中的代码是:

    <?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>";
?>

我只需要“内容”下的值,即列车编号,列车名称,旅行日期等。

那么,将这些信息提取到每个变量的最佳方法是什么?

就像我想要的那样:

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

依旧......

提前致谢。

我试过了:

echo $ pnr ['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"}]}}  

现在任何人都可以告诉我如何从上面的数组中获取唯一值?

2 个答案:

答案 0 :(得分:1)

我不确定JSON字符串在哪里。但是,让我们说它是$ pnr变量。

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

更新: 如果您不需要所有其他事项,您可以执行以下操作:

$npr = file_get_contents(url);

然后运行上面的代码。

答案 1 :(得分:0)

您正在查看header,在那里您应该查看content。在您的函数中返回$content,然后您可以解析响应:

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>";