【问题标题】:Passing array through PayPal IPN通过 PayPal IPN 传递数组
【发布时间】:2016-03-19 09:12:09
【问题描述】:

我有一个如下所示的数组:

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”,可能是“数组”。我知道我在这里遗漏了一些东西,但不太确定如何让它工作?

【问题讨论】:

标签: php html arrays paypal paypal-ipn


【解决方案1】:

$_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];

【讨论】:

  • 请记住,自定义参数限制为 256 个字符。如果您遇到问题,您总是可以将必要的数据保存到数据库表中,将记录的 ID 传递给自定义参数,然后使用 IPN 脚本中的 ID 将数据拉回。
猜你喜欢
  • 2012-11-07
  • 2011-02-23
  • 1970-01-01
  • 2015-11-07
  • 2014-02-05
  • 2013-05-23
  • 1970-01-01
  • 2015-07-28
  • 2011-02-19
相关资源
最近更新 更多