0

I am trying to format some data for a display screen. I am using the National Rail Live Departure Board Web Service to search for the trains departing from our local station.

I am using a php script called OpenLDBWS.php (h/t RailAleFan on github) to connect to the LDBWS. The connection works fine, but I am struggling to access the arrays that it generates in order to display the results I want.

So this code:

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

Will generate this 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&nbsp;between Carlisle and&nbsp;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
                                        )

                                )

                        )

                )

        )

)

What I really want is to discount a great deal of this information display certain fields, such as [std] [etd] etc.

I am stuck because I know that the information is an array, but I don't seem to be able to get at contents to begin to print the information I want. If I take the print_r out and replace it with an echo "$result" it just displays a blank screen.

I realise that this is a very beginner question, and I'm probably missing something obvious, but any help would be gratefully received.

Thanks.

2 Answers 2

1

$result is not a string, therefore it can't be echo'd. Its an object thats being returned from the API.

You would need to access its parameters like:

echo $result->GetStationBoardResult->locationName

here is a really good explanation of what you have.

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

Comments

0

Try:

echo $result->GetStationBoardResult->trainServices->service->std;
echo $result->GetStationBoardResult->trainServices->service->etd;

Comments

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.