通过PayPal IPN传递数组

时间:2015-12-13 23:32:59

标签: php html arrays paypal paypal-ipn

我有一个看起来像这样的数组:

array(0 => $website_ref,1 => $user_id,2 => $item1,3 => $item2,4 => $item3,5 => $item4);

我尝试了多次,不同的方式通过这个PayPal按钮代码传递它:

<input type="hidden" name="custom" value="<? array(0 => $website_ref,1 => $user_id,2 => $item1,3 => $item2,4 => $item3,5 => $item4); ?>">

所以在IPN.php上可以这样读:

$custom = $_POST['custom'];

$website_ref = $custom[0];
$user_id = $custom[1];
$item1 = $custom[2];
$item2 = $custom[3];
$item3 = $custom[4];
$item4 = $custom[5];

但是我很确定我做错了,因为代码不起作用。我已经尝试在变量中使用数组并传递它,但另一方面我的第一个结果是&#39; A&#39;可能是为了阵列&#39;。我知道我在这里遗漏了一些东西,但不太确定如何让它发挥作用?

1 个答案:

答案 0 :(得分:1)

$_POST['custom']返回一个值,该值是数组的String版本。就像你echo array(...)一样。 $_POST['custom']始终是一个字符串,这就是您A时获得$custom[0]的原因。

设置自定义元素的值时,您最有可能希望将其格式化,然后在从PayPal收回数据时解析数据。

您可以使用JSON作为格式,或者查看其他选项的this SO solution

使用JSON实现:

<?php
  $arr = array($website_ref, $user_id, $item1, $item2, $item3, $item4);
  $data = json_encode($arr);
?>
<input type="hidden" name="custom" value="<?= $data ?>">

然后在IPN.php中:

$custom = json_decode($_POST['custom'], true);

$website_ref = $custom[0];
$user_id = $custom[1];
$item1 = $custom[2];
$item2 = $custom[3];
$item3 = $custom[4];
$item4 = $custom[5];