php - 通过 PayPal IPN 传递数组

标签 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];

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

最佳答案

$_POST['custom'] 返回的值是数组的字符串版本。也就是说,就像您执行 echo array(...) 一样。 $_POST['custom'] 将始终是一个字符串,这就是为什么在执行 $custom[0] 时会得到 A

在设置自定义元素的值时,您很可能希望以某种方式对其进行格式化,以便在您从 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];

关于php - 通过 PayPal IPN 传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34257710/

相关文章:

javascript - 如何使用带有复选框和文本输入的 "other"选项

javascript - PHP:在有限的时间后自动点击提交按钮

html - 像素和百分比 - 调整浏览器大小时的问题

python - 从多维 Numpy 数组中减去平均值

java - java中随机数组与随机数组相加,使其等于另一个数组

php - 在本地开发机器上模拟临时无响应服务器或网络拥塞(Apache/PHP)

php - 使用php从mysql数据库获取外键数据到选择下拉列表中

html - 垂直对齐固定大小的 Div

javascript - HTML5 Canvas 粒子爆炸

python - 对数组的每个元素求和