使用 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