【问题标题】:file_get_contents sometimes works, sometimes doesn'tfile_get_contents 有时有效,有时无效
【发布时间】:2016-10-27 09:44:13
【问题描述】:

我在 Ubuntu 14.04 中使用 Nginx 服务器,我的代码是用 symfony2 编写的。

我有一个返回 Json 响应的简单 API。

我正在尝试使用带有 file_get_contents 的另一个页面访问此页面:

 public function formAction()


        $url = 'http://example.com/api/link/';
        $options = array(
            'http' => array(
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            )
        );


        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);

        return ($result);          
    }

但有时有效,有时无效! 我做了一个循环来查看频率:

它看起来像 63/100 和 250/1000 工作。 (作为平均值)

当它不起作用时我得到了这个错误:

警告: 文件获取内容(http://example.com/api/link/): 无法打开流:HTTP 请求失败! HTTP/1.1 404 未在 /srv/www/path/to/my/file.php 在第 69 行

当我在 Amazon Web 服务器中重新启动我的实例时开始发生这种情况,所以我不知道我现在是否应该进行一些配置更改。我尝试使用 curl 但同样的问题。

更新

如果我尝试在浏览器中访问该页面,它总是会立即生效。

【问题讨论】:

  • 嗯,你得到了一个 404,所以问题似乎在另一端,而不是在你给出的代码中。
  • 你也可以做一些事情,一旦你得到这个错误,可能会再次调用同样的事情..
  • 看看 Guzzle HTTP : docs.guzzlephp.org/en/latest

标签: php symfony nginx server file-get-contents


【解决方案1】:

我发现了它是什么:

在我的 OVH 托管中,我更改了我的 dns 的 ipv4。 但是我没有改ipv6。

所以有时当我尝试访问该站点时,呼叫被重定向到正确的 IP(ipv4 中显示的路径),有时是旧的错误 IP(ipv6 指示的路径)

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 2015-02-11
    • 2012-08-02
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多