How to resolve cURL Error (7): couldn't connect to host?

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

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 Error (7): couldn't connect to host

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.

回答1:

CURL error code 7 (CURLE_COULDNT_CONNECT)

is very explicit ... it means 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.



回答2:

“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.

you will face this issue when ever the curl request is not with standard ports.

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.

Most commonly this error has been seen on CentOS and any other OS with ‘SElinux’.

you need to either disable or change ’SElinux’ to permissive

have a look on this one

http://www.akashif.co.uk/php/curl-error-7-failed-to-connect-to-permission-denied

Hope this helps



回答3:

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.



回答4:

In PHP, If your network under proxy. You should set the proxy URL and port

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



回答5:

Check if port 80 and 443 are blocked. or enter - IP graph.facebook.com and enter it in etc/hosts file



回答6:

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.



回答7:

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:

In Linux terminal:

$ export https_proxy=yourProxy:80 $ export http_proxy=yourProxy:80   

In windows I created (the same) environment variables in the windows way.

I hope it helps!

Regards!



回答8:

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!!!



回答9:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!