17

Hey i try to get header information of url ,

when i use the protocol http it's work fine , but when i use https it's not working

the url : https://200.35.78.130/

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
Warning: get_headers(): Failed to enable crypto in 
Warning: get_headers(https://200.35.78.130/): failed to open stream: operation failed in /

this is my code

print_r(get_headers("https://200.35.78.130/", 1));
Ahmed Safadi
  • 433
  • 1
  • 6
  • 19

2 Answers2

36

That error occurs when you're trying to access a URL without a valid SSL certificate. You can work around this by overriding the default stream context, which will affect all subsequent file operations (including remote URLs)

<?php
stream_context_set_default( [
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
    ],
]);

print_r(get_headers('https://200.35.78.130/'));

Alternatively, if you're using PHP 7.1+, you can create a temporary context using stream_context_create and pass it directly to the function, to avoid overriding the default:

$context = stream_context_create( [
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
    ],
]);

print_r(get_headers('https://200.35.78.130/', 0, $context));

Thanks to Matthijs Wessels in the comments for pointing it out.

iainn
  • 16,826
  • 9
  • 33
  • 40
  • 1
    damn, 5 minutes to get the answer you needed - that's impressive. – Dan Searle Apr 21 '17 at 08:41
  • 3
    get_headers in php7 now also optionally take a context as a parameter. You can create a context using [stream_context_create](https://www.php.net/manual/en/function.stream-context-create.php). – Matthijs Wessels Nov 12 '19 at 14:40
  • 1
    For anyone looking to use self-signed SSL certificates WITHOUT disabling SSL verification, [check out this answer](https://stackoverflow.com/a/28701786/1717535) instead. – Fabien Snauwaert Dec 08 '21 at 21:47
-2

Because you installed the SSL certificate without an intermediate key

aydo000
  • 37
  • 4