我有以下代码段,用于捕获来自Paypal的IPN响应,但是当我将res输出到文件时,文件为空。我做了一些故障排除,发现当我var_dump $res
变量时,它是一个空字符串。套接字打开没有问题,fput的var_dump显示写入139位,但响应字符串本身仍为空,既不包含有效,也无效。
思想?
// Build the required acknowledgement message out of the notification just received
$req = 'cmd=_notify-validate'; // Add 'cmd=_notify-validate' to beginning of the acknowledgement
foreach($_POST as $key => $value) { // Loop through the notification NV pairs
$value = urlencode(stripslashes($value)); // Encode these values
$req.= "&$key=$value"; // Add the NV pairs to the acknowledgement
}
// Set up the acknowledgement request headers
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n"; // HTTP POST request
$header.= "Content-Type: application/x-www-form-urlencoded\r\n";
$header.= "Content-Length: " . strlen($req) . "\r\n\r\n";
$header.= "Connection: Close";
// Open a socket for the acknowledgement request
$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// Send the HTTP POST request back to PayPal for validation
var_dump(fputs($fp, $header . $req));
$res = stream_get_contents($fp, 1024);
答案 0 :(得分:3)
您在回复中指定了回调,但是您错过了“主机”标题 HTTP 1.1需要“主机”。
试试这个:
// Build the required acknowledgement message out of the notification just received
$req = 'cmd=_notify-validate'; // Add 'cmd=_notify-validate' to beginning of the acknowledgement
foreach($_POST as $key => $value) { // Loop through the notification NV pairs
$value = urlencode(stripslashes($value)); // Encode these values
$req.= "&$key=$value"; // Add the NV pairs to the acknowledgement
}
// Set up the acknowledgement request headers
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n"; // HTTP POST request
$header.= "Content-Type: application/x-www-form-urlencoded\r\n";
$header.= "Host: www.sandbox.paypal.com\r\n";
$header.= "Content-Length: " . strlen($req) . "\r\n";
$header.= "Connection: Close\r\n\r\n";
// Open a socket for the acknowledgement request
$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// Send the HTTP POST request back to PayPal for validation
var_dump(fputs($fp, $header . $req));
$res = stream_get_contents($fp, 1024);
虽然我强烈建议你移植到cURL而不是fsockopen(就像我们的例子)
虽然fsockopen有其用途,但我们发现它通常会导致更多问题,因为它在必须发送的内容以及必须具体读取响应的方式中非常具体。
PHP下的cURL更宽松,处理了很多后台工作。
答案 1 :(得分:0)
似乎paypal已经改变了他们的IPN api而没有更新他们的文档。我发现以下内容非常有帮助。第一次工作。
https://github.com/Quixotix/PHP-PayPal-IPN
你必须将use_ssl设置为true,因为paypal现在需要ssl才能工作。不过不用担心,它已经附带了共享证书,或者您可以下载curl库中记录的cacert.pem
http://curl.haxx.se/docs/caextract.html
设置完毕后,它只在我的ipn监听器中使用以下代码进行滚动:
<?php
header('HTTP/1.1 200 OK');
require_once('includes/ipnlistener.php');
$ipn = new IpnListener();
$ipn->use_ssl = true;
$ipn->use_sandbox = true;
if($ipn->processIpn() === true) {
file_put_contents('ipn.log',$ipn->getTextReport());
} else {
file_put_contents('ipn.log',"Not Verified\n");
}
希望这对你也有帮助