如何在 PHP 中获取 chargebee 关联数组响应

How to fetch chargebee associative array response in PHP

我正在尝试使用其 api 键从 chargebee 获取客户详细信息,响应位于关联数组中。我尝试使用以下代码获取值 $all 包含响应

foreach($all as $entry){
    $customer[] = $entry->customer();
         $card = $entry->card();
}

print_r($客户);

//try 1
    foreach($customer as $value){
      print_r($value->allowed:protected]);
    } 

//try 2
    foreach($customer as $key->$value){
          print_r($value->allowed:protected]);
        } 

我的排列方式如下

Array
(
    [0] => ChargeBee_Customer Object
        (
            [allowed:protected] => Array(
                    [0] => id
                    [1] => firstName
                    [2] => lastName
                    [3] => email
                    [4] => phone
                    [5] => company
             )
             [_values:protected] => Array
                ( [first_name] => ashutosh
                    [email] => ashutosheve@gmail.com)
             [_data:protected] => Array
               (
                    [firstName] => ashutosh
                    [email] => ashutosheve@gmail.com
                    [autoCollection] => on
                    [netTermDays] => 0
               )
          )}

一直报错

PHP Parse error:  syntax error, unexpected ':', expecting ',' or ')'

如何从这样的数组中获取值请帮助。

由于您想要获得的属性具有受保护的访问权限,因此您无法直接执行此操作。但是,PHP 中有两种方法可以做到这一点。

一种是将对象转换为数组或者第二种是使用ReflectionClass

$reflect = new ReflectionClass('ChargeBee_Customer'); // create reflection class
$p = $reflect->getProperty('allowed'); // get protected property allowed

$p->setAccessible(true); // set it accessible

foreach ($customer as $value){
      var_dump($p->getValue($value)); //get property value on the object using reflection
}

Chargebee php 库有访问每个资源属性的简单方法。

您可以访问客户的属性,如下所示:

    $customer = $all->customer();
    print($customer->id);
    print($customer->firstName);

同样,您可以访问其他资源属性。

您还可以获取完整的客户详细信息作为 json 字符串:

    $json = $customer->toJson();
    print($json);