使用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.0rn";
$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Content-Length: " . strlen($req) . "rnrn";
$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
由于您自己打开套接字,而不是使用 curl 等 HTTP 库,因此您需要设置正确的 HTTP 协议版本,并在 POST 行下方自行添加 HTTP Host 标头。
$header = "POST /cgi-bin/webscr HTTP/1.1rn";
$header .= "Host: www.sandbox.paypal.comrn";
我遇到了同样的问题,这些是必需的更改。 上面的一些答案并不能解决所有问题。
标头的新格式:
$header = "POST /cgi-bin/webscr HTTP/1.1rn";
$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Host: www.sandbox.paypal.comrn"; // www.paypal.com for a live site
$header .= "Content-Length: " . strlen($req) . "rn";
$header .= "Connection: closernrn";
请注意最后一行上的额外 \r 集。此外,字符串 compare 不再有效,因为在服务器的响应中插入了换行符,因此请更改以下内容:
if (strcmp ($res, "VERIFIED") == 0)
对此:
if (stripos($res, "VERIFIED") !== false) // do the same for the check for INVALID
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.1rn";
$header .="Content-Type: application/x-www-form-urlencodedrn";
$header .="Host: www.paypal.comrn";
$header .="Connection: closern";
我发现使用fsockopen的PayPals示例代码无法正常工作。
为了使IPN与PHP一起工作,我使用了Aireff从8月5日开始的建议,并在 x.com 网站上使用curl技术查看了代码。
我遇到了同样的问题,最好的办法是使用PayPal示例代码...然后它完美地工作:https://www.x.com/developers/PayPal/documentation-tools/code-sample/216623
另一种解决方案是在比较之前修剪$res。
$res = fgets ($fp, 1024);
$res = trim($res); //NEW & IMPORTANT