我正在尝试为显示屏幕格式化一些数据。我正在使用国家铁路直播离境委员会网络服务来搜索从我们当地车站出发的列车。
我正在使用名为OpenLDBWS.php的php脚本(h / t在Github上使用RailAleFan)连接到LDBWS。连接工作正常,但我正在努力访问它生成的数组,以显示我想要的结果。
所以这段代码:
<?php
require 'OpenLDBWS.php';
$OpenLDBWS = new OpenLDBWS("MY LDBWS API KEY HERE");
$result = $OpenLDBWS->GetDepartureBoard(1, "BHM");
header ("Content-Type: text/plain");
print_r($result);
?>
会产生这样的结果:
stdClass Object
(
[GetStationBoardResult] => stdClass Object
(
[generatedAt] => 2016-02-03T16:11:20.1570854+00:00
[locationName] => Birmingham New Street
[crs] => BHM
[nrccMessages] => stdClass Object
(
[message] => Array
(
[0] => stdClass Object
(
[_] => <P>Disruption between Carlisle and Glasgow Central / Edinburgh. More details can be found in <A href="http://nationalrail.co.uk/service_disruptions/117252.aspx">Latest Travel News</A>.</P>
)
[1] => stdClass Object
(
[_] => Disruption between Milton Keynes Central and London Euston / Clapham Junction. More details can be found in <A href="http://nationalrail.co.uk/service_disruptions/119008.aspx">Latest Travel News. </A>
)
)
)
[platformAvailable] => 1
[trainServices] => stdClass Object
(
[service] => stdClass Object
(
[std] => 16:05
[etd] => 16:09
[platform] => 8
[operator] => London Midland
[operatorCode] => LM
[serviceType] => train
[serviceID] => m+0v++xnRsACKmZqgUZwAg==
[origin] => stdClass Object
(
[location] => stdClass Object
(
[locationName] => Longbridge
[crs] => LOB
)
)
[destination] => stdClass Object
(
[location] => stdClass Object
(
[locationName] => Lichfield City
[crs] => LIC
)
)
)
)
)
)
我真正想要的是折扣大量此类信息显示某些字段,例如[std] [etd]等。
我被困了,因为我知道信息是一个数组,但我似乎无法获取内容以开始打印我想要的信息。如果我取出print_r并用echo“$ result”替换它,它只显示一个空白屏幕。
我意识到这是一个非常初学的问题,我可能会遗漏一些明显的东西,但是我会非常感激地收到任何帮助。
感谢。
答案 0 :(得分:0)
$result
不是字符串,因此无法回显。它是一个从API返回的对象。
您需要访问其参数,如:
echo $result->GetStationBoardResult->locationName
here对你所拥有的内容有很好的解释。
答案 1 :(得分:0)
尝试:
echo $result->GetStationBoardResult->trainServices->service->std;
echo $result->GetStationBoardResult->trainServices->service->etd;