接收错误“;无效的主机头“;来自Paypal IPN


Receiving error "Invalid Host header" from Paypal IPN

我在验证IPN数据时收到来自Paypal的错误"主机头无效"。我的代码与这里的Paypal示例代码几乎相同。

<?php
eventlog('Starting IPN');
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_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 .= "Content-Type: application/x-www-form-urlencoded'r'n";
$header .= "Content-Length: " . strlen($req) . "'r'n'r'n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
if(!$fp) $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
if (!$fp) {
    // HTTP ERROR
    eventlog('HTTP error sending return request from IPN:' . $errstr);
} else {
    eventlog('Sending:' . "'n" . $header . $req);
    fputs ($fp, $header . $req);
    while (!feof($fp))
    {
        $res = fgets ($fp, 1024);
        eventlog('Paypal response: ' . $res);
        if (strcmp ($res, "VERIFIED") == 0) {
            // PAYMENT VALIDATED & VERIFIED!
            if($_POST['payment_status'] === 'completed')
            {
                $custom = unserialize($_POST['custom']);
                $email = $custom[1];
                while(isset($_POST['item_name' . $i]))
                {
                    recordPayment($email, $_POST['item_name' . $i], $_POST['quantity' . $i], $_POST['mc_gross' . $i], 'Paypal');
                    $i++;
                }
            }else
            {
                eventlog('Payment from ' . $email . ' not yet completed.');
            }
        } else if (strcmp ($res, "INVALID") == 0) {
            // PAYMENT INVALID & INVESTIGATE MANUALY!
            eventlog('Invalid payment attempt: ' . $req);
        }
    }
    fclose ($fp);
}
?>

以下是我在日志文件中得到的内容:

Thursday 4th of July 2013 01:20:38 PM: Starting IPN
Thursday 4th of July 2013 01:20:38 PM: Sending:
POST /cgi-bin/webscr HTTP/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 894
cmd=_notify-validate&mc_handling1=1.67&address_state=CA&txn_id=1022805571&last_name=Smith&mc_currency=USD&payer_status=verified&address_status=confirmed&tax=2.02&invoice=abc1234&address_street=123%2C+any+street&payer_email=buyer%40paypalsandbox.com&mc_gross1=12.34&mc_shipping=3.02&first_name=John&business=seller%40paypalsandbox.com&verify_sign=A--8MSCLabuvN8L.-MHjxC9uypBtAgGbVpm-R.25XDqUbmKjsNIBMI1A&payer_id=TESTBUYERID01&payment_date=12%3A35%3A05+4+Jul+2013+PDT&address_country=United+States&payment_status=Completed&receiver_email=seller%40paypalsandbox.com&payment_type=instant&address_zip=95131&address_city=San+Jose&mc_shipping1=1.02&item_name1=something&mc_gross=15.34&item_number1=AK-1234&mc_fee=0.44&residence_country=US&address_country_code=US&notify_version=2.4&receiver_id=seller%40paypalsandbox.com&mc_handling=2.06&txn_type=cart&custom=xyz123&address_name=John+Smith&test_ipn=1
Thursday 4th of July 2013 01:20:38 PM: Paypal response: HTTP/1.0 400 Bad Request
Thursday 4th of July 2013 01:20:38 PM: Paypal response: Server: BigIP
Thursday 4th of July 2013 01:20:38 PM: Paypal response: Connection: close
Thursday 4th of July 2013 01:20:38 PM: Paypal response: Content-Length: 19
Thursday 4th of July 2013 01:20:38 PM: Paypal response: 
Thursday 4th of July 2013 01:20:38 PM: Paypal response: Invalid Host header

与示例代码相比,我已经非常彻底地研究了它,它几乎完全相同。有人看到我做错了什么吗?

缺少此标头

$header .= "Host: www.sandbox.paypal.com'r'n";
// post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.0'r'n";
$header .= "Content-Type: application/x-www-form-urlencoded'r'n";
//$header .= "Content-Length: " . strlen($req) . "'r'n'r'n"; change with the line below
$header .= "Content-Length: " . strlen($req) . "'r'n";
$header .= "Host: www.sandbox.paypal.com'r'n"; //and add this line

它对我有效;)