当前位置: 首页 > 知识库问答 >
问题:

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

游勇军
2023-03-14

我已经安装了WAMP3.0.4,并试图编写一个连接到外部HTTPS web服务的PHP脚本。但这将返回错误:

<?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;
?>

从上面可以清楚地看出,我是Apache/WAMP的新手。也许有人能解释一下我错过了什么吗?

共有1个答案

庾鸿飞
2023-03-14
'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;
?>
"cafile" => "c:/wamp/certificates/cacert.pem",

更重要的是,您在问题中没有提到代理。它是你需要的东西,还是你在某个地方找到的东西,只是想用?

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

 类似资料: