Last active
October 24, 2017 23:09
-
-
Save jasontucker/fb30657baff14b5bce504c39af0917d1 to your computer and use it in GitHub Desktop.
Generates a PRTG friendly XML file for use with a PRTG XML sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: text/xml'); | |
// API Values | |
$api_key = ''; | |
$dc = ''; //us15 | |
$list = ''; | |
// Create the logs directory and create the two log files | |
$lastRunLog = 'logs/lastrun.log'; | |
$subfile = 'logs/subcount.log'; | |
$url = "https://{$dc}.api.mailchimp.com/3.0/lists/{$list_id}/?apikey={$api_key}"; | |
$lastRun = file_get_contents($lastRunLog); | |
if (time() - $lastRun >= 60) { | |
// it's been more than one minute so we will connect to the API | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_URL,$url); | |
$result=curl_exec($ch); | |
curl_close($ch); | |
$json = json_decode($result, true); | |
$total= $json['stats']['member_count']; | |
// update lastrun.log with current time | |
file_put_contents($lastRunLog, time()); | |
file_put_contents($subfile, $total); | |
} else { | |
$total = file_get_contents($subfile); | |
} | |
echo "<mailchimp>"; | |
echo " <subscriber_count>" . $total . "</subscriber_count>"; | |
echo "</mailchimp>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment