tags:

views:

544

answers:

6

Hey, I'm trying to use cURL with PHP and its giving me this error:

"Failed to connect to 208.77.188.166: Operation not permitted"

I'm pretty sure its a server issue - but just in case, here is my code:

<?php

$ch = curl_init();
$url  ="http://www.example.com";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec($ch) or die(curl_error($ch));
echo $output;

?>

cURL is enabled on Apache, I've tried changing permissions of the file to 777.

Any ideas?

A: 

You should try using a version of curl installed on the server or on your workstation (command line version) and try to replicate the error, you may need to set a referrer header in the curl request, but that all depends on the server you are trying to contact.

Redbeard 0x0A
A: 

Could be a proxy issue or some kind of authentication problem on the server - can you access this URL using a regular web browser ?

Adam Pierce
+1  A: 

It's possible that you need to enable allow_url_fopen (reference) -- you can do this in an .htaccess file if it's on apache.

You can enable this by putting this in an .htaccess file:

php allow_url_fopen on
artlung
+1  A: 

Make sure you set all required CURL options:

  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HEADER         => false,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_ENCODING       => "",
  CURLOPT_USERAGENT      => "spider",
  CURLOPT_AUTOREFERER    => true, 
  CURLOPT_CONNECTTIMEOUT => 120,
  CURLOPT_TIMEOUT        => 120,
  CURLOPT_MAXREDIRS      => 10,

I will also suggest you echo out errors also using something like this:

$hostconnect = curl_init($url);
$errmsgcurl  = curl_error($hostconnect);
echo $errmsgcurl;

The above code is not tested and it just serves as an example.

Also would suggest trying out your code on a local apache server this way you can tell where the problem sits easily.

Codex73
+1  A: 

Many shared hosting providers prohibit outbound connections. Bluehost, for example, requires that you purchase a static IP before allowing outbound connections. Then you need to make sure CURL knows what outbound interface to use.

gahooa
this is not true. i have a shared hosting plan with bluehost, and it works perfectly when using curl to do outbound connections. i run a WoW armory fan site that does about 20,000+ outbound connections daily.
Jayrox
+1  A: 

The error you are receiving is most likely do to a firewall blocking all outbound connections. Many shared hosting providers are blocking outgoing port 80 connections to try to stop rampant errors in PHP scripts that allow remote includes to then be used as an attack vector against the server.

Please contact your host, and if this is the case you will need to find an alternate way to connect to the remote host, or move hosting companies.

X-Istence