このウェブサイトが提供する API を使用しています
この 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] = > [コンテンツ] => {"ステータス":"OK","データ":{"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"},"ボード":{"コード":"TPJ","名前":"ティルチラパッリ ジャンクション","時間":"22:20","タイムスタンプ":1369587000},"class":"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":"CHENNA 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 を使用した私の Web ページの出力には、最初に余分なテキストがあるようです。
さて、私の質問は、上記の配列から値を取得するにはどうすればよいかということです。
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"}]}}
上記の配列から一意の値を取得する方法を教えてください。