Skip to content

Instantly share code, notes, and snippets.

@sebmih
Created July 17, 2013 10:27
Show Gist options
  • Select an option

  • Save sebmih/6019431 to your computer and use it in GitHub Desktop.

Select an option

Save sebmih/6019431 to your computer and use it in GitHub Desktop.
A complete script that grabs SendGrid Stats and displays them
<?php
$url = 'http://sendgrid.com/';
$user = 'SG_username';
$pass = 'SG_password';
$days = 'x';
$params = array(
'days' => $days,
'api_user' => $user,
'api_key' => $pass
);
$request = $url . 'api/stats.get.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, 0);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
$decoded_file = json_decode($response, true);
for ($i = 0; $i <= $days; $i++) {
?>
<h4>Date: <?php echo $decoded_file[$i]['date'];?> </h4>
<h5>Sent: <?php echo $decoded_file[$i]['delivered'];?> </h5>
<h5>Opened: <?php echo $decoded_file[$i]['opens'];?></h5>
<?php
}
exit();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment