【问题标题】:SSL error when trying to connect to Azure API尝试连接到 Azure API 时出现 SSL 错误
【发布时间】:2017-07-29 09:13:26
【问题描述】:

我正在尝试连接到 Azure API。我对此有点陌生。我正在使用示例中给出的 php 代码。我在本地服务器上使用 wamp。

我已经在 localhost 上配置了 SSL,这样我就可以打开 https://www.wamphelpers.dev/faces.php

但是我不断收到以下错误:

 Failed to enable crypto stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in <b>C:\wamp\bin\php\php5.5.12\pear\HTTP\Request2\Adapter\Socket.php

My code is below:

<?php
// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
require_once 'HTTP/Request2.php';

$request = new Http_Request2('https://southeastasia.api.cognitive.microsoft.com/face/v1.0/persongroups/{personGroupId}');
$url = $request->getUrl();


$headers = array(
    // Request headers
    'Content-Type' => 'application/json',
    'Ocp-Apim-Subscription-Key' => 'keygoeshere',
);

$request->setHeader($headers);

$parameters = array(
    // Request parameters
    'personGroupId' => 'heroes',
);



$url->setQueryVariables($parameters);

$request->setMethod(HTTP_Request2::METHOD_PUT);

// Request body
$request->setBody("heroes");

try
{
    $response = $request->send();
    echo $response->getBody();
}
catch (HttpException $ex)
{
    echo $ex;
}

?>

根据我的阅读,这是一个证书问题,我检查了我的 php 设置并确保它指向正确的文件。 输出如下:

OPENSSL_CONF    C:\OpenSSL-Win32\bin\openssl.cfg

我读了一点,似乎我可以使用 context_options 通过将验证对等设置为 false 来禁用 PHP 中的 SSL 检查。

但是我如何使用它来点击 URL。

以下是 API 参考链接。

【问题讨论】:

    标签: php azure ssl https


    【解决方案1】:

    只需指向 HTTP/Request2 即可使用 CA 包 as instructed here

    ssl_cafile

    用于验证对等点的证书授权文件(当 ssl_verify_peer 为 TRUE 时使用)。您可以使用例如cURL's CA Extract tool获取这样的文件。

    $request = new HTTP_Request2();
    $request->setConfig(array(
        'ssl_cafile' => '/path/to/cacert.pem'
    ));
    

    现在可以对照整个链验证远程证书。

    【讨论】:

    • 谢谢,所以如果我将它放在名为 cert 的文件夹中的 php 文件的位置,代码将类似于: $request->setConfig(array( 'ssl_cafile' => './cert /cacert.pem'));我尝试了上述和其他一些变体,但收到错误::无法启用加密流_socket_client():无法创建 SSL 句柄流_socket_client():无法设置验证位置./cert/cacert.pem' (null)'我知道它不能找到路径。但不知道如何添加它。
    • 尝试绝对路径/full/path/to/file.pem
    • 我也试过这个,但它不起作用。我在下面尝试: $request->setConfig(array( ''ssl_cafile' => 'C:\wamp\www\wamphelpers\cacert.pem' ));现在我收到错误:无法启用加密流_socket_client(): Peer certificate CN=*.cognitive.microsoft.com' did not match expected CN=southeastasia.api.cognitive.microsoft.com' in C:\wamp\bin\php\php5.5.12\pear\ HTTP\Request2\Adapter\Socket.php
    • PHP 5.5?那是老式的技术,试试更新的更好的 TLS 支持怎么样?切换到 PHP 7.0+。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    相关资源
    最近更新 更多