2017-01-31 24 views
1

我通过PHP做如下要求ChargeBee的API: -

ChargeBee_Environment::configure("chargebee-test","test_uybGuyguyguykynkgYgkfvyt"); 
$all = ChargeBee_Invoice::all(array(
    "customer_id" => 2uyg23inuy2g3ou, 
    "limit"  => 5, 
    "status[is]" => "paid", 
    "total[lte]" => 1000, 
    "sortBy[asc]" => "date")); 

foreach($all as $entry){ 
    $invoice = $entry->invoice(); 
    echo'<pre>'; 
    print_r($invoice); 
    echo'</pre>'; 
} 

$entry->invoice()每个调用返回具有以下结构的对象:

ChargeBee_Invoice Object 
(
    [allowed:protected] => Array 
     (
      [0] => id 
      [1] => poNumber 
      [2] => customerId 
      [3] => subscriptionId 
     ) 

    [_values:protected] => Array 
     (
      [id] => 4 
      [customer_id] => 2uyg23inuy2g3ou 
      [subscription_id] => 2uyg23inuy2g3ou 
      [line_items] => Array 
       (
        [0] => Array 
         (
          [id] => li_2uyg23inuy2g3ou 
          [date_from] => 1484106779 
         ) 

       ) 

      [sub_total] => 200 
      [linked_payments] => Array 
       (
        [0] => Array 
         (
          [txn_id] => txn_2uyg23inuy2g3ou 
          [applied_amount] => 200 
          [applied_at] => 1484106781 
          [txn_status] => success 
          [txn_date] => 1484106781 
          [txn_amount] => 200 
         ) 

       ) 

    [_subTypes:protected] => Array 
     (
      [line_items] => ChargeBee_InvoiceLineItem 
      [discounts] => ChargeBee_InvoiceDiscount 
      [taxes] => ChargeBee_InvoiceTax 
     ) 

) 

(我减少了上面的数据量,因为返回的请求在这里显示的时间很长)

这是我陷入困境的地方,我如何能够extrac来自对象的数据?

我已经尝试以下操作: -

foreach($all as $entry){ 
    $invoice = $entry->invoice(); 
    echo'<pre>'; 
    print_r($invoice->allowed); 
    echo'</pre>'; 
} 

foreach($all as $entry){ 
    $invoice = $entry->invoice(); 
    echo'<pre>'; 
    print_r($invoice->allowed()); 
    echo'</pre>'; 
} 

foreach($all as $entry){ 
    $invoice = $entry->invoice(); 
    echo'<pre>'; 
    print_r($invoice->ChargeBee_Invoice); 
    echo'</pre>'; 
} 

foreach($all as $entry){ 
    $invoice = $entry->invoice(); 
    echo'<pre>'; 
    print_r($invoice->ChargeBee_Invoice()); 
    echo'</pre>'; 
} 

foreach($all as $entry){ 
    $invoice = $entry->invoice(); 
    echo'<pre>'; 
    print_r($invoice['ChargeBee_Invoice']); 
    echo'</pre>'; 
} 

foreach($all as $entry){ 
    $invoice = $entry->invoice(); 
    echo'<pre>'; 
    print_r($invoice['allowed']); 
    echo'</pre>'; 
} 

我也试图用下面的代码上面的许多变化: -

foreach($all as $entry){ 
    $invoice = $entry->invoice(); 
    foreach($invoice as $cinvoice) { 
     echo'<pre>'; 
     print_r($cinvoice->allowed); 
     echo'</pre>'; 
    } 
} 

但一切我尝试刚刚返回: -

Fatal error: Uncaught exception 'Exception' with message 'Unknown property 

任何帮助或推动正确的方向将非常赞赏

回答

1

docs您可以看到发票对象的公共属性。

要访问他们,你只是,例如:

echo "<pre>"; 
foreach($all as $entry){ 
    $invoice = $entry->invoice(); 
    echo "Invoice #{$invoice->customerId} for a total of {$invoice->amountDue} has status '{$invoice->status}'"\n; 
} 
echo "</pre>"; 

测试出来,并检查文档的其他可用的属性。

-1

要直接访问保护属性。访问对象中的数据ChargeBee_Invoice由神奇方法__get()(source code of parent class ChargeBee_Model)实现。可查看的可用属性列表this page。 此代码可以帮助您开始:

<?php 
    foreach ($all as $entry) 
    { 
     /* Get next invoice */ 
     $invoice = $entry->invoice(); 

     /* Get properties from invoice */ 
     echo $invoice->id; 
     echo $invoince->subscription_id; 

     /* And so on */ 
    } 
?> 
+0

'_GET'(一个下划线)不是一个魔术方法,它只是为类'Result'(而不是类发票,他想查询)的私有方法。尽管如此,文档中有关公共属性的一点也不错。 – yivi

+0

哦... ChargeBee_Result并链接到它 - 这是我不幸的误印。我的意思是ChargeBee_Model。这个类是ChargeBee_Invoice的父类。而我用魔术方法__get(两个下划线)访问数据的说法并没有错。查看源代码。 – Ans