php - SSL 与 CURL PHP 无法正常工作的 2 种方式

标签 php ssl curl

在 SOAPUI 中,我只在 SSL 设置中导入 .pfx 文件,然后我测试 API 方法并且它可以正常工作。现在,我想在 SOAPUI 中为我的 PHP 应用程序实现相同的工作示例。所以,我使用 CURL PHP 来管理它。我所做的基本上是,将我在 SOAP UI 中使用的相同证书导入到我的 PHP 文件中。我想使用我的私钥使用我的公钥访问 protected 服务器。

更新:2018 年 6 月 29 日

PFX 文件:test-cert.pfx

 $url = 'https://domain.com.ph/api/id_num';

    $headers = array(
                    "Content-Type: application/json",
                    "token really_long_numbers",
                    );

    $curl = curl_init();

curl_setopt ($curl, CURLOPT_URL, $url );        
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_PORT , 443);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_SSLCERT, "/Applications/XAMPP/xamppfiles/htdocs/d/client.pem");
curl_setopt($curl, CURLOPT_SSLKEY, "/Applications/XAMPP/xamppfiles/htdocs/d/key.pem");
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, "******");
curl_setopt($curl, CURLOPT_CAINFO, "/Applications/XAMPP/xamppfiles/htdocs/d/cacert.pem");

我尝试使用下面的命令从 PFX 生成 CA

openssl pkcs12 -in test-cert.pfx -out test-cert.pem -clcerts

和/或

openssl pkcs12 -in test-cert.pfx -out test-cert.pem -clcerts -nodes

现在是新的错误

cURL Error #:SSL certificate problem: unable to get local issuer certificate

更新:2018 年 7 月 2 日

我已经在 php.ini 文件中添加了以下行,当然,我还从 http://curl.haxx.se/ca/cacert.pem 下载了 cacert.pem 文件。

[ curl ] ; CURLOPT_CAINFO 选项的默认值。这必须是一个 ;绝对路径。

curl.cainfo="/Applications/XAMPP/xamppfiles/etc/openssl/certs/cacert.pem"
openssl.cafile="/Applications/XAMPP/xamppfiles/etc/openssl/certs/cacert.pem"

我的php.ini配置文件所在

Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

并在 curl 中添加以下行

CURLOPT_CAINFO => '/path/to/my/exported/cacert.pem'

CURLOPT_CAPATH => '/path/to/my/exported/cacert.pem'

我还在 apache 配置和 php.ini 中启用了以下行 php_openssl.dllmod_ssl

最后,我也在 cacert.pem 位置下载了 ca-bundle.crt。

还是不行。

最佳答案

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_PORT , 443);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_SSLCERT, getcwd() . "/my_cert.pem");
curl_setopt($curl, CURLOPT_SSLKEY, getcwd() . "/my_key.pem");
curl_setopt($curl, CURLOPT_CAINFO, getcwd() . "/cacert.pem");

您需要指定所有 3 个 - 您的私钥 (SSLKEY)、您的公共(public)证书 (SSLCERT) 和直到根 CA 的证书授权链 (CAINFO)。 key 和证书应该是纯文本 PEM 格式而不是 PFX

openssl pkcs12 -in test-cert.pfx -out test-cert.pem -nodes

更新:2018 年 6 月 29 日

下载http://curl.haxx.se/ca/cacert.pem (或 https://github.com/bagder/ca-bundle/blob/e9175fec5d0c4d42de24ed6d84a06d504d5e5a09/ca-bundle.crt 也包含 RSA-1024 证书)到 /Applications/AMPPS/extra/etc/openssl/certs/cacert.pem 然后更新 php.ini 并重启 Apache

[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
curl.cainfo="/Applications/AMPPS/extra/etc/openssl/certs/cacert.pem"
openssl.cafile="/Applications/AMPPS/extra/etc/openssl/certs/cacert.pem"

关于php - SSL 与 CURL PHP 无法正常工作的 2 种方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51022352/

相关文章:

php - cURL 正在从 Pirate Bay 检索编码的 HTML

php - readfile() 由于 SSL 而失败

javascript - Ajax 请求在 mozilla 中完美运行而不在 chrome 中运行(非常困惑,因为有时它可以运行)

php - 是否需要更改 select 语句的变量名称?

javax.net.ssl.SSLProtocolException : Connection timed out (Read failed)

ssl - 使用和解码原始 SSL 数据作为证据

php - 试图用单引号括起一个 php 变量

php - 计数大表精度

java - 我如何确保我导入到 cacert 的证书(myAppCertificate.crt)已经存在于 cacert 文件中?

.net - C++ 'GET' 请求或如何下载文件以在 C++ 中使用?