我在 Windows 上运行 PHP 5.2.6,我在 php.ini 中没有注释extension=php_curl.dll
;extension=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 时才开始。
我该怎么做才能让它工作?