gpt4 book ai didi

Paypal IPN : HTTP/1. 1 400 错误请求

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:28 25 4
gpt4 key购买 nike

我知道有人问过这个问题,但没有一个解决方案对我有用。我正在使用来自 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";
}

最佳答案

您的脚本似乎有很多问题。例如,$data 未初始化,因此您什么也不发布。但是你得到 400 错误的原因是因为 $header .= "主机:ssl://www.sandbox.paypal.com\r\n";

应该是

$header .= "主机:www.sandbox.paypal.com\r\n";

关于 Paypal IPN : HTTP/1. 1 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16373374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com