1

I am working on a simple PHP code which is supposed to extract specific data from a txt-file. The txt-file comes from a GTFS-RT API. I would like to keep it as simple as possible, and thereby not use the protocol-buffer file.

I am having trouble extracting the data.

I have tried treating the file as JSON, however, the result is empty. I have tried both Json_encode and Json_decode. I have tried print_r, var_dump and echo to see how the data is built, but I am stuck.

The data is built as follows:

https://i.stack.imgur.com/chl8S.png

<?php 
$data=file_get_contents("https://gtfsr-pp.samtrafiken.se/otraf/TripUpdates.pb?key={API-key}");
echo $data->entity[0]->id;
?>

This code comes back empty.

And yes, I have the correct API-key, I just hide the key here.

Lundqan
  • 43
  • 7
  • 3
    `var_dump($data)`, what do you get (better in your question than screenshot)? At best you'll get a string, which you need to actually decode using (assuming it's JSON which it isn't) json_decode. – Jonnix Mar 29 '19 at 12:31
  • It's not returning valid JSON – Jay Blanchard Mar 29 '19 at 12:32
  • Var_dump($data) responds as follows; https://i.imgur.com/kXJ5Wds.png – Lundqan Mar 29 '19 at 12:45
  • Would it be possible to add { } at start and end to convert it into JSON? – Lundqan Mar 29 '19 at 13:15
  • If you want to keep it as simple as possible, use a pre-existing library. https://github.com/google/gtfs-realtime-bindings-php seems to be the standard one for PHP. By trying to not use the protocol buffer you're making more compliated, not more simple – matt freake Apr 09 '19 at 08:52

0 Answers0