【问题标题】:PHP file_get_contents and stream return warning instead of contentPHP file_get_contents 和流返回警告而不是内容
【发布时间】:2014-02-13 17:15:02
【问题描述】:

我正在尝试获取 HTTP / HTTPS 网站的内容。

我在本地有一个正在运行的 XAMPP 环境,并在 php.ini 中添加了这些内容:

extension=php_openssl.dll
allow_url_include = On
allow_url_fopen = On

我尝试通过 2 种方式获取内容:

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

还有:

<?php
    $stream = fopen('http://www.example.com', 'r'))
    {
        echo stream_get_contents($stream);
        fclose($stream);
    }
?>

此外,当我在 fopen 或 file_get_contents 前面使用抑制标记“@”时,我一直收到警告,只在 fopen 或 file_get_contents 行所在的 XY 行中说警告。但是没有从 URL 加载任何内容..

知道如何解决这个问题吗?

【问题讨论】:

  • 在您的 PHP 配置中启用 allow_url_fopen
  • 试试 $homepage = file_get_contents('example.com/'); var_dump($http_response_header);通话后
  • @Hanky웃Panky is not allow_url_fopen = 同样,我认为这会启用它,因为我已经这样做了.. @@Rakesh Sharma .. 与之前相同的警告没有出现输出,因为在此之前必须有一个错误没有显示..
  • 每次更改您的 php.ini 后 .. 您应该重新启动您的 apache
  • @LeoBali 在 php.ini 中进行编辑后,我确实重新启动了 apache 服务器

标签: php apache http url localhost


【解决方案1】:

你可以用 cURL 试试。

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "example.com");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $output = curl_exec($ch);
 curl_close($ch);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2015-12-16
    • 2012-12-13
    • 2014-04-24
    • 1970-01-01
    相关资源
    最近更新 更多