1

我正在使用本网站提供的 API

http://pnrapi.alagu.net/

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

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

数组 ( [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] = > [内容] => {“状态”:“确定”,“数据”:{"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},"类":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}})

但是当我转到 URL 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];

等等...

提前致谢。

我试过这个:

回声 $pnr['内容'];

我得到的输出是:

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

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

4

2 回答 2

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

然后运行上面的代码。

于 2013-05-17T12:43:58.263 回答
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>";
于 2013-05-17T13:01:42.597 回答