【发布时间】:2016-05-03 00:10:06
【问题描述】:
我的代码很大,因为我正在为 WordPress 开发订阅 PHP 插件。然而,尽管大部分工作都完成了,但我现在面临一个无法修复的故障。这是流程:
- 用户在注册表单中填写运输、账单和帐户详细信息
- 我创建了他的帐户 (WordPress)
- 确认后,我打电话给 \Stripe\Customer::create() 并提供他的电子邮件和卡详细信息
- Stripe 创建客户并将其作为 JSON 响应发送
- 我将 Stripe 客户对象转换为一个名为 $object (->__toArray( true )) 的数组,然后将每个字段插入我的数据库中
这就是我的 foreach 发挥作用的地方。我在 foreach 之前做了一个 error_log( $object ),只是为了确保我需要的一切都是正确的。
[26-Jan-2016 12:01:58 UTC] Array
(
[id] => cus_7n5hEVJs2btpg8
[object] => customer
[account_balance] => 0
[created] => 1453809716
[currency] =>
[default_source] => card_17XVWCIh4e6dX4rQYJ8JzE7e
[delinquent] =>
[description] =>
[discount] =>
[email] => hello@xfsgfs.com
[livemode] =>
[metadata] => Array
(
)
[shipping] =>
[sources] => Array
(
[object] => list
[data] => Array
(
[0] => Array
(
[id] => card_17XVWCIh4e6dX4rQYJ8JzE7e
[object] => card
[address_city] =>
[address_country] =>
[address_line1] =>
[address_line1_check] =>
[address_line2] =>
[address_state] =>
[address_zip] => W12 8QQ
[address_zip_check] => pass
[brand] => Visa
[country] => US
[customer] => cus_7n5hEVJs2btpg8
[cvc_check] => pass
[dynamic_last4] =>
[exp_month] => 1
[exp_year] => 2019
[fingerprint] => FIYdIZ89jqCdZ9G4
[funding] => credit
[last4] => 4242
[metadata] => Array
(
)
[name] => Lazhar Ich
[tokenization_method] =>
)
)
[has_more] =>
[total_count] => 1
[url] => /v1/customers/cus_7n5hEVJs2btpg8/sources
)
[subscriptions] => Array
(
[object] => list
[data] => Array
(
)
[has_more] =>
[total_count] => 0
[url] => /v1/customers/cus_7n5hEVJs2btpg8/subscriptions
)
)
但是,遍历每个 $k/$v 的 foreach 循环会在两到三次迭代后停止,我的实际页面给我一个 Chrome 错误消息“err_empty_response。
[26-Jan-2016 11:58:02 UTC] id = cus_7n5diIO2wjeaYX
[26-Jan-2016 11:58:02 UTC] object = customer
我的实际循环是这样的:
public function reconcile( $object = null, $action = null ) {
$exists = $this->exists();
if( $exists ) {
if( !$object )
$object = $this->retrieve();
if( !is_array( $object ) && !($object instanceof stdClass) )
$object = $object->__toArray( true );
error_log( print_r( $object, true ) );
foreach ( $object as $key => $value ) {
error_log( $key . ' = ' . $this->formatvalue( $value, false, true ) );
$this->$key = $this->formatvalue( $value, false, true );
}
error_log( 'update to be called next.' );
$this->update();
}
}
public function formatvalue( $value, $serialize = true, $nulltoemptystring = false ) {
if( !isset( $value ) )
return $nulltoemptystring ? '' : null;
elseif( is_int( $value ) && $value == '0' )
return 0;
elseif( is_array( $value ) )
return $serialize ? serialize( $value ) : $value;
elseif( is_bool( $value ) )
return $value ? 1 : 0;
elseif( trim( $value ) === '' )
return '';
else
return $value;
}
我很困惑,为什么它会停在 object(只是一个字符串)或 account_balance(只是一个 int,通常为 0)并且永远不会去 created 和其余部分。
EDIT2:为了确保它与这些特定的数组键或值无关,我更改了代码并且发生了相同的行为,它在几次迭代后停止
[26-Jan-2016 13:02:13 UTC] id = (string) cus_7n6fFVw0w3awiy
[26-Jan-2016 13:02:13 UTC] created = (integer) 1453813333
结果来自:
if( $key != 'object' && $key != 'account_balance' ) {
$v = $this->formatvalue( $value, false, true );
error_log( $key . ' = (' . gettype( $v ) . ') ' . $v );
$this->$key = $this->formatvalue( $value, false, true );
}
所以它甚至与键或值无关......
【问题讨论】:
-
1) 它显然是正确的数组,因为我在循环之前 print_r 它,这正是我想要的 2) 问题发生在 foreach 循环中,因为它停止后没有执行任何代码.
-
所以你没有得到
error_log在你的foreach中创建一个日志?会被执行吗? -
@CodeGodie 我确实得到了它,但只有两次:[26-Jan-2016 11:58:02 UTC] id = cus_7n5diIO2wjeaYX [26-Jan-2016 11:58:02 UTC] object = customer
-
id 说问题出在你的
formatvalue()函数上。手动测试每个条件以查看其产生的结果。 -
@Lazhar 您能否在不先将其转换为数组的情况下提供 JSON 响应的
var_dump?我想看看你得到的实际响应。
标签: php arrays loops foreach stripe-payments