gpt4 book ai didi

php - 使用 PHP,如何访问从 Stripe API 返回的 protected _values 属性?

转载 作者:可可西里 更新时间:2023-10-31 23:07:58 24 4
gpt4 key购买 nike

我正在整合 Stripe API使用 CMS。我需要从查询中返回 _values 属性作为数组,以便数据在 CMS 中作为模板变量可用,但它始终受到保护。

我一直在使用 Reflection class获取数据,但现在我正在使用 Stripe 的 \Stripe\Plan::all(); ,我不得不多次调用我写的快捷方法来处理Reflection类。不过,它并不完全是递归的,因为我必须根据从 Stripe API 调用的方法来不同地处理它。

有没有办法真正递归地使用反射类?有什么比我只是不知道的反射类更合适的东西吗?


这是 \Stripe\Plan::all(); 的示例 var_dump() 响应:

object(Stripe\Collection)#604 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(4) {
["object"]=>
string(4) "list"
["has_more"]=>
bool(false)
["url"]=>
string(9) "/v1/plans"
["data"]=>
array(2) {
[0]=>
object(Stripe\Plan)#605 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(12) {
["id"]=>
string(8) "my_plan"
["interval"]=>
string(5) "month"
["name"]=>
string(9) "My Plan"
["created"]=>
int(1427441577)
["amount"]=>
int(20000)
["currency"]=>
string(3) "usd"
["object"]=>
string(4) "plan"
["livemode"]=>
bool(false)
["interval_count"]=>
int(1)
["trial_period_days"]=>
NULL
["metadata"]=>
object(Stripe\AttachedObject)#608 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(0) {
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#612 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#613 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["statement_descriptor"]=>
NULL
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#609 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#610 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
[1]=>
object(Stripe\Plan)#611 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(12) {
["id"]=>
string(9) "some_other_plan"
["interval"]=>
string(5) "month"
["name"]=>
string(14) "Some Other Plan"
["created"]=>
int(1427431129)
["amount"]=>
int(40000)
["currency"]=>
string(3) "usd"
["object"]=>
string(4) "plan"
["livemode"]=>
bool(false)
["interval_count"]=>
int(1)
["trial_period_days"]=>
NULL
["metadata"]=>
object(Stripe\AttachedObject)#614 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(0) {
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#618 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#619 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["statement_descriptor"]=>
NULL
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#615 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#616 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
}
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#606 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#607 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}

最佳答案

您不必使用反射 API,Stripe\Collection 类实现了 ArrayAccess ,你可以直接遍历它:

$collection = \Stripe\Plan::all();
foreach ($collection as $plan) {
// Do something with the plan
}

HereCollection 类扩展的基类。 Stripe 的 PHP 库中的几乎所有类都是如此,包括 Stripe\Plan。因此,您可以使用与普通数组一起使用的任何类型的递归。

如果您想将 _values 属性作为数组返回,您可以使用 __toArray()方法:

$array = $collection->__toArray(true);

true 参数是一个递归选项。

关于php - 使用 PHP,如何访问从 Stripe API 返回的 protected _values 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29494535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com