11.2 Performing an SSL Transaction


You want to use PHP to connect to an SSL server.

Technique

Use the Net_Curl module from PEAR, which interfaces with PHP's curl extension:

 <?php include_once 'Net/Curl.php'; // Initialize the transfer $conn = new Net_Curl('https://secureserver.com'); if (Net_Curl::isError($conn)) {     die(sprintf('Error [%d]: %s',                 $conn->getCode(), $conn->getMessage())); } // Set the transfer options //(optional) $conn->sslVersion = 3; $conn->sslCert = 'file_name_of_certificate'; $conn->sslCertPasswd = 'secret'; // Execute the transfer $data = $conn->execute(); if (Net_Curl::isError($conn)) {     die(sprintf('Error [%d]: %s',                $data->getCode(), $data->getMessage())); } print "The results of your transaction were: \n<br>\n"; print $data; ?> 

Comments

The curl extension to PHP interfaces with an external library, libcurl, which is freely available from http://curl.haxx.se. In the solution, we use the Net _ Curl package, which is freely available from PEAR at http://pear.php.net/.Net_Curl provides an easy interface to perform transactions with PHP's curl extension.

If you don't want to use PEAR's Net_Curl package, you can also directly interface with the curl extension:

 <?php // Initialize the transfer $ch = curl_init('https://secureserver.com'); if (!$ch) {     die(sprintf('Error [%d]: %s',                 curl_errno($ch), curl_error($ch))); } //Set the transfer options //(optional) curl_setopt($ch, CURLOPT_SSLVERSION, 3); curl_setopt($ch, CURLOPT_SSLCERT, 'ssl_certificate'); curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'secret'); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Execute the transfer $data = curl_exec($ch); if (!$data) {     die(sprintf('Error [%d]: %s',                 curl_errno($ch), curl_error($ch))); } // Close the transfer curl_close($ch); print "The results of your transfer were:\n<br>\n"; print $data; ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net