php 如何让 SSL 在 fsockopen 中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1757957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get SSL working in fsockopen?
提问by soapergem
I'm running PHP 5.2.6 on Windows, I have extension=php_curl.dlland extension=php_openssl.dlluncommented in php.ini; as such I can see the following in phpinfo:
我在Windows上运行PHP 5.2.6,我extension=php_curl.dll和extension=php_openssl.dll在php.ini中取消注释; 因此,我可以看到以下内容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.
我不确定启用 cURL 对此至关重要,但由于它提到了 OpenSSL,我想无论如何我都会将它包含在这里以保持完整性。
What I want to do is simple: make a POST request to another server over SSL using fsockopen.
My code so far is this:
我想要做的很简单:使用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;
}
This works fine of course if I just pass in $hostand use port 80. But I really need to send this over SSL, and right now it's not working. $responsegets set to false, $errnostays at 0, and $errstrgets 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.
如果我只是传入$host并使用端口 80,这当然很好用。但我真的需要通过 SSL 发送它,现在它不起作用。$response设置为false,$errno停留在0,然后$errstr设置为php_network_getaddresses: getaddrinfo failed: No such host is known.。我知道这不是服务器停机或主机名输入错误等问题,因为如果我不安全地通过端口 80,它确实可以工作。只有当我尝试切换到 SSL 时才会出现问题。
What do I do to get this working?
我该怎么做才能让它发挥作用?
回答by Powerlord
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 ssland tlsexamples on the PHP Internet Transports pagehave them.
我不确定是否//需要,但PHP Internet Transports 页面上的ssl和tls示例中有它们。
P.S. I also have a "thing" about included variables in strings, in case you're wondering why it uses string concatenation now.
PS 我还有一个关于字符串中包含变量的“事情”,以防您想知道为什么它现在使用字符串连接。

