IanMcEvoy Posted December 2, 2016 Share Posted December 2, 2016 Trying to retrieve information from an API and display it in a table. <form action="dublinbus.php" method="get"><h2>Current Dublin Bus Times.</h2><b>Stop Number: </b><input type="number" name="stopid"></br></br><input type="submit"></form> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ <?php $stopID = $_GET['stopid']; $url = "https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=" . $_GET['stopid'] . "&format=json"; // Process the JSON and check for errors $json = file_get_contents($url); $array = json_decode($json,true); if ($stopID != $array["stopid"]) {// Get the values for $errorCode and $errorMessage$errorCode = $array["errorcode"];$errorMessage = $array["errormessage"]; echo "Error: "; echo $errorCode; echo $errorMessage; } else {// Get the values$duetime = $array["duetime"];$destination = $array["destination"];$route = $array["route"]; echo " <table>";echo " <th>Stop Number</th><th>Route</th><th>Due Time</th><th>Destination</th>";echo " <tr><td>" . $stopID . "</td><td>" . $route . "</td><td>" . $duetime . "</td><td>" . $destination . "</td></tr>";echo "</table>"; } ?> Quote Link to comment Share on other sites More sharing options...
benanamen Posted December 2, 2016 Share Posted December 2, 2016 (edited) You have several undefined index errors. If you turn on error reporting you would know exactly what is wrong. When I fixed those, it works fine with stop id 11 <?php $_GET['stopid'] = 11; // Benanamen Mod $url = "https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=" . $_GET['stopid'] . "&format=json"; // Process the JSON and check for errors $json = file_get_contents($url); $array = json_decode($json,true); echo "<pre>"; print_r($array); echo "</pre>"; die; Edited December 2, 2016 by benanamen Quote Link to comment Share on other sites More sharing options...
Barand Posted December 2, 2016 Share Posted December 2, 2016 Your $array contains a sub-array (results) of bus journeys, so you need foreach ($array['results'] as $bus) { echo $bus['destination]; // ... etc } Quote Link to comment Share on other sites More sharing options...
IanMcEvoy Posted December 3, 2016 Author Share Posted December 3, 2016 Thanks for the help guys but still not having any luck with it. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 3, 2016 Share Posted December 3, 2016 vague comments about not having any luck with something don't tell us anything useful. there are varying levels of luck and unless we know what your standard is, we don't know what result you are getting. communicate exactly what is happening and if it's not blatantly obvious what's wrong with the result, tell us what's wrong with the result and what result you expected. do you have php's error_reporting set to E_ALL and display_errors set to ON (preferably in the php.ini on your development system) so that php would help you by reporting and displaying all the errors it detects? by default, you cannot use a URL with file_get_contents(), and there would be a php error alerting you to this issue. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 3, 2016 Share Posted December 3, 2016 Did you run benanamen's code so you can see what your result's structure actually look like? I showed you how to process the results array. What more do you need? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.