PHP客户端验证https证书

时间:2011-10-04 14:50:10

标签: php web-services ssl https

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

谢谢!

2 个答案:

答案 0 :(得分:6)

如果您有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/

答案 1 :(得分:0)

看起来像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。从cURL 7.10开始安装的默认包。


  

CURLOPT_SSL_VERIFYHOST

     

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