【问题标题】:PayPal IPN Bad Request 400 ErrorPayPal IPN 错误请求 400 错误
【发布时间】:2012-08-02 08:46:02
【问题描述】:

使用 PayPal IPN,我不断收到错误 400。

我一直在让脚本向我发送$res 的电子邮件,以查看在while (!feof($fp)) {} 循环内的响应是什么。我总是最终得到错误:HTTP/1.0 400 Bad Request

我总共回来了:

HTTP/1.0 400 Bad Request
​Connection: close
Server: BigIP
Content-Length: 19
​Invalid Host Header

​最后一行是空白。这是我的代码,我尝试过改变很多东西,但没有任何效果。

$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}', $value);// IPN fix
$req .= "&$key=$value";
}

// post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

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

if (!$fp) {
// HTTP ERROR
} else {
   fputs($fp, $header . $req);
   while (!feof($fp)) {
       $res = fgets ($fp, 1024);
       if (strcmp ($res, "VERIFIED") == 0) {
           //ADD TO DB
       } else if (strcmp ($res, "INVALID") == 0) {
           // PAYMENT INVALID & INVESTIGATE MANUALY!
           // E-mail admin or alert user
       }
   }
   fclose ($fp);
}

我加了一行,这是发送前的表头:

 Host: www.sandbox.paypal.com
 POST /cgi-bin/webscr HTTP/1.0
 Content-Type: application/x-www-form-urlencoded
 Content-Length: 1096

【问题讨论】:

    标签: php paypal paypal-ipn


    【解决方案1】:

    由于您自己打开套接字,而不是使用诸如 curl 之类的 HTTP 库,因此您需要设置正确的 HTTP 协议版本并自己在 POST 行下方添加HTTP Host header

    $header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
    $header .= "Host: www.sandbox.paypal.com\r\n";
    

    【讨论】:

    • 您还应该将“HTTP/1.0”更改为“HTTP/1.1”。
    • 其实是这样。 'Host' 标头不是 HTTP/1.0 协议的一部分,因此正确答案是;您需要添加“主机”标头移至 HTTP/1.1。
    • 我不明白为什么需要这个 Host 标头。两年前,我设置了一个 IPN 列表器 - 今天当我需要更新它并在 localhost 上对其进行测试时,我发现在 POST 回 HTTP 时出现错误(现在它给出了 302)。切换到 SSL 后,就像在这个例子中一样,我也得到了 400。并且添加主机头工作 - 但我的实时 IPN 已经使​​用了没有主机头的 SSL。 :s 那么为什么 localhost IPS 需要它呢?
    • 另外,当我尝试 HTTP/1.1 时,PayPal IPN 测试工具会返回一个错误 - 它似乎不喜欢它。切换回 1.0 后一切正常。
    • 我的解决方法:从 HTTP/1.0 更改为 HTTP/1.1
    【解决方案2】:

    我遇到了同样的问题,这些是必需的更改。上面的一些答案并不能解决所有问题。

    标题的新格式:

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

    请注意仅在最后一行额外的 \r\n 集。 此外,字符串比较不再起作用,因为在服务器的响应中插入了换行符,因此请更改:

    if (strcmp ($res, "VERIFIED") == 0) 
    

    到这里:

    if (stripos($res, "VERIFIED") !== false)  // do the same for the check for INVALID
    

    【讨论】:

    【解决方案3】:

    https://www.x.com/content/bulletin-ipn-and-pdt-scripts-and-http-1-1

    // post back to PayPal system to validate
    $header .="POST /cgi-bin/webscr HTTP/1.1\r\n";
    $header .="Content-Type: application/x-www-form-urlencoded\r\n";
    $header .="Host: www.paypal.com\r\n";
    $header .="Connection: close\r\n";
    

    【讨论】:

      【解决方案4】:

      我发现使用 fsockopen 的 PayPal 示例代码无法正常工作。

      为了使 IPN 与 PHP 一起工作,我使用了 Aireff 从 8 月 5 日开始提出的建议,并在 x.com 网站上使用 curl 技术查看了代码。

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题,最好的办法是使用 paypal 示例代码...然后完美运行:https://www.x.com/developers/PayPal/documentation-tools/code-sample/216623

        【讨论】:

        • 自两年前我第一次设置 IPN 以来,示例代码发生了变化。我认为旧版本是为 PHP4 制作的 - 它现在在沙盒模式下失败,仍然可以正常工作。虽然这让我很紧张,但我会将代码迁移到更新的示例中。
        【解决方案6】:

        另一种解决方案是在比较之前修剪 $res..

        $res = fgets ($fp, 1024);
        
        $res = trim($res); //NEW & IMPORTANT
        

        【讨论】:

          猜你喜欢
          • 2021-10-10
          • 2017-12-29
          • 2018-04-27
          • 2014-10-28
          • 2013-04-28
          • 2017-05-31
          • 2013-02-24
          • 2015-08-12
          • 2015-07-17
          相关资源
          最近更新 更多