Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make https request using php Http_Request2()

i want to make a https request using pear http_request2($url) class. i am able to make http request but not https. and the website facilitate both http and https. No prob with server responding to 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;

this is the error msg:

$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)

like image 771
Necktwi Avatar asked Dec 12 '22 03:12

Necktwi


2 Answers

The comment to disable certificate verification was the key for me.

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

$request->setConfig(array(
    'ssl_verify_peer'   => FALSE,
    'ssl_verify_host'   => FALSE
));
like image 122
Mr. Lee Avatar answered Dec 28 '22 22:12

Mr. Lee


I experienced this problem. The fix (for me) was to use 'curl' as the adapter, for example :

$request = new Http_Request2('https://whatever');
$request->setAdapter('curl');
$response = $request->send();

See also the "SSL parameters" section on http://pear.php.net/manual/en/package.http.http-request2.config.php which says

Peer verification is likely to fail if you don't explicitly provide ssl_cafile and/or ssl_capath, especially with Socket adapter.

Presumably curl knows where to find a local CA file to trust.

(PHP 5.4.38 with curl).

like image 29
David Goodwin Avatar answered Dec 28 '22 22:12

David Goodwin