What is fsockopen blocking?

After a noon battle, I can finally get reCAPTCHA to work by converting this function:

function _recaptcha_http_post($host, $path, $data, $port = 80) { $req = _recaptcha_qsencode ($data); $http_request = "POST $path HTTP/1.0\r\n"; $http_request .= "Host: $host\r\n"; $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n"; $http_request .= "Content-Length: " . strlen($req) . "\r\n"; $http_request .= "User-Agent: reCAPTCHA/PHP\r\n"; $http_request .= "\r\n"; $http_request .= $req; $response = ""; if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) { die ("Could not open socket"); } fwrite($fs, $http_request); while ( !feof($fs) ) $response .= fgets($fs, 1160); // One TCP-IP packet fclose($fs); $response = explode("\r\n\r\n", $response, 2); return $response; } 

in

 function _recaptcha_http_post($host, $path, $data, $port = 80) { $req = _recaptcha_qsencode ($data); $request = curl_init("http://".$host.$path); curl_setopt($request, CURLOPT_USERAGENT, "reCAPTCHA/PHP"); curl_setopt($request, CURLOPT_POST, true); curl_setopt($request, CURLOPT_POSTFIELDS, $req); curl_setopt($request, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($request); return $response; } 

Basically, I'm curious to know why curl works, and fsockopen fails with "Failed to open socket." Thanks.

In addition : socket support is included.

+7
source share
4 answers

Maybe I'm wrong, but you use $port = 80 in fsockopen() , and in the case of cURL this variable is not used at all. I had the same problem when you tried to connect to SSL through port 80 instead of port 443 ; as far as I know, cURL by default checks SSL and connects accordingly.

Also try running cURL with CURLOPT_VERBOSE to see what it does.

+1
source

What is $ errno and $ errstr inside if (false === ...)? So what does it output if you switch to

  if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) { die ("Could not open socket, error: " . $errstr); } 
0
source

Woah

  if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) { die ("Could not open socket"); } 

It does not matter. Try:

  $fs = fsockopen($host, $port, $errno, $errstr, 10); // @ ignores errors if(!$fs) die ("Could not open Socket"); 

Also, Skype also sometimes blocks port 80.

0
source

Googling for your error is surprising if your /etc/resolv.conf is read by PHP. Do ls -lah /etc/resolv.conf in bash to make sure it is readable. You will get something like:

 myserver:~ myname$ ls -lah /ets/resolv.conf lrwxr-xr-x 1 root wheel 20B 16 mrt 2011 /etc/resolv.conf ^ if there is an 'r' here it is readable. if you have '-' here, it is not. 

If it is not readable, try making in bash: chmod 644 /etc/resolv.conf to make it readable.

0
source

All Articles