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

14

Best Answer


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

If you are encountering a cURL Error (7): couldn't connect to host, it means that your script or application is unable to establish a connection to the host server. This error can occur due to various reasons, such as network connectivity issues, incorrect server configurations, or firewall restrictions.

To resolve this error, you can follow the steps below:

  1. Check your internet connection: Ensure that you have a stable and working internet connection. Sometimes, the error might occur due to a temporary network issue.
  2. Verify the host URL: Double-check the URL you are trying to connect to. Make sure it is correct and accessible.
  3. Check firewall settings: If you have a firewall enabled, ensure that it is not blocking the outgoing connection to the host server.
  4. Verify DNS settings: Ensure that your DNS settings are configured correctly. You can try using a different DNS server or flush your DNS cache.

By following these steps, you should be able to resolve the cURL Error (7): couldn't connect to host and establish a successful connection to the desired server.

“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

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

setsebool -P httpd_can_network_connect on

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 urlcurl_setopt($ch, CURLOPT_PROXYPORT, "80"); // your proxy port number

This is solves my problem

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!

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.

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

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

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.

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

For a couple of days I was totally blocked on this. I'm very very new to networking/vms but was keen to try set it up myself instead of paying a hosting company to do it for me.

Context

I'm rebuilding the server side for an app that uses php routines to return various bits of data from internal sources as well as external APIs for a map based app. I have started an Oracle VM instance and have installed/set up Apache and php. All running totally fine, until one of my php routines tries to execute a cURL. I start implementing error logging to find that I don't even get a message - just '7', despite implementation being very similar to the above. My php routine accessing an internal file for data was running successfully so I was fairly sure it wasn't an Apache or php issue. I also checked my Apache error logs, nothing telling.

Solution

I nearly gave up - there's talk on disabling SELinux above and in other articles, I tried that and it did work for my purposes, but here's a really good article on why you shouldn't disable SELinux https://www.electronicdesign.com/technologies/embedded-revolution/article/21807408/dont-do-it-disabling-selinux

If temporarily disabling it works and like me you don't want to do this (but it confirms that SELinux is blocking you!), I found a neat little command that actually prints out any SELinux issues in a more readable fashion:

sealert -a /var/log/audit/audit.log

This returned the following:

found 1 alerts in /var/log/audit/audit.log--------------------------------------------------------------------------------SELinux is preventing php-fpm from name_connect access on the tcp_socket port 443.

Great, I now get a bit more information than just '7'. Reading further down, I can see it actually makes suggestions:

***** Plugin catchall_boolean (24.7 confidence) suggests ******************If you want to allow httpd to can network connectThen you must tell SELinux about this by enabling the 'httpd_can_network_connect' boolean.Dosetsebool -P httpd_can_network_connect 1

This has been mentioned further above but now I have a bit more context and an explanation as to what it does. I run the command, and I'm in business. Furthermore, my SELinux is still set to enforcing, meaning my machine is more secure.

There are many other suggestions logged out, if you're blocked it might be worth logging out/checking out /var/log/audit/audit.log.

Execute the command on your terminalsetsebool -P httpd_can_network_connect on

If you white try from your browser and show message like "curl error 7: failed to connect to 172...**: permission denied" then try from terminal like "http://172 ****". If it working from terminal then run this command.

sudo setsebool httpd_can_network_connect 1

And try from you browser again. Hope will work. Worked fro me.