Start a new topic

Basic API pull and json parse example....

Hi,


DISCLAIMER, it's been a few years since I was coding full time, I bashed up a quick app to display the current status of the UK Central line tube, you should be able to see it in the app store. I know the code is far from ideal but it may help somebody who is starting out. 


OBVS need a server with PHP. Try XAMPP is starting out.


Anywhere here is the evil.


<?php

$url = "http://cloud.tfl.gov.uk/TrackerNet/StationStatus";

$xml = simplexml_load_file("$url");

$att = 'Name';

$att2 = 'Description';

$att3 = 'CssClass';

$CenOK =0;

$CentralStatus ="";

$CentralDesc ="";

$url = "http://cloud.tfl.gov.uk/TrackerNet/LineStatus";

$xml = simplexml_load_file("$url");

foreach($xml->LineStatus as $a)

{

 $Line = $a->Line[0]->attributes()->$att;

  if ($Line == "Hammersmith and City") {

     $HammersmithandCityStatus = $a->Status[0]->attributes()->$att2;

     $HammersmithandCityDesc= $a->Status[0]->attributes()->$att3;

    }

  if ($Line == "Central") {

     $CentralStatus = $a->Status[0]->attributes()->$att2;

     $CentralDesc= $a->Status[0]->attributes()->$att3;

    }

  if ($Line == "Piccadilly") {

     $PiccadillyStatus = $a->Status[0]->attributes()->$att2;

     $PiccadillyDesc = $a->Status[0]->attributes()->$att3;

    }

  if ($Line == "District") {

     $DistrictStatus = $a->Status[0]->attributes()->$att2;

     $DistrictDesc = $a->Status[0]->attributes()->$att3;

    }

}

if ($CentralStatus == "Good Service") {

echo "{ \"frames\": [ { \"index\": 0, \"text\": \"All OK\", \"icon\": \"i1043\" } ]}";

$CenOK =1;

}else {

echo "{ \"frames\": [ { \"index\": 0, \"text\": \"Delays\", \"icon\": \"i1043\" } ]}";

}

?>


1 person has this question

You'll notice I've left in a station status pull. Maybe you can add a second frame with you local station status? (I have a private app that does this ). 


Take out 


$url = "http://cloud.tfl.gov.uk/TrackerNet/StationStatus";

$xml = simplexml_load_file("$url");


If you don't need.


Wilksy

You can also replace the for, next loop with decent code.

Login or Signup to post a comment