Googles Recaptcha service is great to get rid of those web bots which enter false information into web forms. Its great for the elimination of SPAM. In most organizations the use of proxy servers or caching servers are used for either caching content because of a saturated internet link or for authentication and moderation. The “recaptcha” library unfortunately doesn't cater for the use of the proxy servers and therefore we needed to update the library with a bit of PHP proxy information. The error message was “ Could not open socket “. This means that it had a problem connecting to port 80. This can be one of two reasons, either a host based firewall has blocked port 80 or you have a caching server in place running on the standard proxy ports, 3128 or 8080.
Locate the PHP library file “ recaptchalib.php “ and update the “The reCAPTCHA server URL's“ section with the information below. Set the proxy from false to true and enter the proxy server information.
define("PROXY_HOST", ""); //define proxy
define("PROXY_PORT", ""); //define port
define("USE_PROXY", false); //set this true if you want to use proxy.
Now that we have told our PHP script where to look for the proxy server we need to tell the HTTP POST to submit via the proxy server to the reCAPTCHA server. Add the code below to the recaptchalib.php file.
/**
* Submits an HTTP POST to a reCAPTCHA server
* @param string $host
* @param string $path
* @param array $data
* @param int port
* @return array response
*/
function _recaptcha_http_post($host, $path, $data, $port = 80) {
if(USE_PROXY) {
$req = _recaptcha_qsencode ($data);
$http_request = "POST http://" . $host . $path . " HTTP/1.0rn";
$http_request .= "Host: $hostrn";
$http_request .= "Content-Type: application/x-www-form-urlencoded;rn";
$http_request .= "Content-Length: " . strlen($req) . "rn";
$http_request .= "User-Agent: reCAPTCHA/PHPrn";
$http_request .= "rn";
$http_request .= $req;
$response = '';
if( false == ( $fs = @fsockopen(PROXY_HOST, PROXY_PORT, $errno, $errstr, 10) ) ) {
echo $errno . $errstr;
die ('Could not open socket to proxy');
}
}
else {
$req = _recaptcha_qsencode ($data);
$http_request = "POST $path HTTP/1.0rn";
$http_request .= "Host: $hostrn";
$http_request .= "Content-Type: application/x-www-form-urlencoded;rn";
$http_request .= "Content-Length: " . strlen($req) . "rn";
$http_request .= "User-Agent: reCAPTCHA/PHPrn";
$http_request .= "rn";
$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("rnrn", $response, 3);
//print_r($response);
return $response;
}
The reCaptcha service should now send HTTP POST requests via your proxy server.