PHP Paypal IPN侦听器事务数组 [英] PHP paypal ipn listener transaction array

查看:81
本文介绍了PHP Paypal IPN侦听器事务数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        $req = 'cmd=' . urlencode('_notify-validate');
        foreach ($_POST as $key => $value) {
            $value = urlencode(stripslashes($value));
            $req .= "&$key=$value";
        }    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.sandbox.paypal.com/cgi-bin/webscr');
        curl_setopt($ch, CURLOPT_HEADER, 0);
        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_HTTPHEADER, array('Host: www.paypal.com'));
        $res = curl_exec($ch);
        curl_close($ch);

        if (strcmp ($res, "VERIFIED") == 0) {
            //TODO
        }
        else if (strcmp ($res, "INVALID") == 0) {
            //TODO
        }

响应

cmd=_notify-validate&transaction=Array

更多关于响应的信息,但是您可以看到问题是它们的$ _POST数组中的is数组.如何在$ _POST数组中对数组进行编码和反斜杠?

theres more to the response but has you can see the problem is that their is array within the $_POST array. How to encode and stripslashes of array with in array of $_POST?

推荐答案

在PHP中,通过$ _POST变量无法正确处理数组中的数组.

Arrays within arrays are not handled correctly in PHP through the $_POST variable.

您需要直接从输入流中读取原始数据.

You need to read the raw data directly from the input stream.

这里有一些示例代码.

<?php
   $raw_post_data = file_get_contents('php://input');
   $raw_post_array = explode('&', $raw_post_data);
   $myPost = array();
   foreach ($raw_post_array as $keyval)
      {
      $keyval = explode ('=', $keyval);
      if (count($keyval) == 2)
         $myPost[$keyval[0]] = urldecode($keyval[1]);
      }
      $_req = 'cmd=_notify-validate';
      foreach ($myPost 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 .= "Host: www.sandbox.paypal.com:443\r\n";
$header .= "Content-type: text/html; charset=utf-8\r\n";
$header .= "Content-Length: " . strlen($_req) . "\r\n\r\n";

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


if (!$fp){
//Something failed
} else {
fputs ($fp, $header . $_req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {

//Success

}
else if (strcmp ($res, "INVALID") == 0) {
//Failure to validate
}
}
fclose ($fp);
}
?>

这篇关于PHP Paypal IPN侦听器事务数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆