自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} 就调用出图片地址。把这个放在 标签中,或者放在 background 中,至此,搞定。
下面是整个ExtLabelController文件的代码:
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;
}
}最终效果,请移步bpootcms官网首页banner图欣赏最终效果。
版权声明: 本站资源均来自互联网或会员发布,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!