PHP | Reseller API: how to get product usage from all your customers' accounts

Posted over 1 year ago by Julien Pauthier

Post a topic
J
Julien Pauthier Admin

Here is a short PHP script to query the reseller API, read the JSON structure and connect to each customer account to retrieve product usage KPI:


<?php 
define("SEPARATOR", "\t");

$server = "api.agendize.com";
$apiKey = "";
$ssoToken = "";

$arrProfiles = array();

$startDate = date("Y-m-d\T00:00:00P", strtotime('first day of previous month'));
$endDate = date("Y-m-d\T23:59:59P", strtotime('last day of previous month'));

$fileContent = "Email" . SEPARATOR . "Client name" . SEPARATOR . "Partner ID" . SEPARATOR . "Plan" . SEPARATOR . "Creation date" . SEPARATOR . "Status" . SEPARATOR . "Logins" . SEPARATOR . "Accepted Appointments" . SEPARATOR . "New clients\n";

$clients = json_decode(file_get_contents("https://$server/api/2.0/resellers/accounts?apiKey=$apiKey&token=$ssoToken"));

foreach ($clients->items as $client) {
    $nbRdv = 0;
    $nbLogins = 0;
    $nbNewClients = 0;

    if ($client->status == "enabled") {
        $arrProfiles[$client->profile->name] += 1;

        // Customer's usage report for last month
        $lastMonthStats = json_decode(file_get_contents("https://$server/api/2.0/reporting/full?startDate=$startDate&endDate=$endDate&apiKey=$apiKey&token=" . $client->ssoToken));
        if ($lastMonthStats != null) {
            $nbNewClients = $lastMonthStats->crm->newClients;

            $nbRdv = $lastMonthStats->scheduling->newAppointments;

            $nbLogins = $lastMonthStats->login->count;
        }
    }

    $fileContent .= $client->email . SEPARATOR . $client->clientName . SEPARATOR . $client->resellerId . SEPARATOR . $client->profile->name . SEPARATOR . $client->created->dateTime . SEPARATOR . $client->status . SEPARATOR . $nbLogins . SEPARATOR . $nbRdv . SEPARATOR . $nbNewClients . "\n";
}

file_put_contents(date("Y-m-d", strtotime('last day of previous month')) . "_report.tsv", $fileContent);

// Number of customer account for each profile
foreach ($arrProfiles as $profileName => $profile) {
    echo $profile . SEPARATOR . "enabled" . SEPARATOR . $profileName . "\n";
}

?>


0 Votes


0 Comments

Login or Sign up to post a comment