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

PhpXmlCurl

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

“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

Solution 3 - Php

If you have tried all the ways and failed, try this one command:

setsebool -P httpd_can_network_connect on

Solution 4 - Php

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

Solution 5 - Php

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!

Solution 6 - Php

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.

Solution 7 - Php

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

Solution 8 - Php

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

Solution 9 - Php

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.

Solution 10 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajView Question on Stackoverflow
Solution 1 - PhpBabaView Answer on Stackoverflow
Solution 2 - PhpAliView Answer on Stackoverflow
Solution 3 - PhpIman MarashiView Answer on Stackoverflow
Solution 4 - Phpprince joseView Answer on Stackoverflow
Solution 5 - PhpIdealdo FélixView Answer on Stackoverflow
Solution 6 - PhpMohan ShanmugamView Answer on Stackoverflow
Solution 7 - PhpWhiteHorseView Answer on Stackoverflow
Solution 8 - Phpdv3View Answer on Stackoverflow
Solution 9 - PhpScott T RogersView Answer on Stackoverflow
Solution 10 - PhpJeffView Answer on Stackoverflow