【问题标题】:file_get_contents not returning any datafile_get_contents 不返回任何数据
【发布时间】:2016-11-21 12:10:11
【问题描述】:

所以我使用了一个足球联赛 API,我让它返回了我需要的数据。然而,它现在突然停止工作,我不知道为什么。

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);

如果我回显$this-&gt;baseUri . $resource 我得到一个工作link 那么为什么file_get_contents 不能获取内容?

See the page here too.

【问题讨论】:

  • stream_context 的用途是什么?在您的示例代码中,$this-&gt;reqPrefs 为空,因此它似乎是多余的。还要检查ini_get('allow_url_fopen') 的值是否为1。

标签: php


【解决方案1】:

好吧,我把它简化为

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

正如你所料,这让我经历了整个灾难。 不知道这对你有没有帮助。

【讨论】:

  • 有些东西已经改变了,就像老话说的那样......但为了绝对确定,你是否像我上面所做的那样尝试过,只是为了从其他东西中消除任何可能的“噪音”。
【解决方案2】:

您的管理员可能已禁用 php.ini 文件中的功能 (file_get_contents)?您应该打印您的 php 信息页面并查看它。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
  • 2016-03-30
  • 2017-11-23
  • 2021-12-13
  • 2016-12-06
相关资源
最近更新 更多