My contemplations and diurnal novelties are publicized here


Recaptcha provides a php library for integration into web applications. Unfortunately, it depends on the web server having full outgoing access to the internet. In a situation where you are forcing outgoing connections to go via proxy,  the following modifications should work.

The new _recaptcha_http_post() function:

/**
 * 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) {

        $proxy_host = '@@YOUR_WEB_PROXY_HERE';
        $proxy_port=3128;

        $req = _recaptcha_qsencode ($data);

        $http_request  = "POST http://$host$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($proxy_host, $proxy_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;
}
You can find the modified code here recaptchalib.php
Copied and edited from :-http://www.elifulkerson.com/projects/recaptcha-proxy-server-patch.php
Advertisements

Comments on: "reCAPTCHA Could not open socket (Behind a proxy)" (3)

  1. Thanks. It helped me.

  2. Hello,

    What about proxy authentication? How can you get the PHP code to use a username/password?

    Rob
    🙂

  3. Actually, I found it out from http://drupal.org/node/1338672

    function _recaptcha_http_post($host, $path, $data, $port = 80) {

    $proxy_host = ‘YOUR PROXY HOST’;
    $proxy_port=YOUR PROXY PORT;
    $proxy_username=YOUR USER NAME;
    $proxy_password=YOUR PASSWORD;

    $req = _recaptcha_qsencode ($data);

    $http_request = “POST http://$host$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”;

    if (!empty($proxy_username)) {
    $auth_string = base64_encode($proxy_username . ($proxy_password != ” ? “:{$proxy_password}” : ”));
    $http_request .= “Connection: close\r\n”;
    if ( !empty($auth_string ) ) $http_request .= “Proxy-Authorization: Basic {$auth_string}\r\n”;
    }

    $http_request .= “\r\n”;
    $http_request .= $req;

    $response = ”;
    if( false == ( $fs = @fsockopen($proxy_host, $proxy_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;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: