Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 错误:无法';t通过同一域上的CURL连接到主机_Php_Apache_Curl - Fatal编程技术网

Php 错误:无法';t通过同一域上的CURL连接到主机

Php 错误:无法';t通过同一域上的CURL连接到主机,php,apache,curl,Php,Apache,Curl,我有一个网址- 这个URL,我可以通过浏览器打开。我甚至可以从我的机器上通过cURL调用这个URL,但是当我从同一服务器和域上的代码调用它时,它会给我一个错误-错误:无法连接到主机 下面是我的代码- function get_xml_via_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); echo $url; curl_setopt($ch, CURLOPT_FAILONER

我有一个网址-

这个URL,我可以通过浏览器打开。我甚至可以从我的机器上通过cURL调用这个URL,但是当我从同一服务器和域上的代码调用它时,它会给我一个错误-
错误:无法连接到主机

下面是我的代码-

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调用时,会出现一个错误-
错误:无法连接到主机

更新代码-

我将服务器上的代码修改为-

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;
}
旋度的误差变为如下-

错误:libcurl中不支持或禁用协议http

请尝试:

$url="//your url";
echo get_xml_via_curl($url);

更新后的代码无法工作,因为http前面的空格出现在
$url=“http…

由于某些防火墙或名称服务器问题,原始代码可能无法工作。请尝试访问要在同一服务器上的浏览器中直接加载的URL。这可能也会失败

您需要修复服务器以接受本地主机的请求


此外,还可以检查web服务器和防火墙日志文件。查看请求被拒绝的原因。甚至暂时禁用防火墙以排除这种情况。

我也遇到过同样的问题,因为同一个域而抛出“连接超时错误”,并尝试了所有其他选项,但没有任何效果

最后我得到了答案并修复了它。只需运行下面的命令(ubuntu)

对于Cent OS:

# sudo ip link set lo up
# sudo ip addr show

“lo”是环回接口。这是一个特殊的网络接口,系统用来与自身通信。

可能有用的答案:它对我来说工作正常。奇怪。很抱歉我花了这么长时间,但这就是问题所在。
# sudo ip link set lo up
# sudo ip addr show