使用Guzzle传递客户端证书的Curl请求

17

我有以下的curl命令

sudo curl -E openyes.crt.pem --key openyes.key.pem https://sky.myapitutorial.in:444/app/live/get

这个功能本来是可以正常运行的。但是当我试着使用 Guzzle 实现时,它失败了。

我无法将客户端证书传递到请求中。

这是我的尝试:

$headers = ['Content-Type' => 'application/json','X-Client-Id' => config('mykey') , 'X-Client-Secret' => config('mykey')];

        $client = new client();

        try {
            $response = $client->post(
                $endpoint
                , 
                ['json' => $content, 'headers' => $headers,['connect_timeout' => 650]],
                [
                    'config' => [
                        'curl' => [
                            'CURLOPT_SSLKEY' => base_path().'/openyes.key.pem',
                            'CURLOPT_SSLCERT' => base_path().'/openyes.crt.pem',
                            'CURLOPT_VERBOSE' => true
                        ],
                    ]
                ],
                ['debug'=>true],
                ['http_errors' => false]
            );

            dd($response);

        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            throw $e;
        }

我在Guzzle的文档中找不到任何解决方案。

不知道为什么这不起作用?

我收到的错误信息是:

cURL error 35: error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure (see http:\/\/curl.haxx.se\/libcurl\/c\/libcurl-errors.html)
1个回答

43

你可以使用 ssl_keycert

$response = $client->post(
    $endpoint, [
        'json' => $content,
        'headers' => $headers,
        'connect_timeout' => 650,
        // add these
        'cert' => '/path/to/openyes.crt.pem',
        'ssl_key' => '/path/to/openyes.key.pem'
    ]
);

如果他们有密码短语,您可以像这样设置它们:

        'cert' => ['/path/to/openyes.crt.pem', 'password'],
        'ssl_key' => ['/path/to/openyes.key.pem', 'password']

我现在遇到了cURL错误58:无法设置私钥文件:'/Users/ajeesh/Documents/Open/open-sme-backend-live/private.key',类型为PEM(请参见http://curl.haxx.se/libcurl/c/libcurl-errors.html)。我的私钥有密码。我该如何传递它? - Ajeesh
我在我的答案中加入了一个例子,将密码作为第二个元素存储在数组中。 - Federkun
1
你很快就会得到你的赏金 :) 它像魔法一样奏效了! - Ajeesh
1
很高兴听到这个消息 :D - Federkun
您可能还需要添加 'verify' => false, - ejntaylor
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接