PHP-无法访问外部URL [英] PHP - Cannot access external URLs
问题描述
由于流量很大,我最近升级了我网站的服务器.在新服务器上,PHP的某些方面似乎已损坏.我有一个非常具体的代码无法正常工作.但是,由于版权原因,我只能向您显示与您无关的非机密信息:
I have recently upgraded my website's servers due to high amounts of traffic. On the new servers, some aspects of PHP seem to be broken. I have a very specific code that isn't working. However, due to copyright reasons, I can only show the non-confidential equivalent to you:
<?php
echo file_get_contents('http://www.google.com');
?>
此代码在升级之前绝对完美正常工作,现在在这里或那里有些奇怪的设置使该代码无法工作.
This code worked absolutely flawlessly before the upgrade, and now some odd setting here or there has prevented this code for working.
具体来说,无论您输入哪个外部URL,file_get_contents
函数都根本不起作用(file_get_contents('index.php')
可以正常工作);
To be specific, the file_get_contents
function doesn't work at all, regardless of what external URL you put in (file_get_contents('index.php')
works fine);
感谢您的帮助!
更新#1
此代码也不起作用:
UPDATE #1
This code does not work either:
<?php
ini_set("allow_url_fopen", "On");
echo file_get_contents('http://www.google.com');
?>
更新#2
该代码有效...
UPDATE #2
This code works...
<?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')
...
... but if I try to do simplexml_load_file($data);
bad things happen. Same if I do 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中.
The contents will be stored in $data.
这篇关于PHP-无法访问外部URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!