Skip to content

Instantly share code, notes, and snippets.

@mskian
Last active June 1, 2023 05:15
Show Gist options
  • Save mskian/3c1e1148544cbb1183c37d50c16401a4 to your computer and use it in GitHub Desktop.
Save mskian/3c1e1148544cbb1183c37d50c16401a4 to your computer and use it in GitHub Desktop.
Pihole API Proxy using PHP cURL for Fetching ads Data
<?php
header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
header('X-Content-Type-Options: nosniff');
header('Strict-Transport-Security: max-age=63072000');
header('Content-type:application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: X-Requested-With');
header('X-Robots-Tag: noindex, nofollow', true);
$url = "http://<PIHOLE SERVER IP>/admin/api.php?summaryRaw=&auth=<API KEY>";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$data = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
$obj = json_decode($data);
$dns_queries_today = ($obj->dns_queries_today);
$ads_blocked_today = ($obj->ads_blocked_today);
$ads_percentage_today = ($obj->ads_percentage_today);
$clean_ads_percentage = ((int)$ads_percentage_today);
$data_clear = ["dns_queries_today"=>number_format($dns_queries_today), "ads_blocked_today"=>number_format($ads_blocked_today), "ads_percentage_today"=>$clean_ads_percentage . "%"];
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo json_encode($data_clear);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment