【问题标题】:PayPal IPN PHP Handshake ErrorPayPal IPN PHP 握手错误
【发布时间】:2017-01-02 12:57:29
【问题描述】:

好的,所以我最近开始尝试使用 PayPals IPN,我已经阅读了他们 IPN 上的一些 PayPals 页面,并从这里使用了 PHP 源:https://developer.paypal.com/docs/classic/ipn/gs_IPN/,我已经完成了整个代码:

<?php

header('HTTP/1.1 200 OK');

$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}

$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";

$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

$header .= "Host: www.sandbox.paypal.com:443\r\n";

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

fputs($fp, $header . $req);

while (!feof($fp))
{
    $res = fgets($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0) {  

    } else if (strcmp ($res, "INVALID") == 0) { 

    }

    fclose ($fp);
}
?>

当我使用 PayPal 的 IPN 模拟器时,它说

IPN 未发送,握手未验证。请检查您的信息。

这很奇怪,因为我似乎正确阅读了 PayPal 的 PHP 文档,而且这段代码似乎应该可以工作?可能出了什么问题?

【问题讨论】:

标签: php paypal paypal-ipn paypal-sandbox


【解决方案1】:

尝试登录后端。有时 PayPal 的 IPN 监听器会说握手无效,即使它是无效的。

【讨论】:

  • 感谢您告诉我 :)
【解决方案2】:

也许尝试使用 cURL 运行相同的东西?

$params = clone $_POST;
$params['cmd'] = '_notify-validate';

$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt_array($ch, array(
    CULROPT_RETURNTRANSFER=>true,
    CURLOPT_POST=>true,
    CURLOPT_POSTFIELDS=>$params
));

$result = curl_exec($ch);
$status = 'unknown';
if($result === false) {
    $status = 'error';
} else {
    if(strcmp($result, 'VERIFIED') == 0) {
         $status = 'verified';
    } elseif (strcmp($result, 'INVALID') == 0) {
         $status = 'invalid';
    }
}

echo $status;

【讨论】:

    【解决方案3】:

    问题是你的请求返回400 Bad Request

    这是因为该请求不包含Host 标头(请求被它遇到的第一个\r\n 序列终止,并且Host 标头仅在 this 之后传递),这是 HTTP/1.1 要求的,因此导致请求失败。

    现在,将Host 标头放在首位:

    $header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
    
    $header .= "Host: www.sandbox.paypal.com:443\r\n";
    
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
    

    它应该可以工作。 (至少我在当地得到了正确的200 OK 回复)。可能还有其他问题,但这些都是无关的。

    [顺便说一句。是的,那么贝宝文档上的代码似乎是错误的。]

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 2016-05-27
      • 2017-02-17
      • 2016-05-04
      • 2016-07-05
      • 2016-05-04
      • 2016-04-29
      • 2016-05-02
      • 2021-10-10
      相关资源
      最近更新 更多