PayPal ipn 不起作用的代码停止


Paypal Ipn Not Working Code Stops

我的PayPal ipn 代码在一段时间后停止工作,或者似乎没有从 while 收集正确的信息或未设置信息。有没有人对这行代码的正确输入有任何想法。这是代码:

 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) {

有谁知道为什么它没有通过PayPal ipn代码的这一部分?

我找到了这个脚本:https://github.com/paypal/ipn-code-samples/blob/master/paypal_ipn.php

我遇到了同样的问题,没有得到回应。 决定改用cURL进行研究。 这个脚本完美运行。 我确实需要安装 cURL 并下载脚本注释中引用的证书。

你应该使用 curl 而不是 ssl socket。您需要将请求(在我的示例中为 $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);

你会得到PayPal结果$res变量。

更改此行:

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

对此:

$value = urlencode($value);

你只需要在那里使用stripslashes,如果你运行PHP并打开魔术报价。从 PHP 5.4 开始,这(谢天谢地)不再可能。

PayPal允许用户数据包含反斜杠(我在 address_street 变量的 IPN 数据中看到过)。如果从包含反斜杠的 IPN 数据中删除反斜杠,PayPal将返回无效响应。