ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php – file_get_contents():SSL操作失败,代码为1(证书验证失败)

2019-05-27 21:15:42  阅读:997  来源: 互联网

标签:apache php wamp


我已经安装了WAMP 3.0.4并且正在尝试编写一个连接到外部HTTPS Web服务的PHP脚本.但是这会返回错误:

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新手.也许某人可能会解释我所缺少的东西?

解决方法:

如果要禁用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",

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?

如果您不需要代理,请从您的请求中删除.

标签:apache,php,wamp
来源: https://codeday.me/bug/20190527/1166049.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有