How to resolve cURL Error (7): could not connect to the host?

I send the product code to the web service in xml format using cUrl (php). I get the correct answer in localhost, but when the server does it, it shows

CURL (7) error: failed to connect to host

And here is 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 product code to the account and receive details from it. I tried to use both versions of php 4+ and php5 +, nothing works. Any solution.

+51
xml php curl
Mar 29 '12 at 9:26
source share
9 answers

Error code CURL 7 (CURLE_COULDNT_CONNECT)

very explicit ... that means Failed to connect() to host or proxy.

The following code will 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 do not see the Google page, then .. your URL is wrong or you have a problem with firewall or restriction .

+43
Mar 29 2018-12-12T00:
source share
— -

“Error CURL ERROR 7 Failed to connect to permission to refuse” is caused when, for some reason, a curl request is blocked by some kind of firewall or similar thing.

You will encounter this problem when the curl request will not be with standard ports.

for example, if you hang out with some URL that is on port 1234, you will run into this problem when a URL with port 80 will easily give you the results.

Most often this error is observed in CentOS and any other OS using "SElinux".

you need to either disable or change SElinux to allow

look at this

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

Hope this helps

+19
Aug 7 '13 at 7:49 on
source share

Can you hit this URL with a browser or PHP script? An error has occurred: you cannot connect. So first make sure the URL is accessible.

+2
Mar 29 2018-12-12T00:
source share

In my case, I had something like cURL Error (7): ... Operation Timed Out . I am using the network connection of the company I am working on. I needed to create some environment variables. The following worked for me:

In a Linux terminal:

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

On Windows, I created (the same) environment variables in the Windows path.

Hope this helps!

Hello!

+2
Feb 29 '16 at 19:17
source share

In PHP, if your network is under a proxy. You must set the proxy server url and port

 curl_setopt($ch, CURLOPT_PROXY, "http://url.com"); //your proxy url curl_setopt($ch, CURLOPT_PROXYPORT, "80"); // your proxy port number 

It solves my problem

+2
Jul 21 '16 at 6:19 06:19
source share

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

+1
Oct 13 '12 at 21:13
source share

This issue can also be caused by making curl calls on https when it is not configured on the remote device. An HTTP call can solve this problem in these situations, at least until you configure ssl on the remote computer.

+1
Feb 12 '16 at 12:16
source share

In my case, the problem was caused by the hosting provider, in which I used the blocking of the http packets addressed to their IP block, which came from their IP block. Un-frickin-believable !!!

+1
Sep 13 '16 at 0:38
source share

you can also get this if you are trying to use the same URL at the same time with multiple HTTP requests. Many curl requests will not be able to connect and therefore return with an error

-one
Jun 23 '14 at 13:05
source share



All Articles