Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

curl

xml

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.

like image 678
Raj Avatar asked Mar 29 '12 09:03

Raj


People also ask

How to fix cURL error 7?

Check if port 80 and 443 are blocked, or check your personal firewall. Sometimes, Curl will use the post 8080 instead of the classic port 80. So you also should be sure that your server uses the port 80 on your server settings. Also, you should look at your security plugin if you have one enabled.

What is cURL error code 7?

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

What is cURL error?

cURL errors are often caused by an outdated version of PHP or cURL. cURL errors are a good example of server-related errors. These are errors that aren't caused by WordPress or Really Simple SSL, but by a server configuration. These errors usually won't cause any issues on the front end of your site.

How do I resolve curl error(7)?

How do I resolve cURL Error (7): couldn't connect to host? You either have a bad web address or the server you are seeking is not available. Carefully check the contents of the browser’s address bar to be sure that you didn’t leave something out “ttps:…” is sometimes the problem when one misses the initial “h” in an address.

What is curl error 7 failed to connect to permission denied?

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

Why am I getting a curl error on my website?

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 Show activity on this post. This issue can also be caused by making curl calls to https when it is not configured on the remote device.

Why can't I run curl on a remote device?

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. Show activity on this post.


2 Answers

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.

like image 157
Baba Avatar answered Sep 29 '22 20:09

Baba


“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

like image 39
Ali Avatar answered Sep 29 '22 19:09

Ali