2013-12-18 137 views
1

我的paypal ipn代码停止工作后,或似乎没有收集正确的信息,或者信息没有设置。有没有人有任何想法,应该为这行代码输入正确的内容。代码如下:贝宝Ipn不工作代码停止

while (!feof($fp)) { 
$res = fgets ($fp, 1024); 

以上代码是否正常运行,代码如下。下面是代码:

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); 

这里的大部分代码:

// Response from Paypal 

// read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-validate'; 
foreach ($_POST as $key => $value) { 
    $value = urlencode(stripslashes($value)); 
    $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix 
    $req .= "&$key=$value"; 
} 

// assign posted variables to local variables 
$data['item_name']   = $_POST['item_name']; 
$data['item_number']  = $_POST['item_number']; 
$data['payment_status']  = $_POST['payment_status']; 
$data['payment_amount']  = $_POST['mc_gross']; 
$data['payment_currency'] = $_POST['mc_currency']; 
$data['txn_id']    = $_POST['txn_id']; 
$data['receiver_email']  = $_POST['receiver_email']; 
$data['payer_email']  = $_POST['payer_email']; 
$data['custom']    = $_POST['custom']; 

// 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) { 
    // HTTP ERROR 
} else {  

    fputs ($fp, $header . $req); 
    while (!feof($fp)) { 
     $res = fgets ($fp, 1024); 
     if (strcmp($res, "VERIFIED") == 0) { 

有谁知道为什么它不通过贝宝IPN代码的这一部分?

+0

'ssl'应该是'https'吗?此外,这不起作用?它停在哪里? – halfer

+0

$ res = fgets($ fp,1024); – user3109009

回答

0

您应该使用curl而不是ssl套接字。你需要建立你的请求(在我的例子中,$ req),作为对值字符串,即:$req="key1=value1&key2=value2";

$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr'); 
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close')); 

    if(!($res = curl_exec($ch))) { 
     curl_close($ch); 
     exit; 
    } 
    curl_close($ch); 

而且您将在$res var。中获得Paypal结果。

0

改变这一行:

$value = urlencode(stripslashes($value)); 

要这样:

$value = urlencode($value); 

你只需要使用stripslashes那里,如果你捉迷藏PHP与魔术引号上。从PHP 5.4开始,这是(幸好)不再可能。

贝宝允许用户数据包含反斜杠(我看到它在address_street变量中的IPN数据中)。如果你从IPN数据中去掉反斜杠,贝宝将返回一个INVALID响应。