由于流量很大,我最近升级了网站的服务器。在新服务器上,PHP 的某些方面似乎被破坏了。我有一个非常具体的代码,但不起作用。但是,由于版权原因,我只能向您展示非 secret 的等效内容:
<?php
echo file_get_contents('http://www.google.com');
?>
在升级之前,此代码绝对完美工作,但现在这里或那里的一些奇怪设置阻止了该代码的工作。
具体来说,file_get_contents
函数根本不起作用,无论您输入什么外部 URL (file_get_contents('index.php')
都可以正常工作);
感谢任何帮助!
更新#1
此代码也不起作用:
<?php
ini_set("allow_url_fopen", "On");
echo file_get_contents('http://www.google.com');
?>
更新#2
该代码有效...
<?php
ini_set("allow_url_fopen", "On");
$url = "http://www.google.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>
...但是如果我尝试执行 simplexml_load_file($data);
则会发生不好的事情。如果我这样做 simplexml_load_file('http://www.google.com')
...
最佳答案
尝试使用 CURL。
$url = "http://google.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
内容将存储在$data中。
关于PHP - 无法访问外部 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16748206/