为什么 tlstest.paypal.com 可以在浏览器中运行,但不能在我的 PHP 代码中运行(对 Paypal I

2023-06-23php开发问题
3

本文介绍了为什么 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 连接时会失败?(我根本没有得到任何响应,浏览器仍处于等待状态"像这样,所以它甚至没有到达 echo $errno; echo $errstr;)

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
";
$header .= "Host: tlstest.paypal.com
";
$header .= "Content-Type: application/x-www-form-urlencoded
";
$header .= "Content-Length: " . strlen($req) . "

";
$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);
}
?>

注意:

  • 这不是 Paypal IPN HTTP/1.1 的副本 - 根本不提供任何响应,它是一个空字符串,后者已经过时.

我有 PHP 5.6.33-0+deb8u1 (cli)(构建时间:2018 年 1 月 5 日 15:46:26)和 openssl 版本文本:OpenSSL 1.0.1t 2016 年 5 月 3 日

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

然后就挂了,可能是因为是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 有用)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17