忽略Zend php中的SSL证书

时间:2017-01-14 04:52:00

标签: php ssl zend-framework ssl-certificate

我正在尝试从我的Zend客户端在远程Windows 2008服务器R2上发出API请求,每次我尝试这样做时,都会出现以下错误:

消息: 无法在TCP连接上启用加密wvm024.dei.isep.ipp.pt

和: 以前的例外:

ErrorException 文件: C:\ Program Files(x86)\ Zend \ ZendServer \ data \ apps \ http__default __ \ 0 \ TukPorto \ 1.0.0_118 \ TukPorto \ vendor \ zendframework \ zendframework \ library \ Zend \ Http \ Client \ Adapter \ Socket.php:281 信息: stream_socket_enable_crypto():对等证书CN = wvm024.wvdom024.dei.isep.ipp.pt' did not match expected CN= wvm024.dei.isep.ipp.pt'

我的代码如下:

    $username = WebApiServices::$username;
    $password = WebApiServices::$password;

    $enderecoBase = WebApiServices::$enderecoBase;

    $httpClientOptions = array(
        'adapter'      => 'Zend\Http\Client\Adapter\Socket',
        'persistent'=>false,

        'sslverifypeer' => false,
        'sslallowselfsigned' => true,
        'sslusecontext'=>true,

        'ssl' => array(
            'verify_peer' => false,
            'allow_self_signed' => true,
            'capture_peer_cert' => true,
        ),

        'useragent' => 'Feed Reader',
    );


    $client = new Client($enderecoBase . '/Token');
    $client->setOptions($httpClientOptions);
    $client->setMethod(Request::METHOD_POST);
    $data = "grant_type=password&username=$username&password=$password";
    $len = strlen($data);
    $client->setHeaders(array(
        'Content-Type' => 'application/x-www-form-urlencoded',
        'Content-Length' => $len
    ));

   $client->setOptions([
      'sslverifypeer' => false,
 ]);
    $client->setRawBody($data);
    $response = $client->send();
    $body = Json::decode($response->getBody());
    if (! empty($body->access_token)) {
        if (! isset($_SESSION)) {
            session_start();
        }
        $_SESSION['access_token'] = $body->access_token;
        $_SESSION['username'] = $username;
        return true;
    } else
        return false;

1 个答案:

答案 0 :(得分:0)

可以使用CURL适配器,例如在ZF1中:

<?php
$config = array(
    'adapter'     => 'Zend_Http_Client_Adapter_Curl',
    'curloptions' => array(CURLOPT_SSL_VERIFYPEER => false),
);

$client = new Zend_Http_Client($url, $config);
$result = $client->request('GET');