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

C:\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
...

Last update: 2011.

Table of Contents

 About This Book

 Introduction of PKI (Public Key Infrastructure)

 Introduction of HTTPS (Hypertext Transfer Protocol Secure)

 Using HTTPS with IE (Internet Explorer) 10

 Using HTTPS with Chrome 40

 Using HTTPS with Firefox 35

 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

 Certificate Stores and Certificate Console

 .NET Programs Communicating with HTTPS Servers

 CAcert.org - Root CA Offering Free Certificates

 PKI CA Administration - Issuing Certificates

 Digital Signature - Microsoft Word 2007

 Digital Signature - OpenOffice.org 3

 S/MIME and Email Security

 PKI (Public Key Infrastructure) Terminology

 Outdated Tutorials

 References

 PDF Printing Version