4

So i'm using a football league API, I had it returning the data I needed. However it has now stopped working all of a sudden and im not sure why.

class leagueTable {
    public $data;
    public $baseUri;
    public $config;
    public $tr;

    public function __construct($payload) {
      $this->data = $payload;
      $this->config = parse_ini_file('config.ini', true);
      $this->baseUri = $this->config['baseUri'];
      $this->writeTable();
    }

    public function writeTable() {
      $resource = 'soccerseasons/' . $this->data . '/leagueTable';
      $response = file_get_contents($this->baseUri . $resource, false, stream_context_create($this->reqPrefs));

      if ($response == "")  {
        echo "Unable to retrieve data, please contact the administrator!<br />
            <a target='_blank' href='".$this->baseUri.$resource."'>JSON</a>";
      }
      $result = json_decode($response);

If I echo out $this->baseUri . $resource I get a working link so why can't file_get_contents get the contents?

See the page here too.

Martyn Ball
  • 4,679
  • 8
  • 56
  • 126
  • What is the purpose of the stream_context? In your sample code, `$this->reqPrefs` is null, so it appears to be redundant. Also check that the value of `ini_get('allow_url_fopen')` is 1. – Steve E. Nov 22 '16 at 02:22

3 Answers3

1

Well I dumbed this down to be

<?php
$response = file_get_contents('http://api.football-data.org/v1/soccerseasons/426/leagueTable');
$result   = json_decode($response);
var_dump($result);

And that got me the whole catastrophe as you are expecting. Not sure if that helps you any.

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
  • Something has changed as the ole saying goes... But just to be absolutely certain, did you try it as I did above, just to remove any possible "noise" from something else. – TimBrownlaw Nov 21 '16 at 10:31
0

Your administrator might have disabled the function (file_get_contents) in php.ini file? You should print your php info page and have a look into it.

Brijin
  • 23
  • 4
  • Will try that soon, it was on my hosting however and I havn't changed any of the settings – Martyn Ball Nov 21 '16 at 10:18
  • 1
    This is along the lines as suggested by @Brijin http://stackoverflow.com/questions/19024349/php-file-get-contents-does-not-work-from-server – TimBrownlaw Nov 21 '16 at 10:34
0

Be aware, if your url has get parameters and if the those parameters contains "white spaces" , you will need encode the parameters.

for example:

$query="this text has white spaces";
$url='https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&key='.$YOUTUBE_API_KEY.'&q='.rawurlencode($query).'&regionCode=US';
Tiran
  • 31
  • 1
  • 4