title: cURL error: Failed to connect to live website 80: Bad access
body: <p>First I am new to CURL but not PHP.</p>
  
  <p>I have the following code. </p>
  
  <p>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 &amp; A to the title <a href="https://stackoverflow.com/questions/22612946/curl-returns-failed-connect-to-url80"></a> 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.</p>
  
  <pre><code>    #Note this is within a public function within a class so $this-&gt;currentword has been defined as a string
              $url = "http://example.com/{$this-&gt;currentword}";
      #$url = "http://example.localhost/{$this-&gt;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 = "&lt;br /&gt;No cURL data returned for {$url} [". $info['http_code']. "]";
              if (curl_error($curl)) $html .= "&lt;br /&gt;\n".curl_error($curl);
          }
          curl_close($curl);
          echo "&lt;br /&gt;\ncurled";
      } else if (file_get_contents(__FILE__) &amp;&amp; ini_get('allow_url_fopen')) {
          $options = array(
              'http' =&gt; array(
                  'user_agent' =&gt; $agent,
                  'max_redirects' =&gt; 10,
                  'timeout'       =&gt; 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)&lt;meta http-equiv="Expires"[^&gt;]*&gt;/i', '', $html);
          echo $html;
      }
  </code></pre>
  
  <p>the error message returned is:
  curled
  No cURL data returned for <a href="http://example.com/" rel="nofollow noreferrer">http://example.com/</a> [0]
  Failed to connect to example.com port 80: Bad access</p>
  
  <p>If I force it to use file_get_contents() it also falls over totally:
  Warning: file_get_contents(<a href="http://example.com/" rel="nofollow noreferrer">http://example.com/</a>): 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</p>
  
  <p>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.</p>
  
  <p>Thanks in advance.</p>