2016-08-12 275 views
1

我已经安装了WAMP 3.0.4,并且正在尝试编写连接到外部HTTPS Web服务的PHP脚本。但是,这将返回错误:file_get_contents():SSL操作失败,代码为1(证书验证失败)

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

我写了证明的问题很短的脚本:

<?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新手。有人可能可以解释我失踪了吗?

回答

3

如果你想禁用 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" => "c:/wamp/certificates/cacert.pem", 

More important - you said nothing regarding the proxy in your question. Is it something that you need or is it something you found somewhere and just trying to use?

如果您不需要代理,只需从您的请求中删除即可。

+0

感谢您花时间回复。不幸的是,我已经尝试了这些东西,仍然收到错误。我编辑了这个问题,试图使这个更清楚。 – BrightonBoy

0

虽然在macOS上macOS,只需重新启动MAMP服务器解决了我这个问题。

相关问题