Fopen 接受自签名证书

Fopen accept self signed certificate

我想在 php 变量中获取 https 页面的结果,但是 fopen 函数 return false;

我认为这个错误可能是由自签名的 ssl 证书引起的

fopen("https://192.168.1.1:8443", "rb"))

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

是否接受所有证书的 php ssl 配置?

导出 PEM 编码的自签名证书并将其附加到 ca-bundle.crt(或者如果还没有 ca-bundle.crt,只需重命名导出文件)

然后使用

$context = stream_context_create(array('ssl'=>array(
    'verify_peer' => true,
    'cafile' => '/path/to/ca-bundle.crt'
)));
$fd = fopen("https://192.168.1.1:8443", "rb", false, $context);

SSL context options and stream_context_create

检查这个 - http://php.net/manual/en/function.stream-context-create.php

<?php
$opts=array(
    "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);  

$response = fopen("https://192.168.1.1:8443", 'rb', false, stream_context_create($opts));

echo $response; ?>