PHP客户端验证https证书


PHP Client to verify https certificates

我需要创建一个php,将作为一个客户端,并在https下使用一些web服务。我的问题是我还想验证服务器证书。我需要知道我有正确的服务器,并且中间没有人充当服务器。有人能帮帮我吗?

谢谢!

如果你有curl扩展,它可以配置为在连接时验证证书。

http://php.net/manual/en/function.curl-setopt.php

// As of writing this, Twitter uses Verisign, Google uses Eqifax
$exampleUrl = 'https://twitter.com/'; // Success
$exampleUrl = 'https://google.com/';  // Fails
// create a new CURL resource
$ch = curl_init($exampleUrl);
// enable verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
// list of CAs to trust
// If the remote site has a specific CA, they usually have a .crt
// file on their site you can download.  Or you can export key items from
// some browsers.
// In this example, using: Verisign [1]
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/ca_bundle.crt');
// - or -
curl_setopt($ch, CURLOPT_CAPATH, __DIR__ . '/ca_certs/');
// If the remote site uses basic auth:
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
// And a helpful option to enable while debugging
//curl_setopt($ch, CURLOPT_VERBOSE, true);
// defaults to stdout, don't want that for this case.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);

[1] http://www.verisign.com/support/verisign-intermediate-ca/extended-validation/apache/

看起来在Curl 7.10中,这些都是默认选中的:
http://php.net/manual/en/function.curl-setopt.php


CURLOPT_SSL_VERIFYPEER

FALSE停止cURL验证对端证书。可以使用CURLOPT_CAINFO选项指定要验证的备用证书,或者可以使用CURLOPT_CAPATH选项指定证书目录。

在cURL 7.10中默认为TRUE。


CURLOPT_SSL_VERIFYHOST

1检查SSL对端证书中是否存在通用名称。2检查是否存在通用名称,并验证它是否与所提供的主机名匹配。在生产环境中,该选项的值应该保持为2(默认值)。