2013-04-20 76 views
0

只是想知道如果有人可以幫我解決我關於貝寶IPN的一個令人沮喪的問題。我的代碼是:貝寶IPN的問題

<?php 
    error_reporting(E_ALL); 
    // tell PHP to log errors to ipn_errors.log in this directory 
    ini_set('log_errors', true); 
    ini_set('error_log', dirname(__FILE__).'/ipn_errors.log'); 

    // intantiate the IPN listener 
    include('ipnlistener.php'); 
    $listener = new IpnListener(); 

    // tell the IPN listener to use the PayPal test sandbox 
    $listener->use_sandbox = false; 

    // try to process the IPN POST 
    try { 
     $listener->requirePostMethod(); 
     $verified = $listener->processIpn(); 
    } catch (Exception $e) { 
     error_log($e->getMessage()); 
     exit(0); 
    } 

    // TODO: Handle IPN Response here 
    // ... 

    if ($verified) { 
     echo 'hello'; 
     // TODO: Implement additional fraud checks and MySQL storage 
     mail('my email here', 'Valid IPN', $listener->getTextReport()); 
    } else { 
     // manually investigate the invalid IPN 
     mail('my email here', 'Invalid IPN', $listener->getTextReport()); 
    } 

>

我ipnlistener.php是這裏給出的一個:https://github.com/Quixotix/PHP-PayPal-IPN

當有人提出付款,我得到了有效的IPN電子郵件,我應該在代碼中,但沒有其他迴應(即你好不回聲),我的網頁的頁腳不在那裏。這就像拒絕迴應任何事情,但會遵循其餘的命令。在我的錯誤日誌中,我總是會收到'無效的HTTP請求方法'。

任何人都可以幫忙嗎?

謝謝。

編輯:只是櫃面它有助於瞭解,任何事情之前

try { 
    $listener->requirePostMethod(); 
    $verified = $listener->processIpn(); 
} catch (Exception $e) { 
    error_log($e->getMessage()); 
    exit(0); 
} 

是好的呼應,但任何經過呼應不起作用。

+1

你得到什麼錯誤 – 2013-04-20 09:36:30

+0

錯誤在哪裏?在頁面上我根本沒有任何錯誤,它會在html中打印我的頭文件,但一旦它到達PHP部分,一切都會停止,除非我仍然收到有效的IPN電子郵件。在error_log中,我收到'無效的HTTP請求方法'。 – tryingmathematician 2013-04-20 09:38:19

+0

請確定'$ header =「POST/cgi-bin/webscr HTTP/1.0 \ r \ n」;'將此添加到您的標題 – 2013-04-20 09:42:55

回答

0

requirePostMethod()有意引發「無效HTTP請求方法」錯誤。這個想法是,這個頁面應該只接受POST請求。當您在瀏覽器中查看該頁面時,這是一個GET請求。

編輯:當requirePostMethod()拋出異常時,這會阻止PHP解析頁面的其餘部分,這就是爲什麼後面的東西不會被回顯出來的原因。

查看關於GitHub上requirePostMethod的評論=>https://github.com/Quixotix/PHP-PayPal-IPN/blob/master/ipnlistener.php(它正好在底部)。

希望有幫助!

+0

感謝您的回覆。看起來你可能能夠幫助我,這很棒。我已經閱讀了評論,並且已經理解爲什麼會發生這種情況,但我不確定如何更改它以便解析頁面的其餘部分? – tryingmathematician 2013-04-20 18:18:38

+0

我設法讓它使用post請求,但是現在我得到'header('Allow:POST',true,405)''行引起的'headers already sent'錯誤。無論如何,我可以解決這個問題嗎? – tryingmathematician 2013-04-20 19:53:06

+0

只需取出$ listener-> requirePostMethod()即可。類方法似乎沒有被其他任何東西使用,只是爲了確保你的腳本只接受POST請求。如果你想允許GET請求,那麼就不需要使用這個函數。但是,我建議,如果您只是調試腳本,並將其放回到活動狀態(這對安全性有好處)。 – 2013-04-20 22:12:32