我知道有人曾经问过这个问题,但没有一个解决方案适合我。 我正在使用paypal(https://developer.paypal.com/webapps/developer/applications/ipn_simulator)的即时付款通知(IPN)模拟器,但我总是收到: 来自PayPal的意外回复:HTTP / 1.1 400错误请求
<?php
//Open a socket for the acknowledgement request
$fp = fsockopen (ssl://www.sandbox.paypal.com, 443, $errno, $errstr, 30);
if (!$fp) {
// fsockopen error
throw new Exception("An error occured while using fsockopen(): [$errno] $errstr");
}
//Set up the acknowledgement request headers
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: ssl://www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($data)."\r\n";
$header .= "Connection: Close\r\n\r\n";
// Post request back to PayPal for validation
fputs ($fp, $header . $data);
while (!feof($fp)) { // While not EOF
$res = fgets ($fp, 1024); // Get the acknowledgement response
if (strcmp ($res, "VERIFIED") == 0) { // Response is VERIFIED
$response = 'verified';
}
else if (strcmp ($res, "INVALID") == 0) { // Response is INVALID
$response = 'invalid';
}
else {
throw new Exception("Unexpected response from PayPal: $res");
}
}
编辑: 更改后
$header .= "Host: ssl://www.sandbox.paypal.com\r\n";
到
$header .= "Host: www.sandbox.paypal.com\r\n";
我现在收到 HTTP / 1.1 200 OK 响应。 另一个问题是,我的if()部分不起作用,它总是跳到最后一个
else {
throw new Exception("Unexpected response from PayPal: $res");
}
if (strcmp ($res, "VERIFIED") == 0)
是否有问题?
更新2: 这是我的完整代码。也许有人可以找到错误:
class Paypal {
protected $sandbox = false;
protected $data = null;
const SANDBOX_URL = 'www.sandbox.paypal.com';
const PAYPAL_URL = 'www.paypal.com';
public function __construct($sandbox = false) {
$this->sandbox = $sandbox;
}
public function receiveData() {
if (empty($_POST)) {
throw new Exception('No $_POST data found');
}
$this->data = $_POST;
// Read the notification from PayPal and create the acknowledgement response
$req = 'cmd=_notify-validate'; // add 'cmd' to beginning of the acknowledgement you send back to PayPal
foreach ($_POST as $key => $value) { // Loop through the notification NV pairs
$value = urlencode(stripslashes($value)); // Encode the values
$req .= "&$key=$value"; // Add the NV pairs to the acknowledgement
}
if ($this->fsock($req))
return true;
else
return false;
}
public function getData() {
return $this->data;
}
private function fsock($data) {
//Open a socket for the acknowledgement request
$fp = fsockopen ('ssl://'.$this->getURL(), 443, $errno, $errstr, 30);
if (!$fp) {
// fsockopen error
throw new Exception("An error occured while using fsockopen(): [$errno] $errstr");
}
//Set up the acknowledgement request headers
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: ".$this->getURL()."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($data)."\r\n";
$header .= "Connection: Close\r\n\r\n";
// Post request back to PayPal for validation
fputs ($fp, $header . $data);
while (!feof($fp)) { // While not EOF
$res = fgets ($fp, 1024); // Get the acknowledgement response
if (strcmp ($res, "VERIFIED") == 0) { // Response is VERIFIED
$response = 'verified';
// Notification protocol is complete, OK to process notification contents
// Possible processing steps for a payment might include the following:
// Check that the payment_status is Completed
// Check that receiver_email is your Primary PayPal email
// Check that payment_amount/payment_currency are correct
// Process payment
}
else if (strcmp ($res, "INVALID") == 0) { // Response is INVALID
$response = 'invalid';
}
else {
throw new Exception("Unexpected response from PayPal: $res");
}
}
fclose ($fp); //close file pointer
if ($response == 'verified')
return true;
else
return false;
}
private function getURL() {
if ($this->sandbox)
return self::SANDBOX_URL;
else
return self::PAYPAL_URL;
}
}
$paypal = new Paypal(true);
try {
if ($paypal->receiveData()) {
// success
}
else {
// fail
}
}
catch (Exception $e) {
// exception
echo 'Exception: ', $e->getMessage(), "\n";
}
答案 0 :(得分:4)
您的脚本似乎存在许多问题。例如,$ data未初始化,因此您不发布任何内容。 但是你得到400错误的原因是因为 $ header。=“主持人:ssl://www.sandbox.paypal.com \ r \ n”;
应该是
$ header。=“主持人:www.sandbox.paypal.com \ r \ n”;
答案 1 :(得分:2)
我维护了一些当前的IPN程序,在我看来,您阅读数据的方法不太可能从PayPal获得完整的响应。 TCP数据以数据包形式发送,而PayPal响应不作为单个缓冲区发送,因此实际上单个读取实际上不太可能包含尽可能多的字节,直到请求的1,024个字节。
为了在你自己的代码中实现,你必须循环并附加到你读到的变量,直到达到EOF。
此外,您可以在此处查看此PHP示例代码:
https://www.x.com/developers/PayPal/documentation-tools/code-sample/216623
我认为我现在可以看到你的错误了。您的fgets语句一次只读取一行响应,因为它在达到长度或达到换行时停止读取,以先到者为准。您正在寻找的线是Pay Pal的响应体,而不是前几行(标题等)。然而你的else语句包含在循环中。因此,一旦从PayPal收到标题的第一行,就会执行else语句。
我建议两个改变。在循环之后移动失败代码,因为在达到EOF之前实际上并不确定失败。
我建议的另一个改变是使用strpos而不是strcmp。尽管paypal响应主体仅包含已验证的单个单词,但CR或LF或两者都很有可能成为fgets读取的那一行的一部分。所以strcmp会失败。您可以使用条件strpos ($res, "VERIFIED") !== false
代替现有条件(以及检查是否收到INVALID的相同更改。
这是一些可能有效的可能的代码(我无法在您的情况下测试此代码,未经测试的代码永远无法确定是否正常工作)。请将此代码作为建议,而不是作为字面答案:
// Post request back to PayPal for validation
fputs ($fp, $header . $data);
while (!feof($fp)) { // While not EOF
$res = fgets ($fp, 1024); // Get the acknowledgement response
if (strpos ($res, "VERIFIED") !== false) { // Response is VERIFIED
$response = 'verified';
// Notification protocol is complete, OK to process notification contents
// Possible processing steps for a payment might include the following:
// Check that the payment_status is Completed
// Check that receiver_email is your Primary PayPal email
// Check that payment_amount/payment_currency are correct
// Process payment
break;
}
else if (strpos ($res, "INVALID") !== false) { // Response is INVALID
$response = 'invalid';
break;
}
}
fclose ($fp); //close file pointer
if ($response == 'verified')
return true;
else if ($response == 'invalid') {
return false;
}
else { // no else statement is actually needed here,
// since the previous conditions both return
throw new Exception("Unexpected response from PayPal: $res");
return false;
}
答案 2 :(得分:1)
你的If语句是正确的,你需要做的就是打印你正在检查的值 标题/尾随字符的“已验证”: 将这样的内容复制到你的else块中:
var_dump($res) ; die;
所以你可以看到你得到的东西:)
答案 3 :(得分:0)
请清理浏览器缓存或重新启动浏览器。
这肯定会解决您的错误请求问题。
答案 4 :(得分:0)
套接字级HTTP通信使您的代码非常容易出错。我建议您使用像cURL这样的HTTP客户端库,它将为您处理请求/响应周期,遵循潜在的重定向和读/写标题/预告片。