php - 使用 php 和 curl 抓取客户端证书请求

标签 php ssl curl

我正在尝试下载一个需要通过客户端数字证书进行身份验证的文件,我已经有了证书,但我不知道如何在 curl 中配置它。

$useragent = '...';
$post = array( ... );
$certPass = '123456';
$certPath = _DIR_PATH.'cert/';
$certPfx = $certPath.'certificate.pfx';
$cert = $certPath.'certificate.pem';

$url = 'https://www.url.com/path/to/access';

$ch = curl_init( $url );
$options = array(
          CURLOPT_FAILONERROR => true,
                        CURLOPT_RETURNTRANSFER => true,
                        CURLOPT_AUTOREFERER => true,
                        CURLOPT_HEADER => true,
                        CURLOPT_NOBODY => true,
                        CURLOPT_CAINFO => $cert,
                        CURLOPT_CAPATH => $certPath,
                        CURLOPT_SSH_PRIVATE_KEYFILE => $certPfx,
                        CURLOPT_SSLCERT => $cert,
                        CURLOPT_SSLCERTPASSWD => $certPass,
                        CURLOPT_SSL_VERIFYHOST => 2,
                        CURLOPT_SSL_VERIFYPEER => true,
                        CURLOPT_POST => true,
                        CURLOPT_POSTFIELDS => $post,
                        CURLOPT_USERAGENT => $useragent,
                        CURLOPT_COOKIE => 'ASP.NET_SessionId='.$cookie
                    );
curl_setopt_array( $ch, $options );
$resp = curl_exec($ch);

$ch_errno = curl_errno($ch);
$ch_erro = curl_error($ch);

curl_close($ch);

我总是收到消息:SSL 证书问题:无法获取本地颁发者证书。

有人可以帮助我吗?

最佳答案

PHP cURL:修复“SSL 证书问题:无法获取本地颁发者证书”错误。

如果您使用 PHP 的 cURL 函数连接到 HTTPS URL,您可能会遇到以下错误:

SSL 证书问题:无法获取本地颁发者证书。 ( curl 错误代码 60)

这是一个常见错误,每当您尝试使用 PHP 的 cURL 函数连接到 HTTPS 网站时都会发生。本质上,您的 cURL 客户端尚未配置为连接到支持 SSL 的网站。

快速修复。

CURLOPT_SSL_VERIFYHOST: This option tells cURL that it must verify the host name in the server cert.
CURLOPT_SSL_VERIFYPEER: This option tells cURL to verify the authenticity of the SSL cert on the server.

例如。

$url = 'https://google.com';

//Initiate cURL.
$ch = curl_init($url);

//Disable CURLOPT_SSL_VERIFYHOST and CURLOPT_SSL_VERIFYPEER by
//setting them to false.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

//Execute the request.
curl_exec($ch);

//Check for errors.
if(curl_errno($ch)){
    throw new Exception(curl_error($ch));
}

关于php - 使用 php 和 curl 抓取客户端证书请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56176718/

相关文章:

linux - 从 json 文件 curl 下载 json 文件

java - 具有相互身份验证的 Https 请求通过 curl 但通过 java 失败

C# request.GetClientCertificate() 在处理具有较大负载的 PUT/POST 请求时卡住

php - 警告 : mysql_connect() [function. mysql-connect]:主机 'xyz' 由于许多连接错误而被阻止;

php - FFmpeg 可以连接来自不同域的文件吗?

java - 无法下载站点地图 : SSLHandshakeException: Received fatal alert: handshake_failure

Bash:将函数结果输出到Sed参数中

php - curl 跨源请求需要用户代理才能工作吗?

php - 在 laravel 中创建的类/不同的命名空间中访问和使用 eloquent

php - 如何从数组中的较大字符串中选择一个字符串并将其按名称分组?