【问题标题】: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/"));)
提前致谢。
【问题讨论】:
标签:
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】:
您使用的代码行没有问题。
- 确保您的系统连接到互联网(如果是本地系统)
- 检查 php 错误日志
- 在 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;