如何使用Stripe API检索客户总数(无“限制”)

Sar*_*rah 5 php api stripe-payments

我正在使用PHP条带API来检索特定条带帐户的所有客户的列表。我只需要客户对象的电子邮件地址。

以下功能效果很好,但是仅回馈了10个客户。

  function getListOfCustomers($stripe){
        \Stripe\Stripe::setApiKey($stripe['secret_key']);
        $list_of_customers = \Stripe\Customer::all(array());
        return $list_of_customers['data']; 

    }
Run Code Online (Sandbox Code Playgroud)

看完后在这里对API它告诉我,“限制”参数(即\Stripe\Customer::all(array("limit" => 3));)是可选的,“默认限制为10”。所以我想这就是为什么它只回馈10个客户的原因。

我想回馈无数的客户。我想知道有人确切知道该怎么做吗?

我也在同一页面上阅读了以下内容:

您可以选择要求响应中包括与您的过滤器匹配的所有客户的总数。为此,请在您的请求中指定include [] = total_count。

但是,它并没有告诉我确切如何将其“包含在我的请求中”。我尝试了以下操作,但是我收到语法错误。

$list_of_customers = \Stripe\Customer::all(array(), include[]=total_count);
Run Code Online (Sandbox Code Playgroud)

而且我还尝试了:

$list_of_customers = \Stripe\Customer::all(array(include[]=total_count));
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助。

koo*_*jah 9

Stripe 不再支持获取对象的总数,因为该功能已被弃用一段时间。

相反,他们建议遍历所有客户以计算他们或找到您想要的特定客户。使用自动分页这真的很容易你的代码看起来像这样:

$customers = \Stripe\Customer::all(array("limit" => 100));
foreach ($customers->autoPagingIterator() as $customer){
     echo "Current customer: $customer";
}     
Run Code Online (Sandbox Code Playgroud)

它不会一次将所有客户存储在 $customers 中,只会存储一个页面。但是当您到达该页面的最后一个时,迭代器会自动为您获取下一页。