0

I am trying (for learning purpose) to get a specific Real-Time Bus Predictions from the API of Washington Metropolitan Area; https://developer.wmata.com and Real-Time Bus Predictions

Bus Stop ID: 3001954

Api Key: This is a Demo Api Key that can be use for demo: e13626d03d8e4c03ac07f95541b3091b Source: https://developer.wmata.com/demokey

I am trying to use their PHP JSON example code, but nothing is happing. I am missing something? Any suggestions please? Thank you so much in advance for your help.

<?php
// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
require_once 'HTTP/Request2.php';

$request = new Http_Request2('https://api.wmata.com/NextBusService.svc/json/jPredictions');
$url = $request->getUrl();

$headers = array(
    // Request headers
    'api_key' => 'e13626d03d8e4c03ac07f95541b3091b',
);

$request->setHeader($headers);

$parameters = array(
    // Request parameters
    'StopID' => '3001954',
);

$url->setQueryVariables($parameters);

$request->setMethod(HTTP_Request2::METHOD_GET);

// Request body
$request->setBody("{body}");

try
{
    $response = $request->send();
    echo $response->getBody();
}
catch (HttpException $ex)
{
    echo $ex;
}

?>

Maybe I am missing something here? $request->setBody("{body}");

Fabio
  • 237
  • 2
  • 13
  • Doesn’t look like you should send any request body at all here; this is a simple GET request that transports all necessary parameters in the query string. – 04FS Oct 14 '19 at 08:32
  • Thanks @04FS So you think this code should be good. If yes how come it does not display anything on the screen? Thanks – Fabio Oct 14 '19 at 13:43
  • Couldn’t telly you by just looking at the code, you’ll have to do a bit of debugging. Start by making sure you have proper PHP error reporting enabled via the PHP configuration, if not already the case. – 04FS Oct 14 '19 at 13:46
  • Thanks a lot @04FS Well that is out of my comfortable zone :-) I just did to see the bus schedule but I am not very good in php and coding in general. The reason why I asked this question because previously I did an API with another website Weather WebSite and it works perfectly. Do you know how can I enable PHP debugging? – Fabio Oct 14 '19 at 13:50
  • 1
    https://stackoverflow.com/questions/1475297/phps-white-screen-of-death – 04FS Oct 14 '19 at 14:08
  • Thanks @04FS I am going to try to enable and view the logs and publish here. Stay tuned ;-) – Fabio Oct 14 '19 at 14:11

1 Answers1

0

Looking at the logs I found the issue:

[Mon Oct 14 20:16:25.256582 2019] [php7:warn] [pid 1072] [client 10.0.0.25:64171] PHP Warning:  require_once(HTTP/Request2.php): failed to open stream: No such file or directory in /var/www/html/bus.php on line 3

So, I installed:

sudo apt-get update
sudo apt-get install php-http-request2

And now it works

Fabio
  • 237
  • 2
  • 13