PHP ActiveX Requires Chilkat v11.0.0+
PHP ActiveX
SendGrid: Report daily requests, deliveries, bounces, and blocks
See more SendGrid Examples
Retrieve seven completed UTC dates and print one row per date. The counts are account-level aggregates; a day’s delivered count does not identify which messages sent that day were delivered.
Chilkat PHP ActiveX Downloads
<?php
$success = 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = 0;
$http = new COM("Chilkat.Http");
$http->RequireSslCertVerify = 1;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
$http->AuthToken = 'SENDGRID_API_KEY';
$resp = new COM("Chilkat.HttpResponse");
$day = new COM("Chilkat.CkDateTime");
$day->SetFromCurrentSystemTime();
$day->AddDays(-1);
$http->SetUrlVar('end',$day->getAsIso8601('YYYY-MM-DD',0));
$day->AddDays(-6);
$http->SetUrlVar('start',$day->getAsIso8601('YYYY-MM-DD',0));
$success = $http->HttpNoBody('GET','https://api.sendgrid.com/v3/stats?start_date={$start}&end_date={$end}&aggregated_by=day',$resp);
if ($success != 1) {
if ($resp->StatusCode == 0) {
print $http->LastErrorText . "\n";
}
else {
print 'HTTP ' . $resp->StatusCode . ': ' . $resp->BodyStr . "\n";
}
exit;
}
if ($resp->StatusCode != 200) {
print 'HTTP ' . $resp->StatusCode . ': ' . $resp->BodyStr . "\n";
exit;
}
$days = new COM("Chilkat.JsonArray");
$success = $days->Load($resp->BodyStr);
if ($success != 1) {
print 'Unexpected response: expected JSON.' . "\n";
exit;
}
print 'Date,Requests,Delivered,Bounces,Blocks' . "\n";
$row = new COM("Chilkat.JsonObject");
$i = 0;
while (($i < $days->Size)) {
$days->ObjectAt2($i,$row);
print $row->stringOf('date') . ',' . $row->IntOf('stats[0].metrics.requests') . ',' . $row->IntOf('stats[0].metrics.delivered') . ',' . $row->IntOf('stats[0].metrics.bounces')
. ',' . $row->IntOf('stats[0].metrics.blocks') . "\n";
$i = $i + 1;
}
?>