SSL support for fsockopen
I'm trying to get an SSL connection using fsockopen but it's giving me this error.
Quote:
Originally Posted by PHP error
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://www.domain.com:443 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in [__my path__] on line 291
Here's my fsockopen code:
PHP Code:
function sendToHost($host,$port=80,$method,$path,$data,$useragent=0)
{
$buf='';
// Supply a default method of GET if the one passed was empty
if (empty($method)) {
$method = 'GET';
}
$method = strtoupper($method);
$fp = fsockopen($host, $port,$errNo,$errStr);
if (!$fp) {
die('<div class="error"><strong>PHP:</strong> Error connecting to '.$host.'</div>');
}
if ($method == 'GET') {
$path .= '?' . $data;
}
fputs($fp, "$method $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp,"Content-type: application/x-www-form- urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($data) . "\r\n");
if ($useragent) {
fputs($fp, "User-Agent: MSIE\r\n");
}
fputs($fp, "Connection: close\r\n\r\n");
if ($method == 'POST') {
fputs($fp, $data);
}
while (!feof($fp)) {
$buf .= fgets($fp,128);
}
fclose($fp);
if (empty($errStr)) {
return $buf;
}
else {
return $errStr;
}
}
Normal HTTP connections work, but not HTTPS connections. It says I need to configure it in PHP, but I don't know where to start.
I'm lost.
Sample code showing fsockopen used with an SSL connection
Once you have OpenSSL running within PHP, you can connect to a secure web site using port 443, and the url format of ssl://
Below is some conceptual code, which posts two form fields (REG and TRANSACTIONTYPE) using the x-www-form-urlencoded content type, to a page called index.php on the web site securesite.com. The data returned by that index.php page is then read into a variable called $_return, and printed out to the web browser.
Example Code
<?php
$http_data = 'REG=K9 DDR';
$http_data .= '&TRANSACTIONTYPE=03';
$fp = fsockopen("ssl://www.securesite.com",
443, $errno, $errstr, 15);
if (!$fp) {
$_return = ' error: ' . $errno . ' ' . $errstr;
die $_return;
} else {
$http = "POST /index.php HTTP/1.1\r\n";
$http .= "Host: " . $_SERVER['HTTP_HOST'] . "\r\n";
$http .= "User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n";
$http .= "Content-Type: application/x-www-form-urlencoded\r\n";
$http .= "Content-length: " . strlen($http_data) . "\r\n";
$http .= "Connection: close\r\n\r\n";
$http .= $http_data . "\r\n\r\n";
fwrite($fp, $http);
while (!feof($fp)) {
$_return .= fgets($fp, 4096);
}
fclose($fp);
echo $_return;
}
?>
__________________________
RapidSSL Certificate
Thawte SSL Certificate