Reputation: 377
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?
Upvotes: 3
Views: 13103
Reputation: 304
I ran into this as well on a remote webserver. Running curl on this machine I wasn't able to access files hosted by an apache server on the same machine. Strange this was I could access file by
curl -O http://example.com/file1.txt
but
curl -0 http://sub.example.com/file1.txt
did not work.
I solved it by adding the missing DNS record to the DNS server running on the machine. Seems like nslookup
etc first have a look at the local DNS server before asking somebody else. And as the local DNS server replied with "can't find" curl
gave up.
Upvotes: 0
Reputation: 1134
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.
Upvotes: 3
Reputation: 6920
Try to open http://www.mysite.com/test.php
in a web browser. If it works, then the problem is with cURL. If it doesn't, then the problem is either with your network, or apache configuration.
You can check your apache configuration by making sure that it is running on your system. Then try to telnet <apache server ip> 80
from the command line. Once connected try to do the following:
GET /test.php HTTP/1.1
Host: www.mysite.com
<2 line breaks>
If you get a response from apache, then it sounds like a network issue. Make sure mysite.com resolves to the IP address of your apache server, and port 80 traffic can get to your server. It could also be a loopback routing issue of your router trying to make a request to it's own ip address (assuming your apache box is behind a routed network connection, the same connection that you are on).
Upvotes: 0
Reputation: 35008
Can you ping www.mysite.com
?
Check your /etc/hosts ... maybe there is a wrong IP specified for www.mysite.com
.
Upvotes: 1