Fopen accept self signed certificate
Asked Answered
L

2

8

i would like to get the result of my page in https in a php variable but the fopen fonction return false;

i think this error can be product by the ssl certifacte which is self signed

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

Is it a php ssl configuration to accept all certificate ?

Lupien answered 28/9, 2015 at 10:6 Comment(0)
E
14

see Error when loading external xml file with php via https : SSL3_GET_SERVER_CERTIFICATE

Export your self-signed certificate PEM-encoded and append it to ca-bundle.crt (or if there is no ca-bundle.crt yet, just rename your export file)

Then use

$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);

see SSL context options and stream_context_create

Epinephrine answered 28/9, 2015 at 10:44 Comment(0)
A
20

Check this - 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; ?>
Abreaction answered 28/9, 2015 at 10:44 Comment(1)
It should be $response = fopen("https://192.168.1.1:8443", 'rb', false, stream_context_create($opts)); You skipped 1 parameterCamarilla
E
14

see Error when loading external xml file with php via https : SSL3_GET_SERVER_CERTIFICATE

Export your self-signed certificate PEM-encoded and append it to ca-bundle.crt (or if there is no ca-bundle.crt yet, just rename your export file)

Then use

$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);

see SSL context options and stream_context_create

Epinephrine answered 28/9, 2015 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.