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

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

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

话不多说,先找图源。度娘告诉我必应搜索就有每日一图的API。

地址是这个:

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

获取到的是一串JSON,正和我意。

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

上代码:

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

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

  1.     // 扩展单个标签
  2.     private function diylabel()
  3.     {
  4.         $this->content = str_replace(\'{pboot:walle}\’$this->getBingImage(), $this->content);
  5.     }

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

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

  1. <?php
  2. /**
  3.  * @copyright (C)2020-2099 Hnaoyun Inc.
  4.  * @author XingMeng
  5.  * @email hnxsh@foxmail.com
  6.  * @date 2020年3月8日
  7.  *  个人扩展标签可编写到本类中,升级不会覆盖
  8.  */
  9. namespace app//home//controller/* 此处双斜杠需要换成单反斜杠*/
  10. use core//basic//Controller;  /* 此处双斜杠需要换成单反斜杠*/
  11. class ExtLabelController
  12. {
  13.     protected $content;
  14.     /* 必备启动函数 */
  15.     public function run($content)
  16.     {
  17.         // 接收数据
  18.         $this->content = $content;
  19.         
  20.         // 执行个人自定义标签函数
  21.         $this->diylabel();
  22.         
  23.         // 返回数据
  24.         return $this->content;
  25.     }
  26.     // 扩展单个标签
  27.     private function diylabel()
  28.     {
  29.         $this->content = str_replace(\'{pboot:walle}\’$this->getBingImage(), $this->content);
  30.     }
  31.     //抓取必应每日一图
  32.     public function getBingImage(){
  33.         $url = \’https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1\’;
  34.         $data = json_decode(get_url($url));
  35.         $result = $data->images[0];
  36.         $image = \’https://www.bing.com\’.$result->url;
  37.         return $image;
  38.     }
  39.     
  40. }

最终效果,请移步首页banner图欣赏最终效果。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。