<?php
$domain = 'example.com';
$favicon_size = '48';
// using Google Favicon API
$api_url = 'https://www.google.com/s2/favicons?domain='.$domain.'&sz='.$favicon_size;
// retreiving the URL with PHP cURL
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $api_url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
$file_content = curl_exec($curl_handle);
curl_close($curl_handle);
// saving the favicon localy
$target_file = './cache/favicons/'.$domain.'.png';
file_put_contents($target_file, $file_content);
// and/or sending to browser
header('Content-Type: image/png');
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($target_file)).' GMT');
header('Content-Length: ' . filesize($target_file));
header('Cache-Control: public, max-age=2628000');
readfile($target_file);
exit;