我整天都在沙盒模式下尝试使用 PayPal IPN 侦听器,试图调整示例代码以供我自己使用。我对得到的结果感到困惑——如果为发送到侦听器脚本的每个 IPN 插入 6 行数据,则 MySQL 查询将在底部运行。我已经更改了查询并将典型的插入复制到数据库中以说明我的观点 - 请参见代码下方的表格。有任何想法吗?我确定我记得以前使用其他一些 API 发生过这种情况,但可以肯定这将是导致它的根本原因。一旦我可以验证表单是否正常工作,我当然会正确地转义数据。
<?php
$dbc = mysqli_connect ('host', 'user', 'pass', 'db');
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$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 ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if ((stristr ($res, "VERIFIED") == 0) &&
($_POST['payment_status'] == 'Completed'))
{
// PAYMENT VALIDATED & VERIFIED!
$email = $_POST['payer_email'];
$next = mysqli_query($dbc, "INSERT INTO users (email, password) VALUES('$email', NOW() ) ");
$to = $email;
$subject = 'Download Area | Login credentials';
$message = '
Thank you for your purchase
Your account information
-------------------------
Email: '.$email.'
Password:
-------------------------
You can now login at http://yourwebsite.com/PayPal/';
$headers = 'From:noreply@downloadarea.com' . "\r\n";
mail($to, $subject, $message, $headers);
}
我从一个IPN得到的数据如下:
+----+-------------------+---------------------+
| id | email | password |
+----+-------------------+---------------------+
| 25 | testing@gmail.com | 2013-07-03 17:15:58 |
+----+-------------------+---------------------+
| 26 | testing@gmail.com | 2013-07-03 17:15:58 |
+----+-------------------+---------------------+
| 27 | testing@gmail.com | 2013-07-03 17:15:58 |
+----+-------------------+---------------------+
| 28 | testing@gmail.com | 2013-07-03 17:15:58 |
+----+-------------------+---------------------+
| 29 | testing@gmail.com | 2013-07-03 17:15:59 |
+----+-------------------+---------------------+
| 30 | testing@gmail.com | 2013-07-03 17:15:59 |
+----+-------------------+---------------------+
为什么要多次插入此 IPN 的数据?