English 中文(简体)
How do I get SSL working in fsockopen?
原标题:

I m running PHP 5.2.6 on Windows, I have extension=php_curl.dll and extension=php_openssl.dll uncommented in php.ini; as such I can see the following in phpinfo:

curl
cURL support        enabled
cURL Information    libcurl/7.16.0 OpenSSL/0.9.8g zlib/1.2.3

openssl
OpenSSL support     enabled
OpenSSL Version     OpenSSL 0.9.8g 19 Oct 2007

I m not sure that having cURL enabled is vital to this, but since it mentioned OpenSSL I thought I d include it here anyway for completeness.


What I want to do is simple: make a POST request to another server over SSL using fsockopen.
My code so far is this:

$host =  www.redacted.com ;
$data =  user=redacted&pass=redacted&action=redacted ;
$response = "";

if ( $fp = fsockopen("ssl:{$host}", 443, $errno, $errstr, 30) ) {

    $msg  =  POST /wsAPI.php HTTP/1.1  . "
";
    $msg .=  Content-Type: application/x-www-form-urlencoded  . "
";
    $msg .=  Content-Length:   . strlen($data) . "
";
    $msg .=  Host:   . $host . "
";
    $msg .=  Connection: close  . "

";
    $msg .= $data;
    if ( fwrite($fp, $msg) ) {
        while ( !feof($fp) ) {
            $response .= fgets($fp, 1024);
        }
    }
    fclose($fp);

} else {
    $response = false;
}

This works fine of course if I just pass in $host and use port 80. But I really need to send this over SSL, and right now it s not working. $response gets set to false, $errno stays at 0, and $errstr gets set to php_network_getaddresses: getaddrinfo failed: No such host is known.. I know that it s not an issue of the server being down, or a typo in the host name, etc., because it DOES work if I go over port 80 unsecurely. The problems only start when I try to switch to SSL.

What do I do to get this working?

最佳回答

This may sound obvious, but have you tried this instead?

if ($fp = fsockopen( ssl:// . $host, 443, $errno, $errstr, 30)) {

I m not sure if the // is required or not, but the ssl and tls examples on the PHP Internet Transports page have them.

P.S. I also have a "thing" about included variables in strings, in case you re wondering why it uses string concatenation now.

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签