View Full Version : SSL support for fsockopen
tech_support
12-01-2007, 03:50 AM
I'm trying to get an SSL connection using fsockopen but it's giving me this 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:
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.
Master_script_maker
12-01-2007, 11:32 PM
http://us2.php.net/manual/en/function.fsockopen.php#75388
Check here
tech_support
12-02-2007, 02:28 AM
Same error.
Oh, and BTW, here's how I use the function:
echo sendToHost('ssl://www.domain.com/',443,'POST','/index.php','');
thetestingsite
12-02-2007, 02:39 AM
Well, there's nothing wrong with the function; but if you need to get rid of the trailing slash on the host name in the last code you posted. I have not received that error that you describe in your first post, but I did receive a Bad Request error (connecting to paypal.com using ssl on port 443). I will look at my php.ini file to see what there could be in there that would make it show that error.
Hope this helps for now.
tech_support
12-02-2007, 02:55 AM
Hm... Thanks. I'm still having the same error though.
thetestingsite
12-02-2007, 03:06 AM
Just a quick question; are you on a windows or a linux server and what version of php do you have installed? Either way; if you have php4, ssl will not work with fsockopen; however, if you have php5, you need to have the openssl module installed/enabled.
Hope this helps.
check the following websites to get more information:
http://www.google.com/search?q=Unable+to+find+the+socket+transport+%22ssl%22&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
tech_support
12-02-2007, 03:07 AM
I'm developing this thing on a Windows server, but moving it into a Linux server. Both have PHP5 installed with OpenSSL.
thetestingsite
12-02-2007, 03:13 AM
Not sure then. All of the documentation and whatnot state that you have to have the OpenSSL php module enabled for it (ssl connection using fsockopen) to work properly. Other than what I have stated above, I have no idea.
tech_support
12-02-2007, 03:16 AM
Yeah. It's strange. Here's my phpinfo() output if anyone's interested.
http://home.alotofstuffhere.com/phpinfo.php
thetestingsite
12-02-2007, 03:19 AM
Ok, it looks as if you do not have the openssl module for php installed/enabled. Take a look at my phpinfo (http://www.thetestingsite.net/phpinfo.php) and compare. If you are running it on a Windows server (I assume you are using a WAMP server program on your personal computer), you will have to enable it in your php.ini file. It would be under extension=openssl.dll (Windows) or extension=openssl.so (Linux).
Hope this helps.
tech_support
12-02-2007, 03:26 AM
Can you point me to where I can find the openssl.dll? It's not even in my php.ini file. Thanks.
And I installed everything separately, it wasn't a package.
thetestingsite
12-02-2007, 03:30 AM
Note: Note to Win32 Users In order for this extension to work, there are DLL files that must be available to the Windows system PATH. See the FAQ titled "How do I add my PHP directory to the PATH on Windows" for information on how to do this. Although copying DLL files from the PHP folder into the Windows system directory also works (because the system directory is by default in the systems PATH), it is not recommended. This extension requires the following files to be in the PATH: libeay32.dll
Additionally, if you are planning to use the key generation and certificate signing functions, you will need to install a valid openssl.cnf on your system. As of PHP 4.3.0, we include a sample configuration file in the openssl folder of our win32 binary distribution. If you are using PHP 4.2.0 or later and are missing the file, you can obtain it from » the OpenSSL home page or by downloading the PHP 4.3.0 release and using the configuration file from there. PHP will search for the openssl.cnf using the following logic:
* the OPENSSL_CONF environmental variable, if set, will be used as the path (including filename) of the configuration file.
* the SSLEAY_CONF environmental variable, if set, will be used as the path (including filename) of the configuration file.
* The file openssl.cnf will be assumed to be found in the default certificate area, as configured at the time that the openssl DLL was compiled. This is usually means that the default filename is c:\usr\local\ssl\openssl.cnf.
In your installation, you need to decide whether to install the configuration file at c:\usr\local\ssl\openssl.cnf or whether to install it someplace else and use environmental variables (possibly on a per-virtual-host basis) to locate the configuration file. Note that it is possible to override the default path from the script using the configargs of the functions that require a configuration file.
Hope this helps.
or you can view the installation faq: http://www.php.net/manual/en/faq.installation.php#faq.installation.addtopath
tech_support
12-02-2007, 03:31 AM
Where can I find the DLL files? :p
Nevermind. Got a non-developer version of PHP and it had all the DLL's.
thetestingsite
12-02-2007, 03:35 AM
It's been a little while since I installed PHP on a Windows machine, but I believe it is in the PHP_ROOT_DIR extensions folder or something to that effect. But, being that you are on a Windows machine, you can do a search for openssl using the "Search" function.
Hope this helps.
tech_support
12-02-2007, 03:39 AM
Hm.. this looks like fun.
To use the CSR and key generation functions from PHP, you will need to install
an openssl.cnf file. We have included a sample file that can be used for this
purpose in this folder alongside this readme file.
The default path for the openssl.cnf file is determined as follows:
OPENSSL_CONF environmental variable, if set, is assumed to hold the
path to the file.
If it is not set, SSLEAY_CONF environmental variable is checked next.
If neither are set, PHP will look in the default certificate area that was set
at the time that the SSL DLLs were compiled. This is typically
"C:\usr\local\ssl\openssl.cnf".
If the default path is not suitable for your system, you can set the
OPENSSL_CONF variable; under windows 95 and 98 you can set this variable in
your autoexec.bat (or the batch file that starts your webserver/PHP).
Under NT, 2000 and XP you can set environmental variables using "My Computer"
properties.
If setting an environmental var is not suitable, and you don't want to install
the config file at the default location, you can override the default path
using code like this:
$configargs = array(
"config" => "path/to/openssl.cnf"
);
$pkey = openssl_pkey_new($config);
$csr = openssl_csr_new($dn, $pkey, $config);
Please consult the online manual for more information about these functions.
NOTE!
Windows Explorer gives special meaning to files with a .cnf extension.
This typically means that editing the file from the explorer (by double or
right-clicking) will be difficult or impossible depending on your setup.
It is often easier to open the file from within the editor.
You can avoid this issue by naming the file something else (you might need to
rename the file using a DOS box) and then setting up an environmental variable
as described above.
(For anyone else going through the same trouble as I am)
tech_support
12-02-2007, 03:49 AM
Where can I find my openssl.cnf anyway?
Oh, and here's even more debug info:
[Sun Dec 02 14:31:53 2007] [notice] Parent: Created child process 3016
[Sun Dec 02 14:31:54 2007] [notice] Child 5972: Released the start mutex
PHP Warning: PHP Startup: openssl: Unable to initialize module\nModule compiled with module API=20060613, debug=0, thread-safety=1\nPHP compiled with module API=20071006, debug=0, thread-safety=1\nThese options need to match\n in Unknown on line 0
[Sun Dec 02 14:31:54 2007] [notice] Child 3016: Child process is running
[Sun Dec 02 14:31:54 2007] [notice] Child 3016: Acquired the start mutex.
[Sun Dec 02 14:31:54 2007] [notice] Child 3016: Starting 250 worker threads.
[Sun Dec 02 14:31:54 2007] [notice] Child 3016: Starting thread to listen on port 443.
[Sun Dec 02 14:31:54 2007] [notice] Child 3016: Starting thread to listen on port 80.
[Sun Dec 02 14:31:55 2007] [notice] Child 5972: Waiting for 250 worker threads to exit.
[Sun Dec 02 14:31:55 2007] [notice] Child 5972: All worker threads have exited.
Error in my_thread_global_end(): 1 threads didn't exit
[Sun Dec 02 14:32:00 2007] [notice] Child 5972: Child process is exiting
Straight from error.log
tech_support
12-09-2007, 02:47 AM
So NO ONE knows?
seo4ssl
11-05-2009, 07:58 AM
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 (http://www.clcikssl.com)
Thawte SSL Certificate (http://clickssl.blogspot.com)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.