我正在尝试与PayPal IPN系统合作。我已经设置了我的代码,使用他们的沙盒平台进行测试。我正在寻找一个原因,为什么我的代码的if (strcmp ($res, "VERIFIED") == 0) {
部分不执行。不仅如此,这段代码的任何部分都没有向"log.txt"写入任何输出。
如果您能检查一下我的代码,让我知道哪里出了问题,我将不胜感激。
<?php
require_once('includes/mysql.inc.php');
if($_POST) {
$req = 'cmd=' . urlencode('_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.0rn";
$header .= "Host: sandbox.paypal.comrn";
$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Content-Length: " . strlen($req) . "rnrn";
$fp = fsockopen ('ssl://sandbox.paypal.com', 443, $errno, $errstr, 30);
if (!$fp) {
file_put_contents('log.txt', 'httperrrrr');
die();
} else {
file_put_contents('log.txt', 'die here');
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
$data = print_r($res, TRUE);
if (strcmp ($res, "VERIFIED") == 0) {
file_put_contents('log.txt', 'verified');
$username = $_POST['custom'];
require_once('classes/admin.class.php');
$activate = new Admin($db);
$activate->modifyUser($username, 'activate');
} else {
file_put_contents('log.txt', 'noverifyshit');
}
}
} else {
file_put_contents('log.txt', 'no post');
}
}
?>
我修好了!我只是添加了"if ($_POST['payment_status'] = "Completed"){",代码现在执行为true。
这是我更新的代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
require_once('includes/mysql.inc.php');
if($_POST) {
$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.0rn";
$header .= "Host: www.paypal.comrn";
$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Content-Length: " . strlen($req) . "rnrn";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if (!$fp) {
file_put_contents('log.txt', 'httperrrrr');
die();
} else {
file_put_contents('log.txt', 'die here');
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
if ($_POST['payment_status'] = "Completed") {
file_put_contents('log.txt', 'verified');
$username = $_POST['custom'];
require_once('classes/admin.class.php');
$activate = new Admin($db);
$activate->modifyUser($username, 'activate');
}
} else {
file_put_contents('log.txt', $_POST['payment_status']);
}
}
}
}
?>