"unable to get local issuer certificate" 尝试访问 Microsoft Graph 时

"unable to get local issuer certificate" when trying to access Microsoft Graph

我想配置我的 Symfony4 应用程序以使用 msgraph-sdk-php 库阅读和发送电子邮件。

我的初体验是这段代码:

    $guzzle = new \GuzzleHttp\Client();
    $url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
    $token = json_decode($guzzle->post($url, [
        'form_params' => [
            'client_id' => $clientId,
            'client_secret' => $clientSecret,
            'resource' => 'https://graph.microsoft.com/',
            'grant_type' => 'client_credentials',
        ],
        'verify' => false
    ])->getBody()->getContents());
    $accessToken = $token->access_token;

    $graph = new Graph();
    $graph->setAccessToken($accessToken);

    try {
        $user = $graph->createRequest("GET", "/me")
            ->setReturnType(User::class)
            ->execute();
    } catch (GraphException $e) {
        $user=(object) ['getGivenName'=>$e->getMessage()];
    }

    return "Hello, I am $user->getGivenName() ";

但是 Symfony 向我显示了一个异常页面,其中包含以下消息:

cURL error 60: SSL certificate problem: unable to get local issuer certificate

我应该怎么做才能克服这个问题?

在 Windows 系统上,cURL 有时无法访问 CA-Certs。所以你必须下载 CA-Files 并将它们添加到 Curl。您可以在这里下载证书:

http://curl.haxx.se/docs/caextract.html

curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/certs/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

因此,要临时解决问题,您可以禁用对等验证,但您应该只在测试时这样做。

$client->setDefaultOption('verify', false);

那么应该可以连接了。要将证书添加到以下内容,但您必须先下载证书。

$client = new \GuzzleHttp\Client();
$client->setDefaultOption('verify', 'curl-ca-bundle.crt');

或者最后一个解决方案将 ca 文件添加到您的 php.ini(来自 curl.haxx.se 的文件):

curl.cainfo = "[pathtothisfile]\cacert.pem"