我有一个如下所示的数组:
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/