我已经安装了 WAMP 3.0.4,并尝试编写一个连接到外部 HTTPS Web 服务的 PHP 脚本。但这会返回错误:
警告:file_get_contents():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败
我写了一个简短的脚本来演示这个问题:
<?php
$auth = base64_encode('username:password');
$aContext = array(
'http' => array(
'proxy' => 'tcp://proxyip:proxyport',
'request_fulluri' => true,
'header' => 'Proxy-Authorization: Basic $auth'
),
'SSL' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
'cafile' => 'C:/wamp/certificates/cacert.pem'
)
);
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents("https://www.google.com", False, $cxContext);
echo $sFile;
?>
要求使用代理服务器。
可以看出,我尝试安装根证书包并将 verify_peer 添加到 false (不是说我会在生产中这样做),但我仍然收到此错误。
从上面可以清楚地看出,我是一个Apache/WAMP新手。有人可以解释我所缺少的吗?
如果你想disable您可以使用的 SSL 连接验证:
'verify_peer' => false
在你的代码中:
<?php
$auth = base64_encode('username:password');
$aContext = array(
'http' => array(
'proxy' => 'tcp://proxyip:proxyport',
'request_fulluri' => true,
'header' => "Proxy-Authorization: Basic $auth"
),
'ssl' => array(
'verify_peer' => false,
),
);
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents("https://www.google.com", False, $cxContext);
echo $sFile;
?>
但请注意,这意味着没有人可以保证您获得的数据是真实的(因为 ssl 证书未经验证)。
如果您想验证证书,您应该使用根证书然而,就像在你的问题中一样,你说你使用 WAMP,所以你的 cafile 的路径应该是这样的:
"cafile" => "c:/wamp/certificates/cacert.pem",
更重要的是——你什么也没说proxy
在你的问题中。它是您需要的东西还是您在某处找到并只是想使用的东西?
如果您不需要代理,只需从您的请求中删除 if 即可。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)