zoukankan      html  css  js  c++  java
  • PbootCMS用扩展标签定制一个每日一图

    自PbootCmsV2.0.6开始,PbootCMS支持自定义标签,且升级不被覆盖。妈妈再也不用担心我的代码升级被覆盖啦。

    于是就想到用这个功能定制一个每日一图。

    这个文件位置在 home下ExtLabelController控制器。

    我们先找图源。通过百度找到必应搜索的API。

    地址是这个:

    https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1

    获取到的是一串JSON,正是我们需要的。

    用pbootcms自带的get_url方法直接抓取一下就搞定。

    上代码:

    //抓取必应每日一图
    public function getBingImage(){
        $url = 'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1';
        $data = json_decode(get_url($url));
        $result = $data->images[0];
        $image = 'https://www.bing.com'.$result->url;
        return $image;
    }

    然后再给自己定制一个标签就好啦。

    // 扩展单个标签
    private function diylabel()
    {
     $this->content = str_replace('{pboot:walle}', $this->getBingImage(), $this->content);
    }

    最后只要在模板文件中写上 {pboot:walle} 就调用出图片地址。把这个放在 <img> 标签中,或者放在 background 中,至此,搞定。

    下面是整个ExtLabelController文件的代码:

    <?php
    /**
     * @copyright (C)2020-2099 AndyGuo .
     * @author AndyGuo
     * @email vip@d163.net
     * @date 2020年4月5日
     *  个人扩展标签可编写到本类中,升级不会覆盖
     */
    namespace app//home//controller; /* 此处双斜杠需要换成单反斜杠*/
    
    use core//basic//Controller;  /* 此处双斜杠需要换成单反斜杠*/
    
    class ExtLabelController
    {
    
        protected $content;
    
        /* 必备启动函数 */
        public function run($content)
        {
            // 接收数据
            $this->content = $content;
            
            // 执行个人自定义标签函数
            $this->diylabel();
            
            // 返回数据
            return $this->content;
        }
    
        // 扩展单个标签
        private function diylabel()
        {
            $this->content = str_replace('{pboot:walle}', $this->getBingImage(), $this->content);
        }
    
        //抓取必应每日一图
        public function getBingImage(){
            $url = 'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1';
            $data = json_decode(get_url($url));
            $result = $data->images[0];
            $image = 'https://www.bing.com'.$result->url;
            return $image;
        }
        
    }

    本文来源:http://www.d163.net/html/php/35.html -- 小郭博客(AndyGuo)

    学无止境
  • 相关阅读:
    SpringBoot+MyBatis通过ScriptRunner读取SQL文件
    Redis 分布式锁使用不当,酿成一个重大事故,超卖了100瓶飞天茅台!!!(转)
    better-scroll插件中导致fixed定位失效处理方便
    VUE SSR服务器端渲染NUXT采坑总结
    js的三种异步处理
    微信小程序支付功能讲解
    函数防抖与节流
    转:HTML5 History API 详解
    微信小程序 上拉刷新/下拉加载
    跨域你需要知道这些
  • 原文地址:https://www.cnblogs.com/andywenming/p/12651659.html
Copyright © 2011-2022 走看看