2

我在我的 WordPress 主题中使用 BWP Recaptcha(更好的 WordPress Recaptcha)。我的系统在代理后面....

但是每次我尝试发布它都会出错

Could not open socket

所以我用谷歌搜索了解决方案并得到了一个我必须更改功能的东西

recpatchalib.php

改变

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.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;
}

即使在实施了这个解决方案之后,系统仍然会说

Could not Open socket

我不是一个经验丰富的 PHP 程序员……我只是在尝试使用 wordpress

在这方面的任何帮助都会有所帮助

4

3 回答 3

1

您可能需要检查是否启用了“allow_url_fopen”。如果未启用,您将无法打开套接字或进行 reCaptcha 所需的远程调用。

尝试查看博客根目录中的 php.ini 文件(我的实际上在 /etc 中,因为我运行自己的服务器)并检查“allow_url_fopen=On”。如果它不存在,则添加它。

于 2014-02-25T14:16:58.123 回答
0

我像这样编辑了函数并为我工作:

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.0\r\n";
    $http_request .= "Proxy-Authorization: Basic ".base64_encode($proxy_user.":".$proxy_pass)."\r\n";
    $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
    $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
    $http_request .= "\r\n";

    $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;
}
于 2013-12-23T22:56:06.680 回答
0

从 WordPress 2.8 开始,内置了新的代理支持并由 wp-config.php 定义控制。我已经修改了 recaptchalib.php 以包含以下内容,这似乎对我有用(而且我只需要在一个地方定义代理内容)。

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

    $req = _recaptcha_qsencode ($data);

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

    $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.0\r\n" . $http_request;
        $sockhost = $proxy->host();
        $sockport = $proxy->port();

        if ( $proxy->use_authentication() ) {
            $http_request .= "Connection: close\r\n";
            $http_request .= $proxy->authentication_header() . "\r\n";
        }
    } else {
        $http_request = "POST $path HTTP/1.0\r\n" . $http_request;
        $sockhost = $host;
        $sockport = $port;
    }

    $http_request .= "\r\n";
    $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("\r\n\r\n", $response, 2);

    return $response;

}

于 2014-07-08T13:24:30.750 回答