前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Typecho 无插件获取必应每日壁纸、故事

Typecho 无插件获取必应每日壁纸、故事

作者头像
laoknas网络技术
发布2021-07-14 14:58:41
6050
发布2021-07-14 14:58:41
举报
文章被收录于专栏:LLnetk信息分享

2021年6月8日: 给主页添加背景,结合bing每日壁纸: css样式:

代码语言:javascript
复制
<style>
body {
    background-image:url('<?php _e($this->options->siteUrl() .'usr/uploads/image/'.date('Ymd').'.jpg'); ?>');
    background-color: #000000;
}
</style>

在原来的基础上添加了自定义路径变量$pach_image;注释掉了每日删除之前图片和故事信息。因为要保存图片。

代码语言:javascript
复制
function bing(){
//设置图片存储路径
代码语言:javascript
复制
$pach_image='usr/uploads/image/';
//删除之前的图片和故事
代码语言:javascript
复制
/*    for (i=1; i <=30 ; 
   @unlink(date('Ymd',$pach_image.time()-24*3600*$i).'.jpg');
    @unlink(date('Ymd',$pach_image.time()-24*3600*$i).'.json');
}*/

$img_name   = $pach_image.date('Ymd').'.jpg'; //每日图片
$coverstory = $pach_image.date('Ymd').'.json'; //每日故事 json格式
if (!file_exists($img_name)) {
    $url = "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1";
    $result = file_get_contents($url);
    $output = json_decode($result,true);
    $img_url = $output["images"][0]["url"];
    $img = file_get_contents("http://cn.bing.com".$img_url);  
    @file_put_contents($img_name,$img); //写入图片
}
if (!file_exists($coverstory)) {
    $json = file_get_contents('http://cn.bing.com/cnhp/coverstory/');
    @file_put_contents($coverstory,$json); //写入文本
}
$coverstory = json_decode(file_get_contents($coverstory),true);
return $coverstory;
}

2021年5月24日 在主题目录中,修改functions.php文件内加入下面代码:

代码语言:javascript
复制
*
* 获取必应每日壁纸、故事
* @author  Warner
* @link    https://github.com/WarnerYang
* @return  string
*/
function bing(){
    //删除之前的图片和故事
    for ($i=1; $i <=10 ; $i++) { 
        @unlink(date('Ymd',time()-24*3600*$i).'.jpg');
        @unlink(date('Ymd',time()-24*3600*$i).'.json');
    }

    $img_name   = date('Ymd').'.jpg'; //每日图片
    $coverstory = date('Ymd').'.json'; //每日故事 json格式

    if (!file_exists($img_name)) {
        $url = "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1";
        $result = file_get_contents($url);
        $output = json_decode($result,true);
        $img_url = $output["images"][0]["url"];
        $img = file_get_contents("http://cn.bing.com".$img_url);  
        @file_put_contents($img_name,$img); //写入图片
    }
    if (!file_exists($coverstory)) {
        $json = file_get_contents('http://cn.bing.com/cnhp/coverstory/');
        @file_put_contents($coverstory,$json); //写入文本
    }
    $coverstory = json_decode(file_get_contents($coverstory),true);
    return $coverstory;
}

在网页中调用:

代码语言:javascript
复制
<?php $bing = bing(); ?>

<?php _e($this->options->siteUrl() . date('Ymd') . '.jpg'); ?>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年05月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档