PHP - 无法访问外部 URL

标签 php url file-get-contents

由于流量很大,我最近升级了网站的服务器。在新服务器上,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/

相关文章:

javascript - 使用带有括号的图像 URL 作为 jQuery 的背景

PHP/MySQL - 如何在没有表单的情况下将 MySQL 数据添加到地址栏中的 url

javascript - 如果 AngularJS $location.path() 改变,做一些事情

curl - file_get_contents 和 curl 都不起作用

php - 列计数错误与第 1 行的值计数不匹配,错误仍然存​​在

php - 使用javascript执行php文件时出现问题

php - 合并两个查询 MYSQLi

php - 当使用浏览器打开 URL 且 URL 有效时,file_get_contents 返回 404

PHP file_get_contents() 遵循 Content-length header

php - 根据另一个表更新 mysql 表。复杂查询