为什么tlstest.paypal.com可以通过浏览器工作,而不能通过我的PHP代码工作(对Paypal IPN有用)? [英] Why does tlstest.paypal.com work from browser but not from my PHP code (useful for Paypal IPN)?

查看:75
本文介绍了为什么tlstest.paypal.com可以通过浏览器工作,而不能通过我的PHP代码工作(对Paypal IPN有用)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2018年6月30日之后,贝宝(Paypal)赢得了胜利不再接受非TLS 1.2 + HTTP 1.1请求.
他们创建了URL https://tlstest.paypal.com/来测试连接是否正常.如果在浏览器中打开此URL,则会成功:

After 2018 June 30th, Paypal won't accept non-TLS 1.2 + HTTP 1.1 requests anymore.
They created the URL https://tlstest.paypal.com/ to test if connections are OK. If we open this URL in a browser, we get a successful:

PayPal_Connection_OK

问题:为什么使用以下代码从PHP连接时为什么失败?(我完全没有响应,浏览器仍处于等待状态"

Quesiton: why does it fail when connecting from PHP with the following code? (I get no response at all, the browser is still in waiting "state" like this, so it doesn't even arrive at echo $errno; echo $errstr;)

<?php
$req = '';    // usually I use $req = 'cmd=_notify-validate'; for IPN
$header .= "POST / HTTP/1.1\r\n";
$header .= "Host: tlstest.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen('tls://tlstest.paypal.com', 443, $errno, $errstr, 30);

if (!$fp) {
    echo $errno;
    echo $errstr;
} else {
    fputs($fp, $header);
    while (!feof($fp))
    {
        $res = fgets($fp, 1024);
        echo $res;
    }
    fclose($fp);
}
?>

注意:

我有PHP 5.6.33-0 + deb8u1(cli)(内置:2018年1月5日15:46:26)和openssl version text: OpenSSL 1.0.1t 3 May 2016

I have PHP 5.6.33-0+deb8u1 (cli) (built: Jan 5 2018 15:46:26) and openssl version text: OpenSSL 1.0.1t 3 May 2016

推荐答案

通过将tls://更改为ssl://,对我来说这是完全没有意义的,但这也是为什么使用fsockopen级别太低的库,无法与其进行HTTP交换(您应该使用适当的HTTP客户端库),并且与此同时,关于TLS内容的配置也不足够.

It works on my side by changing tls:// to ssl:// which makes absolutely no sense to me, but this is also why using fsockopen is a too low level library to just do HTTP exchanges with it (you should use a proper HTTP client library) and at the same time not configurable enough regarding TLS stuff.

$fp = fsockopen('tls://tlstest.paypal.com', 443, $errno, $errstr, 30); 我得到了:

HTTP/1.1 426 Unknown
Server: AkamaiGHost
Mime-Version: 1.0
Content-Type: text/html
Content-Length: 267
Expires: Fri, 22 Jun 2018 19:49:46 GMT
Date: Fri, 22 Jun 2018 19:49:46 GMT
Connection: keep-alive
Upgrade: TLS/1.2

<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>

You don't have permission to access "http&#58;&#47;&#47;tlstest&#46;paypal&#46;com&#47;" on this server.<P>
Reference&#32;&#35;18&#46;8024a17&#46;1529696986&#46;1fc51318
</BODY>
</HTML>

但使用$fp = fsockopen('ssl://tlstest.paypal.com', 443, $errno, $errstr, 30); 我得到:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 20
Date: Fri, 22 Jun 2018 20:05:35 GMT
Connection: keep-alive

然后挂起,可能是因为它是一个保持活动的连接,并且缓冲区小于1024,所以您无法获得以下主体内容. 这可能是"PayPal_Connection_OK",因为它与Content-Length中显示的长度完全匹配. 这再次表明,您应该使用HTTP客户端库,而不要尝试(严重)在fsockopen之上重新实现HTTP.

And then it hangs, probably because it is a keep-alive connection and the buffer is smaller than 1024 so that you do not get the following body content. Which is probably "PayPal_Connection_OK", as it exactly matches the length displayed in Content-Length. This again shows that you should use an HTTP client library instead of trying to (badly) reimplement HTTP on top of fsockopen.

这篇关于为什么tlstest.paypal.com可以通过浏览器工作,而不能通过我的PHP代码工作(对Paypal IPN有用)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆