PHP Curl Cannot Connect to Host on Own Server

11,430

Solution 1

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

Solution 2

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.

Solution 3

Can you ping www.mysite.com ?

Check your /etc/hosts ... maybe there is a wrong IP specified for www.mysite.com.

Share:
11,430
west1737
Author by

west1737

Updated on July 15, 2022

Comments

  • west1737
    west1737 almost 2 years

    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?