我有一个网址-http: //www.xxx.xxx.xxx/
这个URL,我可以通过浏览器打开。我什至可以通过机器上的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上有效。我可以通过浏览器直接打开URL。但是,当我将代码部署在同一服务器上并尝试通过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
您更新的代码无法正常工作,因为http之前的空白 $url = " http...
由于某些防火墙或名称服务器问题,原始代码可能无法正常工作。尝试在同一服务器上的浏览器中直接访问要加载的URL。那也可能会失败。
您需要修复服务器以接受来自本地主机的请求。
还可以帮助您的方式是检查Web服务器和防火墙日志文件。查看为什么请求被拒绝。甚至暂时禁用防火墙以排除它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句