Testing OpenSSL with fopen()

This section provides a tutorial example on how to test the PHP OpenSSL module with the fopen() function to run a GET request on an HTTPS server.

So far, I have only used the file_get_contents() function in my HTTPS tests. But the OpenSSL module does support other PHP functions to communicate with HTTPS servers.

Here is another tutorial example that uses the fopen() function:

<?php # OpenSSL_HTTPS_fopen_with_CA.php
#- Copyright (c) 2011, HerongYang.com, All Rights Reserved.
$script = array_shift($argv);
$url = array_shift($argv);

$context = stream_context_create(array(
    'ssl' => array(
        'verify_peer' => true,
        'cafile' => 'CA_Bundle.crt'

$handle = fopen($url, 'r', false, $context);
while ( ($line = fgets($handle)) !== false) {
  echo "$line\n";

Run this example code with Google HTTPS server. It works.

C:\herong>\local\php\php OpenSSL_HTTPS_fopen_with_CA.php

<style type="text/css">
  body { font-family: arial,sans-serif; background-color: #fff; ...
  td {font-family: arial, sans-serif;}
  .c { width: 4; height: 4; }
  a:link { color: #00c; }

Run this example code with GoDaddy HTTPS server. It does not work. I think you know why.

C:\herong>\local\php\php OpenSSL_HTTPS_fopen_with_CA.php

PHP Warning:  fopen(): SSL operation failed with code 1.
OpenSSL Error messages:
error:14090086:SSL routines:func(144):reason(134)
in OpenSSL_HTTPS_fopen_with_CA.php on line 14

Table of Contents

 About This Book

 Introduction of PKI (Public Key Infrastructure)

 Introduction of HTTPS (Hypertext Transfer Protocol Secure)

 Using HTTPS with Google Chrome

 Using HTTPS with Mozilla Firefox

 HTTPS with Microsoft Edge

 Using HTTPS with Apple Safari

 HTTPS with IE (Internet Explorer)

 Android and Server Certificate

 iPhone and Server Certificate

 Windows Certificate Stores and Console

 RDP (Remote Desktop Protocol) and Server Certificate

 macOS Certificate Stores and Keychain Access

 Perl Scripts Communicating with HTTPS Servers

PHP Scripts Communicating with HTTPS Servers

 Configuring PHP OpenSSL on Windows

 Testing OpenSSL with file_get_contents()

 OpenSSL Configuration Errors

 SSL Context Options for OpenSSL

 Asking OpenSSL to Verify Server's Certificate

 OpenSSL Failing to Verify Server's Certificate

 Multiple CA Certificates in a Single File

Testing OpenSSL with fopen()

 Java Programs Communicating with HTTPS Servers

 .NET Programs Communicating with HTTPS Servers

 CAcert.org - Root CA Offering Free Certificates

 PKI CA Administration - Issuing Certificates

 Comodo Free Personal Certificate

 Digital Signature - Microsoft Word

 Digital Signature - OpenOffice.org 3

 S/MIME and Email Security

 PKI (Public Key Infrastructure) Terminology

 Outdated Tutorials


 Full Version in PDF/EPUB