1

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.

Community
  • 1
  • 1
kimmik
  • 21
  • 2
  • 8
  • I am not sure if this has any relevance but am running Windows XAMPP for Apache/PHP/MySQL/Fillzilla) the external sites are hosted on Linux boxes. But cannot see the relevance of that as all external sites give the error not just the ones I want to access. – kimmik Mar 05 '15 at 14:26

1 Answers1

6

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

vifito
  • 177
  • 4
  • Windows/firewall - I have checked Control Panel/Windows Firewall and it says "These settings are being managed by vendor application Norton Internet Security". Sorry to sound dumb but I do not use Windows security software as it conflicts with Norton Security. I have not set up any firewall since taking possession of the machine over 5 years ago. It is running Win7 Home edition. I cannot see any proxy server settings in Norton. – kimmik Mar 05 '15 at 13:45
  • In my ignorance I don't see why/how the "firewall" out of the box prevents Apache/php from accessing the internet and yet allows the browser to do so. I do have other programs that seem to have access to the internet presumably through this "firewall" without having set them up in any other way to access via a proxy. (iTunes,Evernote,FileZilla, ...) – kimmik Mar 05 '15 at 13:57
  • OK - turning off Norton !! and it works. Turning Norton back on and it doesn't. So what do I do in Norton to allow it to work and keep antivirus operational? – kimmik Mar 10 '15 at 14:55
  • Norton should have a list of allowed programs, and you should add the Apache HTTP process. – vifito Mar 11 '15 at 09:55