Using Recaptcha via Proxy in PHP

I am using BWP Recaptcha (Better WordPress Recaptcha) in my WordPress theme. My System is behind a proxy….

But every time i try to post it gives an error

Read More

Could not open socket

So i googled the solution and got a something where i have to change a function in

recpatchalib.php

Change

function _recaptcha_http_post($host, $path, $data, $port = 80) {
    $proxy_host = 'PROXY-HOST';
    $proxy_port=PROXY-PORT;
    $proxy_username='PROXY-USERNAME';
    $proxy_password='PROXY-PASSWORD';

    $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";

    if (!empty($proxy_username)) {
        $auth_string = base64_encode($proxy_username . ($proxy_password != '' ? ":{$proxy_password}" : ''));
        $http_request .= "Connection: closern";
        if ( !empty($auth_string ) ) $http_request .= "Proxy-Authorization: Basic {$auth_string}rn";
    }

    $http_request .= "rn";
    $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("rnrn", $response, 2);

    return $response;
}

And even after implementing this solution the system still says

Could not Open socket

I am not a seasoned PHP programmer… i was jsut trying my hands on wordpress

Any help in this regard would be helpful

Related posts

Leave a Reply

3 comments

  1. You may need to check if you have enabled “allow_url_fopen”. If it is not enabled you will not be able to open sockets or do remote calls like reCaptcha requires.

    Try looking at your php.ini file in the blog’s root directory (mine is actually in /etc since I run my own server) and check for “allow_url_fopen=On”. If it is not there then add it.

  2. I edited the function like this and worked for me:

    function _recaptcha_http_post($host, $path, $data, $port = 80) {
    
        $proxy_host = 'my.private.proxy';
        $proxy_port = 3128;
        $proxy_user = 'user';
        $proxy_pass = 'pass';
    
        $req = _recaptcha_qsencode ($data);
    
        $http_request  = "GET http://$host$path?$req HTTP/1.0rn";
        $http_request .= "Proxy-Authorization: Basic ".base64_encode($proxy_user.":".$proxy_pass)."rn";
        $http_request .= "Content-Type: application/x-www-form-urlencoded;rn";
        $http_request .= "User-Agent: reCAPTCHA/PHPrn";
        $http_request .= "rn";
    
        $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("rnrn", $response, 2);
    
        return $response;
    }
    
  3. As of WordPress 2.8, there’s new proxy support built in and controlled by wp-config.php definitions. I’ve modified the recaptchalib.php to include the following which seems to work for me (and I only have to define the proxy stuff in one place).

    function _recaptcha_http_post($host, $path, $data, $port = 80) {
    
        $req = _recaptcha_qsencode ($data);
    
        $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";
    
        $proxy = new WP_HTTP_Proxy();
    
        $url = "http://" . $host . ( $port == 80 ? "" : ":" . $port) . $path;
    
        if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
            $http_request = "POST " . $url . " HTTP/1.0rn" . $http_request;
            $sockhost = $proxy->host();
            $sockport = $proxy->port();
    
            if ( $proxy->use_authentication() ) {
                $http_request .= "Connection: closern";
                $http_request .= $proxy->authentication_header() . "rn";
            }
        } else {
            $http_request = "POST $path HTTP/1.0rn" . $http_request;
            $sockhost = $host;
            $sockport = $port;
        }
    
        $http_request .= "rn";
        $http_request .= $req;
    
        $response = '';
        if( false == ( $fs = @fsockopen($sockhost, $sockport, $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, 2);
    
        return $response;
    

    }