采取的基本步骤:
# echo -n | openssl s_client -showcerts -connect example.com:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /tmp/example.cert
# curl -v --cacert /tmp/example.cert https://example.com/
* About to connect() to example.com port 443 (#0)
* Trying 123.45.67.89... connected
* Connected to example.com (123.45.67.89) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: example.cert
CApath: none
* NSS error -12188
* Closing connection #0
* SSL connect error
curl: (35) SSL connect error
我尝试使用证书链的 pem,也尝试使用普通的主机证书。
有时我也会收到错误Problem with the SSL CA cert (path? access rights?)
。
也尝试过(作为临时解决方法):
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
# echo 'insecure' > $CURL_HOME/.curlrc
但最终我还是会选择SSL connect error
。
答案1
不要使用 curl。
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => http_build_query(array([...])),
'protocol_version' => 1.1,
'timeout' => 10,
'ignore_errors' => true
)
));
$result = file_get_contents('https://example.com/', false, $context);
stream_context_create
效果一样好。