【问题标题】:file_get_contents doesn't work but fsockopen worksfile_get_contents 不起作用,但 fsockopen 有效
【发布时间】:2012-09-30 18:51:27
【问题描述】:

我已经在 Ubuntu Desktop 12.04 上安装了 php 5.3.14。

与: allow_url_fopen = 1

下面不起作用:

<?php
echo file_get_contents('http://www.example.com');

工作如下:

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}

甚至 curl_exec() 也有效。 我也用 Python 试过这样,Python 可以获取 www 内容。

我没有使用防火墙、代理。

但是本地网络没有问题。 (192.168.1.36 是我的本地服务器机器。)

<?php
echo file_get_contents('http://192.168.1.36);

是否有任何配置或安装问题? 谢谢。

【问题讨论】:

  • file_get_contents 不起作用时会出现什么错误?
  • 打开完整的错误报告后,您不会收到任何错误吗? - 难以置信。
  • @Leigh 和 error_reporting(E_ALL);
  • @MrCode 错误是PHP Warning: file_get_contents(http://www.example.com/): failed to open stream: HTTP request failed! in /path/to/test.php on line 14

标签: php file-get-contents fsockopen


【解决方案1】:

这是一个类似的问题:

PHP file_get_contents does not work on localhost

检查答案,然后它应该可以工作

【讨论】:

  • 感谢您提出类似的问题。但不幸的是file_get_contents 在本地网络上工作,包括我的环境中的本地主机......
【解决方案2】:

答案之一是:

您还需要检查 PHP.ini 文件

extension = php_openssl.dll

如果启用与否,如果没有,则只需通过删除“;”来启用它签到

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 2014-06-19
    • 2012-08-30
    • 2013-06-30
    • 2010-11-26
    • 2012-02-15
    • 2011-03-23
    相关资源
    最近更新 更多