通过 PayPal IPN 传递数组

Passing array through 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];

但我相当确定我做错了什么,因为代码无法正常工作。我试过在变量中使用数组并将其传递,但​​另一方面我的第一个结果是 'A' 可能是 'Array'。我知道我在这里遗漏了一些东西,但不太确定如何让它工作?

$_POST['custom'] 返回的值是数组的字符串版本。即和你做的一样 echo array(...)$_POST['custom'] 将始终是一个字符串,这就是为什么当您执行 $custom[0].

时会得到 A

设置自定义元素的值时,您很可能希望以某种方式对其进行格式化,以便在您从 PayPal 收到数据时解析数据。

您可以使用 JSON as the format, or have a look at 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];