php 如何解决 cURL 错误 (7):无法连接到主机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9922562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resolve cURL Error (7): couldn't connect to host?
提问by Raj
I send an item code to a web service in xml format using cUrl(php). I get the correct response in localhost, but when do it server it shows
我使用 cUrl(php) 以 xml 格式将项目代码发送到 Web 服务。我在本地主机中得到了正确的响应,但是它什么时候显示在服务器上
cURL Error (7): couldn't connect to host
cURL 错误 (7):无法连接到主机
And here's my code:
这是我的代码:
function xml_post($post_xml, $url)
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_xml);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
// curl_setopt($ch, CURLOPT_PORT, $port);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
} else {
echo "Data received\n";
}
curl_close($ch);
echo $data;
}
I send the item code to the tally and fetch the details from it. I tried using both the versions php 4+ and php5+, nothing works out Any solution.
我将项目代码发送到理货机并从中获取详细信息。我尝试同时使用 php 4+ 和 php5+ 版本,但没有任何解决方案。
回答by Baba
CURL error code 7 (CURLE_COULDNT_CONNECT)
CURL 错误代码 7 (CURLE_COULDNT_CONNECT)
is very explicit ... it means Failed to connect() to host or proxy.
非常明确……这意味着 Failed to connect() to host or proxy.
The following code would work on any system:
以下代码适用于任何系统:
$ch = curl_init("http://google.com"); // initialize curl handle
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);
If you can not see google page then .. your URL is wrong
or you have some firewall
or restriction
issue.
如果您看不到 google 页面,那么 ..your URL is wrong
或者您有一些firewall
或restriction
问题。
回答by Ali
“CURL ERROR 7 Failed to connect to Permission denied” error is caused, when for any reason curl request is blocked by some firewall or similar thing.
“CURL ERROR 7 Failed to connect to Permission denied”错误是由于某种原因导致 curl 请求被防火墙或类似的东西阻止的。
you will face this issue when ever the curl request is not with standard ports.
当 curl 请求不使用标准端口时,您将面临此问题。
for example if you do curl to some URL which is on port 1234, you will face this issue where as URL with port 80 will give you results easily.
例如,如果您确实 curl 到某个位于端口 1234 上的 URL,您将面临这个问题,因为带有端口 80 的 URL 将很容易为您提供结果。
Most commonly this error has been seen on CentOS and any other OS with ‘SElinux'.
最常见的是,在 CentOS 和任何其他带有“SElinux”的操作系统上都可以看到此错误。
you need to either disable or change 'SElinux' to permissive
您需要禁用或更改“SElinux”以允许
have a look on this one
看看这个
http://www.akashif.co.uk/php/curl-error-7-failed-to-connect-to-permission-denied
http://www.akashif.co.uk/php/curl-error-7-failed-to-connect-to-permission-denied
Hope this helps
希望这可以帮助
回答by Idealdo Félix
In my case I had something like cURL Error (7): ... Operation Timed Out
. I'm using the network connection of the company I'm working for. I needed to create some environment variables. The next worked for me:
就我而言,我有类似的东西cURL Error (7): ... Operation Timed Out
。我正在使用我工作的公司的网络连接。我需要创建一些环境变量。下一个对我有用:
In Linux terminal:
在 Linux 终端中:
$ export https_proxy=yourProxy:80
$ export http_proxy=yourProxy:80
In windows I created (the same) environment variables in the windows way.
在 Windows 中,我以 Windows 方式创建了(相同的)环境变量。
I hope it helps!
我希望它有帮助!
Regards!
问候!
回答by prince jose
In PHP, If your network under proxy. You should set the proxy URL and port
在 PHP 中,如果您的网络在代理下。您应该设置代理 URL 和端口
curl_setopt($ch, CURLOPT_PROXY, "http://url.com"); //your proxy url
curl_setopt($ch, CURLOPT_PROXYPORT, "80"); // your proxy port number
This is solves my problem
这是解决我的问题
回答by Mohan Shanmugam
Are you able to hit that URL by browser or by PHP script? The error shown is that you could not connect. So first confirm that the URL is accessible.
您可以通过浏览器或 PHP 脚本访问该 URL 吗?显示的错误是您无法连接。所以首先要确认这个 URL 是可以访问的。
回答by WhiteHorse
Check if port 80 and 443 are blocked. or enter - IP graph.facebook.com and enter it in etc/hosts file
检查端口 80 和 443 是否被阻止。或输入 - IP graph.facebook.com 并将其输入到 etc/hosts 文件中
回答by Jeff
In my case, the problem was caused by the hosting provider I was using blocking http packets addressed to their IP block that originated from within their IP block. Un-frickin-believable!!!
就我而言,问题是由托管服务提供商引起的,我正在使用阻止 http 数据包寻址到他们的 IP 块,这些数据包源自其 IP 块。难以置信!
回答by Iman Marashi
If you have tried all the ways and failed, try this one command:
如果您尝试了所有方法但都失败了,请尝试以下命令:
setsebool -P httpd_can_network_connect on
回答by Scott T Rogers
This issue can also be caused by making curl calls to https when it is not configured on the remote device. Calling over http can resolve this problem in these situations, at least until you configure ssl on the remote.
当远程设备上未配置 https 时,对 https 进行 curl 调用也可能导致此问题。在这些情况下,通过 http 调用可以解决此问题,至少在您在远程配置 ssl 之前是这样。
回答by dv3
you can also get this if you are trying to hit the same URL with multiple HTTP request at the same time.Many curl requests wont be able to connect and so return with error
如果您尝试同时使用多个 HTTP 请求访问同一个 URL,您也可以获得此信息。许多 curl 请求将无法连接,因此返回错误