更新1:
我的代码中还有以下
foreach
循环:
foreach ($_POST as $key => $value){
$message .= $key . " = " .$value ."\n\n<br />";
}
这是最后一个。
或者问题可能出在这行:
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
不喜欢生成的字符串中的
%5B0%5D
,即
transaction%5B0%5D=USD+21.00
使用代码
$req = http_build_query($_POST);
?
原问题:
我收到以下错误消息:
[Sun Aug 04 10:00:00 2012] [error] [client 000.0.00.000] PHP Warning: stripslashes() expects parameter 1 to be string, array given in /var/www/page.php on line 20
[Sun Aug 04 10:00:00 2012] [error] [client 000.0.00.000] PHP Stack trace:
[Sun Aug 04 10:00:00 20122] [error] [client 000.0.00.000] PHP 1. {main}() /var/www/page.php:0
[Sun Aug 04 10:00:00 2012] [error] [client 000.0.00.000] PHP 2. stripslashes() /var/www/page.php:20
问题代码是这样的:
Line 19: foreach ($_POST as $key => $value) {
Line 20: $value = urlencode(stripslashes($value));
Line 21: $req .= "&$key=$value";
Line 22: }
其中
$_POST
包含以下内容:
Array
(
[transaction] => Array
(
[0] => USD 21.00
)
[payment_request_date] => Sat Aug 04 10:00:00 PDT 2012
[return_url] => http://000.000.000.000/success.php
[fees_payer] => EACHRECEIVER
[ipn_notification_url] => http://000.000.000.000/ipn.php
[sender_email] => [email protected]
[verify_sign] => slkdfj30r2489urj0947830ru0ty80ur0er037jfgoiru932e.as329e
[test_ipn] => 1
[cancel_url] => http://000.000.000.000/cancel.php
[pay_key] => AP-SLKJ30F9J90J3RSD3
[action_type] => PAY
[transaction_type] => Adaptive Payment PAY
[tracking_id] => E4902RJF2
[status] => COMPLETED
[log_default_shipping_address_in_transaction] => false
[charset] => windows-1252
[notify_version] => UNVERSIONED
[reverse_all_parallel_payments_on_error] => false
)
由于数组中的数组,
stripslashes()
给我带来了问题吗? 如果是,我该如何处理 $_POST
循环中的 foreeach
,如果不是,可能是什么问题?
只需使用函数 http_build_query,它将为您生成一个 URL 编码的查询字符串。
$query_str = http_build_query($_POST);
function stripslashes_every_string($array){
foreach($array as $key => $value){
$array[$key] = is_array($value) ? stripslashes_every_string($value) : stripslashes($value);
}
return $array;
}
$postdata = stripslashes_every_string($_POST);