【问题标题】:echo file_get_contents for ay url returns null in php在 php 中,ay url 的 echo file_get_contents 返回 null
【发布时间】:2021-02-03 07:01:53
【问题描述】:

我对 PHP 很陌生。我想知道为什么

    echo file_get_contents("https://www.google.com");

返回,实际上对于任何 URL,返回 null

另外,allow_url_fopen=on 在我的 php.ini 中。另外,对 URL 进行编码没有任何积极的结果( echo file_get_contents(urldecode("https://www.google.com/"));

提前致谢。

【问题讨论】:

  • 它没有在我的末尾显示 NULL。

标签: php file-get-contents


【解决方案1】:

我发现使用 curl 的解决方法:

$curl = curl_init('https://www.google.com/');
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
$result = curl_exec($curl);
echo $result;

【讨论】:

    【解决方案2】:

    您使用的代码行没有问题。

    1. 确保您的系统连接到互联网(如果是本地系统)
    2. 检查 php 错误日志
    3. 在 php ini 中设置 allow_url_fopen = On

    或者,试试这个方法可能对你有帮助

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL, "https://www.google.com");
      $file_contents = curl_exec($ch);
      curl_close($ch);
      echo $file_contents;
    

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多