我有一个贝宝表单,该表单提交一个自定义隐藏输入的值。由于我还有一些额外的数据要解析,因此我将它们添加为查询字符串。IE
<input type="hidden" name="custom" value="payment-type=deposit&user-id=<?php echo $user_id; ?>&user-name=<?php echo $user_name; ?>">
输出为:
<input type="hidden" name="custom" value="payment-type=deposit&user-id=1&user-name=admin">
然后,我有一个名为paypal-ipn.php的文件,该文件与paypal进行通信并将付款数据添加到我的数据库中。我使用$ _POST方法获得每个输入的值,即
$item_name = $_POST['item_name']; // wedding name
$booking_id = $_POST['item_number']; // booking id
$payment_status = $_POST['payment_status']; // payment status
$payment_amount = $_POST['mc_gross']; // amount?
$payment_currency = $_POST['mc_currency']; // currency
$txn_id = $_POST['txn_id']; // transaction id
$receiver_email = $_POST['receiver_email']; // reciever email i.e. your email
$payer_email = $_POST['payer_email']; // payer email i.e. client email
$custom_variables = $_POST['custom'];
最后一个(希望)返回我的查询字符串。我的问题是,如何将查询字符串分成单独的变量,即
$payment_type = PAYMENT TYPE FROM STRING
$user_id = USER ID FROM STRING
$user_name = USER NAME FROM STRING
有没有办法使用php来做到这一点?
这是我根据以下答案尝试的最新代码:
$custom_variables = $_POST['custom'];
parse_str($custom_variables);
echo $payment_type;
echo $user_id;
echo $user_name;
parse_str($str, $output);
$payment_type = $output['payment_type'];
$user_id = $output['user_id'];
$user_name = $output['user_name'];
使用解析字符串
将str解析为好像是通过URL传递的查询字符串,并在当前作用域中设置变量。
<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
?>
http://php.net/manual/zh/function.parse-str.php
更新
parse_str($custom_variables , $output);
$payment_type = $output['payment_type'];
$user_id = $output['user_id'];
$user_name = $output['user_name'];
echo $payment_type;
echo $user_id;
echo $user_name;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句