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) 2010-2018 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.

herong> \local\php\php OpenSSL_HTTPS_fopen_with_CA.php \
   https://www.google.com/accounts/ServiceLogin

<html>
<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.

herong> \local\php\php OpenSSL_HTTPS_fopen_with_CA.php \
   https://www.godaddy.com

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()

 Testing OpenSSL with fsockopen()

 Adding CA Certificates for the PHP Engine

 Testing OpenSSL with stream_socket_client()

 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

 Archived Tutorials

 References

 Full Version in PDF/EPUB