获取 Twilio 主账户的所有子账户使用资源

Fetch All SubAccount Usage Resource of Twilio Main Account

我是 PHP 和 twillio 的新手。我正在尝试获取我的子帐户的所有类型使用信息。基本上我想将此信息存储在我的 google sheet 中并需要此值

我可以像下面这样获取我所有的子账户 SID

<?php
require_once 'vendor/autoload.php';

use Twilio\Rest\Client;
$sid  = "my main account sid";
$token = "my token";

$twilio = new Client($sid, $token);

$accounts = $twilio->api->v2010->accounts
                               ->read([], 20);

$items = array();
foreach ($accounts as $record) {
    $items[] = $record->sid;
   
}
 print_r($items);
?>

这很好。我可以在 forearch 中循环我的所有子帐户以获得使用。对于 Fetch 用法,我关注 this guide

代码如下

<?php
require_once 'vendor/autoload.php';

use Twilio\Rest\Client;
$sid  = "my main account";
$token = "my token";

$client = new Client($sid, $token);

$records = $client->usage->records->read(
    array(
        "category" => "sms-outbound",
        "startDate" => "2021-02-01",
        "endDate" => "2021-02-28"
    )
);

// Loop over the list of records and echo a property for each one
foreach ($records as $record) {
    echo $record->price;
}
?>

目前我正在获取主帐户的费用。

我不知道如何获得我在上图中提到的不同类型用途的子账户的费用。我不知道我是否遵循正确的 API 指南来实现我的目标。让我知道这里是否有人可以指导我或给我举例。

非常感谢!

此处为 Twilio 开发人员布道师。

问题是您代表您的主帐户发出 API 请求,因此您从该帐户获取数据。要 make a request on behalf of a subaccount,您应该将子帐户 SID 添加到 API 客户端,如下所示:

$sid = "main account sid";
$subaccountSid = "subaccount sid";
$token = "main account token";
$client = new Client($sid, $token, $subaccountSid);

然后,当您使用此 $client 发出请求时,它们将针对子帐户发出。