Jump to content
IanMcEvoy

unable to retrieve api results

Recommended Posts

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>"; 
 
 
  }
 
  ?>

Share this post


Link to post
Share on other sites

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 by benanamen

Share this post


Link to post
Share on other sites

Your $array contains a sub-array (results) of bus journeys, so you need

foreach ($array['results'] as $bus) {
    echo $bus['destination];
    // ... etc
}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.