PHP script using API used to work but has since stopped, why?


#1

Hey,

So when I go to
http://api.tfl.gov.uk/line/mode/tube,overground,dlr,tflrail,tram/status?app_key=[key]&app_id=[id]
from my web browser it returns the correct JSON.

When I do it from a PHP script on my server I get the following:

Warning: file_get_contents(https://api.tfl.gov.uk/line/mode/tube,overground,dlr,tflrail,tram/status?app_key=[key]&app_id=[id]): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /home/cluster-sites/3/t/[myDomain].com/public_html/[fileName].php on line 12

What’s causing this? It used to work and still works with other non-TFL URLs?

Regards,
Toby.


#2

You could try removing the app_key and app_id as they aren’t actually needed for calls to get the https://api.tfl.gov.uk/line/mode/tube,overground,dlr,tflrail,tram/status status.

Having said that the app I wrote for the Class 707 trains is working…

    $strTFLurl = "https://api.tfl.gov.uk/line/mode/tube,overground,dlr,tflrail,tram/status?" . disruptionViews::TFLAPISUFFIX;

You might get 403’d if you make too many calls? I’m using a memcached 60-second cache wrapper on all my TFL calls to make sure I don’t even accidentally hammer the server.

public static function cacheTFLcall($strURL, $intMinutes = 1)
{
    $CC = new memcachedServer(__CLASS__ . __FUNCTION__ . crc32($strURL), $intMinutes);
    if ($CC->isCacheFresh()) {
        $strData = $CC->getThis();
    } else {
        $strData = file_get_contents($strURL);
        $CC->save($strData);
    }

    return $strData;
}