【发布时间】:2014-08-21 07:34:05
【问题描述】:
我有一个网址 - http://www.xxx.xxx.xxx/
这个网址,我可以通过浏览器打开。我什至可以从我的机器上通过 cURL 调用这个 URL,但是当我从同一服务器和域上的代码调用它时,它给了我一个错误 - error: couldn't connect to host
以下是我的代码 -
function get_xml_via_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
echo $url;
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
/*curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);*/
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$returned = curl_exec($ch);
if(curl_errno($ch)) {
echo 'error: ' . curl_error($ch);
}
curl_close($ch);
// $xml === False on failure
$xml = simplexml_load_string($returned);
return $xml;
}
就像我说的,在 localhost 上这行得通。我可以直接通过浏览器打开网址。但是当我在同一台服务器上部署代码并尝试通过 cURL 调用时,它给了我一个错误 - error: couldn't connect to host
更新代码 -
我将服务器上的代码修改为 -
function get_xml_via_curl($url) {
$url = " http://www.xxx.xxx.xxx.xxx/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$returned = curl_exec($ch);
if(curl_errno($ch)) {
echo 'error: ' . curl_error($ch);
}
curl_close($ch);
$xml = simplexml_load_string($returned);
return $xml;
}
并且从 curl 的错误变为跟随 -
error: Protocol http not supported or disabled in libcurl
【问题讨论】:
-
可能有帮助的答案:stackoverflow.com/a/9923212/3000179
-
它对我来说工作正常。很奇怪。