【发布时间】:2013-04-16 03:41:32
【问题描述】:
我正在尝试使用 PayPal IPN 系统。我已经设置了我的代码以使用他们的 SandBox 平台进行测试。我正在寻找为什么我的代码的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.0\r\n";
$header .= "Host: sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$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');
}
}
?>
【问题讨论】:
-
可能无法写入文件?尝试添加 error_reporting(E_ALL);和 ini_set('display_errors',1) 就在您正在执行的代码之前(如果您的代码中其他任何地方都没有它)。
-
嗯,这实际上帮助了我很多。条件现在正在运行,只是不是我需要运行的.... "if (strcmp ($res, "VERIFIED") == 0) {"
-
也许他来自贝宝的结果包含一些额外的空格?比较失败的最明显原因就是字符串不一样。
-
没有。这很奇怪。当我通过 SandBox 系统运行 PayPal IPN 测试时,它运行良好。当我从沙盒切换到只是 paypal.com 时,它不再工作了?
-
如果我尝试这个 fgets 也会返回一大堆标题。我想那些是给你带来麻烦的。另外,不确定是否只是此处粘贴的代码,但是您在此代码中有双
else语句,这让我的 PHP 搞砸了。
标签: php paypal paypal-sandbox paypal-ipn