我知道有人问过这个问题,但是没有一个解决方案对我有用。
我正在使用来自贝宝(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 - PayPal IPN:HTTP/1.1 400错误请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16373374/

10-09 08:27
查看更多