前言:我自己的导航站懒得添加站点时一个一个去复制粘贴他们的图标地址,网上找的接口和代码不是很好用,于是有了这个作品

本代码需要有redis才可使用,需要搭建在海外服务器上(因为调用的谷歌api)

使用方法:api.php?url=对方网页地址

fav_function.php

<?php
$redis = new Redis();
if (!$redis->connect('127.0.0.1', 6379, 5)) die('Redis无法链接,请联系上级重启Redis');
$redis->select(1); //选择数据库
function redis_search($key) {
    global $redis;
    $json = $redis->get($key);
    if ($json == '') {
        return false;
    } elseif ($array = json_decode($json, true)) {
        return $array;
    } else {
        return $json;
    }
}
function redis_save($key, $data, $limit = 600) {
    global $redis;
    $json = is_array($data) ? json_encode($data) : $data;
    $redis->setex($key, $limit, $json);
}
function redis_del($key) {
    global $redis;
    $redis->del($key);
}
function pr($a) {
    print_r($a);
    die;
}
function restoreUrl($shortUrl) {
    //pr($shortUrl);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $shortUrl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0');
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_NOBODY, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 15);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    $data = curl_exec($curl);
    $curlInfo = curl_getinfo($curl);
    curl_close($curl);
    if ($curlInfo['http_code'] == 301 || $curlInfo['http_code'] == 302) {
        return $curlInfo['redirect_url'];
    }
    return $shortUrl;
}
function geticon($domain) {
    $key = 'icon:'.$domain;
    if ($data = redis_search($key)) return $data;
    $url = 'https://www.google.com/s2/favicons?domain='.$domain;
    $data = curl_get(restoreUrl($url));
    redis_save($key, $data, 6000);
    echo $data;
}
function curl_get($url)
{
    $ch = curl_init($url);
    $httpheader[] = "Accept: */*";
    $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
    $httpheader[] = "Connection: close";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.4.1; zh-cn; R815T Build/JOP40D) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/4.5 Mobile Safari/533.1');
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}
function default_ico($type = 0)
{
    $file = "null.ico";
    $data = file_get_contents($file);
    return $type ? base64EncodeImage($data) : $data;
}
function get_url($url) {
    if (strstr($url, 'https')) {
        preg_match_all('/https?:\\/\\/[\\w-.%#?\\/\\\\]+/i', $url, $matches);
    } else {
        preg_match_all('/http?:\\/\\/[\\w-.%#?\\/\\\\]+/i', $url, $matches);
    }
    $url = $matches[0][0];
    return $url;
}
/**
 * 获取 GET 或 POST 过来的参数
 * @param $key 键值
 * @param $default 默认值
 * @return 获取到的内容(没有则为默认值)
 */
function getParam($key, $default = '')
{
    return trim($key && is_string($key) ? isset($_POST[$key]) ? $_POST[$key] : (isset($_GET[$key]) ? $_GET[$key] : $default) : $default);
}
//将图片转化成 base 编码
function base64EncodeImage($image_data, $mime = 'image/png')
{
    $base64_image = 'data:' . $mime . ';base64,' . chunk_split(base64_encode($image_data));
    return $base64_image;
}

api.php

<?php
/**
 * php 获取网站 favicon 图标
 */
include './fav_function.php';
$url = getParam('url');
//获取传过来的链接参数
$type = getParam('type');
//获取传过来的链接参数
if (substr($url, 0, 4) != 'http') {
    $url = 'http://' . $url;
}
if (empty($type)) {
    header('Content-type: image/x-icon');
}
//输出的是图标格式
$content = geticon($url);
if (empty($content)) {
    echo default_ico($type);
    die;
}
echo $content;
Last modification:February 6, 2022
如果觉得我的文章对你有用,请随意赞赏