PHP 獲取每日BING圖 並且緩存URL鏈接到本地JSON

將獲取的BING圖鏈接進行緩存,減少服務器負擔,提升訪問速度。

<?php
$filename = "./cache.json";
if (file_exists($filename) === false) {
    file_put_contents($filename, "");
}
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$contents = json_decode($contents, true);
if (filesize($filename) === 0) {
    getBingImg();
} else {
    if ($contents['time'] === date("Ymd")) {
        Header("Location: " . $contents['url']);
    } else {
        getBingImg();
    }
}
function getBingImg()
{
    $str = json_decode(file_get_contents('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'));
    if (isset($str->images[0])) {
        $impurely = 'https://cn.bing.com' . $str->images[0]->url;
    } else {
        $impurely = false;
    }
    if ($impurely) {
        global $contents;
        if ($contents['url'] !== $impurely) {
            global $filename;
            $data = array(
                "time" => date("Ymd"),
                "url" => $impurely
            );
            $data = json_encode($data);
            file_put_contents($filename, $data);
        }
        Header("Location: " . $impurely);
        exit();
    } else {
        exit('error');
    }
}
?>

相關說明:

1、VIP會員無限製任意下載,免積分。立即前往開通>>

2、下載積分可通過日常 簽到綁定郵箱 以及 積分兌換 等途徑獲得!

3、本站資源大多存儲在雲盤,如出現鏈接失效請評論反饋,如有密碼,均為:www.ipipn.com。

4、所有站內資源僅供學習交流使用。未經原版權作者許可,禁止用於任何商業環境,否則後果自負。為尊重作者版權,請購買正版作品。

5、站內資源來源於網絡公開發表文件或網友分享,如侵犯您的權益,請聯係管理員處理。

6、本站提供的源碼、模板、軟件工具等其他資源,都不包含技術服務,請大家諒解!

7、源碼、模板等資源會隨著技術、壞境的升級而存在部分問題,還請慎重選擇。

PS.源碼均收集自網絡,如有侵犯閣下權益,請發信件至: admin@ipipn.com .


源站網 » PHP 獲取每日BING圖 並且緩存URL鏈接到本地JSON

發表評論

讚助本站發展 維持服務器消耗

全站源碼免費下載 立刻讚助