kimmik
kimmik

Reputation: 21

cURL error: Failed to connect to live website 80: Bad access

First I am new to CURL but not PHP.

I have the following code.

This works fine where the $url is on the localhost but fails when the url is my live server. I have checked the similar Q & A to the title and as far as I know I am not behind a proxy server. I can access the "live" url perfectly using any one of several browsers. I have several "development" localhost sites and they all return data from curl. None of the equivalent "live" sites (or google.com, stackoverflow.com) seem to be accessible. All produce the same error message.

    #Note this is within a public function within a class so $this->currentword has been defined as a string
            $url = "http://example.com/{$this->currentword}";
    #$url = "http://example.localhost/{$this->currentword}";   // this works returns the content ok
    $agent = "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0";
    if (function_exists('curl_version')) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, $agent);
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, 45);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_MAXREDIRS, 15);
        curl_setopt($curl, CURLOPT_COOKIESESSION, true);
        #curl_setopt($curl, CURLOPT_FAILONERROR, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_VERBOSE, true);
        $html = curl_exec($curl);
        $info = curl_getinfo($curl);
        if ($html === false || $info['http_code'] != 200) {
            $html = "<br />No cURL data returned for {$url} [". $info['http_code']. "]";
            if (curl_error($curl)) $html .= "<br />\n".curl_error($curl);
        }
        curl_close($curl);
        echo "<br />\ncurled";
    } else if (file_get_contents(__FILE__) && ini_get('allow_url_fopen')) {
        $options = array(
            'http' => array(
                'user_agent' => $agent,
                'max_redirects' => 10,
                'timeout'       => 120,
            )
        );
        $context = stream_context_create($options);
        $html = @file_get_contents($url, false, $context);
        if ($html !== false) { echo "works"; }
    } else { echo 'You have neither cUrl installed nor allow_url_fopen activated. Please setup one of those!'; }

    if (!empty($html)) {
        $html = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $html);
        echo $html;
    }

the error message returned is: curled No cURL data returned for http://example.com/ [0] Failed to connect to example.com port 80: Bad access

If I force it to use file_get_contents() it also falls over totally: Warning: file_get_contents(http://example.com/): failed to open stream: An attempt was made to access a socket in a way forbidden by its access permissions. in D:\WEB...\cWeb.class.php on line xxx

Any help appreciated - I have pretty much exhausted all else. Is there any way to determine IF I have a proxy set up on this PC. (I do have Norton installed but nothing else and as I said the browsers (Firefox, IE, Chrome) all can see the live sites.

Thanks in advance.

Upvotes: 1

Views: 9477

Answers (1)

vifito
vifito

Reputation: 187

Are you sure windows firewall allow PHP/Apache connection to external sites? Disable temporarily to check

Upvotes: 6

Related Questions