如何使用PHP Http_Request2()发出https请求

Nec*_*twi 3 php https pear http-request

我想使用pear http_request2($ url)类发出https请求.我能够发出http请求但不能https.并且该网站便于http和https.服务器响应https没有问题.

    require 'HTTP/Request2.php';
    $url = 'https://collegedb2.ferryfair.com';
    $r = new Http_Request2($url);
    $r->setMethod(HTTP_Request2::METHOD_POST);
    try {
        $response = $r->send();
    } catch (Exception $exc) {
        $es = $exc->getTraceAsString();
        $ets=$exc->__toString();
        $egc=$exc->getCode();
        $egl=$exc->getLine();
        $egm=$exc->getMessage();
        $egt=$exc->getTrace();
        $response = null;
    }
    $page = $response->getBody();
    echo $page;
Run Code Online (Sandbox Code Playgroud)

这是错误消息:

$egm=(string) Unable to connect to ssl://collegedb2.ferryfair.com:443. Error: stream_socket_client(): unable to connect to ssl://collegedb2.ferryfair.com:443 (Unknown error)

Vio*_*man 8

禁用证书验证的评论对我来说很关键.

$request = new HTTP_Request2('https://someserver.com/somepath/something',
    HTTP_Request2::METHOD_POST);

$request->setConfig(array(
    'ssl_verify_peer'   => FALSE,
    'ssl_verify_host'   => FALSE
));
Run Code Online (Sandbox Code Playgroud)

  • 但永远记住你只是压制邪恶的根源 (2认同)

Dav*_*win 5

我遇到了这个问题。修复(对我而言)是使用“curl”作为适配器,例如:

$request = new Http_Request2('https://whatever');
$request->setAdapter('curl');
$response = $request->send();
Run Code Online (Sandbox Code Playgroud)

另请参阅http://pear.php.net/manual/en/package.http.http-request2.config.php上的“SSL 参数”部分,其中说

如果您没有明确提供 ssl_cafile 和/或 ssl_capath,尤其是使用 Socket 适配器,则对等验证可能会失败。

大概 curl 知道在哪里可以找到要信任的本地 CA 文件。

(带有卷曲的 PHP 5.4.38)。