21

我在 Windows 上运行 PHP 5.2.6,我在 php.ini 中没有注释extension=php_curl.dllextension=php_openssl.dll因此,我可以看到以下内容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

我不确定启用 cURL 是否对此至关重要,但由于它提到了 OpenSSL,我想我还是将它包含在此处以保持完整性。


我想做的很简单:使用fsockopen.
到目前为止,我的代码是这样的:

$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' . "\r\n";
    $msg .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
    $msg .= 'Content-Length: ' . strlen($data) . "\r\n";
    $msg .= 'Host: ' . $host . "\r\n";
    $msg .= 'Connection: close' . "\r\n\r\n";
    $msg .= $data;
    if ( fwrite($fp, $msg) ) {
        while ( !feof($fp) ) {
            $response .= fgets($fp, 1024);
        }
    }
    fclose($fp);

} else {
    $response = false;
}

如果我只是传入$host并使用端口 80,这当然可以正常工作。但我真的需要通过 SSL 发送它,现在它不起作用。$response设置为false$errno停留在0,并$errstr设置为php_network_getaddresses: getaddrinfo failed: No such host is known.。我知道这不是服务器关闭或主机名拼写错误等问题,因为如果我不安全地通过端口 80,它确实有效。问题仅在我尝试切换到 SSL 时才开始。

我该怎么做才能让它工作?

4

1 回答 1

59

这听起来很明显,但是您是否尝试过呢?

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

我不确定是否//需要,但PHP Internet Transports 页面ssl上的和tls示例有它们。

PS我还有一个关于字符串中包含变量的“事情”,以防你想知道为什么它现在使用字符串连接。

于 2009-11-23T16:26:13.097 回答