I have a PHP script that uses cURL to access a file also located on my server (installed email marketing program). I wrote a quick script to test my cURL installation and it fails when pointed at a file on my own server. The script is:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mysite.com/test.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
if($contents) {
echo $contents;
} else {
$err = curl_error($ch);
echo $err;
}
curl_close($ch);
When I run this code, it returns an error: couldn't connect to host
If I change the URL to http://www.google.com or any other site, it works just fine. Also, when I try to open the specific page via cURL on another server, it works like it should. The assumptions I'm making from these tests is that the PHP cURL is installed and (kinda) working and that the problem is not in the page that is trying to be opened.
I thought it might be a port issue, so I tried specifying the port with the same result (not connecting to host). I tried both curl_setopt and http://www.mysite.com:80/ to specify a port I know to be open.
This leads me to believe that the issue is something in my apache installation, but I'm not an apache expert and I've been banging my head against Google all day to no avail. Any ideas what could cause such a specific cURL failure?
Ran into the same issue where my domain name would not resolve on the server causing CURL requests to itself to fail.
My problem was that my web server could not resolve to itself due to some firewall rules (there is a load balancer in front of the web server that was blocking traffic from the server).
In other words, running the following command on my server
curl -I http://www.mywebserver.com resulted in:
$ curl -I http://www.mywebserver.com
curl: (7) couldn't connect to host
Updating the /etc/hosts file on the server to map the domain name to the local ip address resolved the issue and fopen and curl from within my scripts were functional again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With