2013-07-04 26 views
1

我在驗證IPN數據時收到來自Paypal的錯誤「Invalid Host Header」。我的代碼幾乎與Paypal示例代碼here相同。從Paypal IPN接收錯誤「無效主機頭」

<?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 

我已經經歷了很徹底的比較示例代碼,這幾乎是相同的。任何人看到我做錯了什麼?

+0

你做的做的HTTPS請求,而不是僅僅使用curl來爲你做的所有工作的任何原因? –

回答

4

缺少這個頭

$header .= "Host: www.sandbox.paypal.com\r\n"; 
+0

非常感謝,修好了! :) –

0
// 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 

它的工作對我來說)