首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ChargeBee发票PHP请求,访问$invoices

ChargeBee发票PHP请求,访问$invoices
EN

Stack Overflow用户
提问于 2017-01-31 10:07:54
回答 2查看 243关注 0票数 1

我通过PHP向ChargeBee的API提出以下请求:

代码语言:javascript
复制
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()的调用都返回一个具有以下结构的对象:

代码语言:javascript
复制
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
        )

)

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

这就是我陷入困境的地方,我如何能够从对象中提取数据?

我尝试了以下几点:

代码语言:javascript
复制
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>';
}

我亦曾尝试使用以下程式码作上述多项修改:

代码语言:javascript
复制
foreach($all as $entry){
    $invoice = $entry->invoice();
    foreach($invoice as $cinvoice) {
        echo'<pre>';
        print_r($cinvoice->allowed);
        echo'</pre>';
    }
}

但我尝试的每一件事都会回来:-

代码语言:javascript
复制
Fatal error:  Uncaught exception 'Exception' with message 'Unknown property
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-31 13:13:03

文档中可以看到发票对象的公共属性。

例如,要访问它们,您只需:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-01-31 14:59:34

您希望直接访问受保护的属性。访问对象ChargeBee_Invoice中的数据是由魔术方法__get() (模型)实现的。可以查看此页的可用属性列表。此代码将帮助您开始:

代码语言:javascript
复制
<?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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41954733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档