php自动生成网站地图txt,自动生成百度小程序sitemap.txt文件路径

朱鸿畅
2023-12-01

因为业务需要,需要在目前项目上开发一个百度小程序,百度智能小程序上线了,但是内容每天得推送,不可能一个小程序路径一个推送吧,因为小程序路径和项目路径不一致。

因为项目是用ThinkPHP开发的,在此附上自动输出sitemap.txt代码

namespace app\index\controller;

use think\Request;

use think\Response;

use mip\Mip;

class Index extends Mip

{

public function index()

{

if ($this->domainSettingsInfo && $this->domainSettingsInfo['diySiteName']) {

$this->assign('mipTitle',$this->domainSettingsInfo['diySiteName']);

} else {

$this->assign('mipTitle',$this->mipInfo['siteName'].$this->mipInfo['indexTitle']);

}

return $this->mipView('index/index');

}

function sitemap() {

$count = model('app\article\model\Articles')->getCount(0);

$tagsCount = db('Tags')->count();

$pageNum = ceil($count / 200)+1;

$tagPageNum = ceil($tagsCount / 200)+1;

$sitemap = '<?xml version="1.0" encoding="utf-8"?>';

$sitemap .= '';

for ($i=1; $i < $pageNum; $i++) {

$sitemap .= '';

$sitemap .= '' . $this->domain . '/xml/' . $i . '.xml' . '';

$sitemap .= '' . date("Y-m-d") . '';

$sitemap .= '';

}

for ($i=1; $i < $tagPageNum; $i++) {

$sitemap .= '';

$sitemap .= '' . $this->domain . '/tagXml/' . $i . '.xml' . '';

$sitemap .= '' . date("Y-m-d") . '';

$sitemap .= '';

}

$sitemap .= '';

return Response::create($sitemap)->contentType('text/xml');;

}

function xml() {

$page = input('param.id');

$page = $page ? $page : 1;

$itemList = model('app\article\model\Articles')->getItemPushList('', $page, 200, 'publish_time', 'desc');

$xml = '<?xml version="1.0" encoding="utf-8"?>';

$xml .= '';

if ($page == 1) {

$xml .= '';

$xml .= '' . $this->domain . '/' . '';

$xml .= '' . date("Y-m-d") . '';

$xml .= 'daily';

$xml .= '1.0';

$xml .= '';

if ($this->itemCategoryList = model('app\article\model\Articles')->getCategory()) {

foreach($this->itemCategoryList as $k => $v) {

$xml .= '';

$xml .= '' . $v["url"] . '';

$xml .= '' . date("Y-m-d") . '';

$xml .= 'daily';

$xml .= '0.9';

$xml .= '';

}

}

}

foreach($itemList as $k => $v) {

$xml .= '';

$xml .= '' . $v["url"] . '';

$xml .= '' . date("Y-m-d", $v["publish_time"]) . '';

$xml .= 'daily';

$xml .= '0.6';

$xml .= '';

}

$xml .= '';

return Response::create($xml)->contentType('text/xml');;

}

function tagXml() {

$page = input('param.id');

$page = $page ? $page : 1;

$xml = '<?xml version="1.0" encoding="utf-8"?>';

$xml .= '';

$tagsList = db($this->tags)->page($page,200)->select();

if ($tagsList) {

foreach ($tagsList as $key => $val) {

if ($val['url_name']) {

$tagsList[$key]['url'] = $this->domain . '/' . $this->mipInfo['tagModelUrl'] .'/' . $val['url_name'] . '/';

} else {

$tagsList[$key]['url'] = $this->domain . '/' . $this->mipInfo['tagModelUrl'] .'/' . $val['id'] . '/';

}

$tagsList[$key]['time'] = $val['add_time'] ? date("Y-m-d", $val["add_time"]) : date("Y-m-d");

}

foreach ($tagsList as $key => $val) {

$xml .= '';

$xml .= '' . $val["url"] . '';

$xml .= '' . $tagsList[$key]['time'] . '';

$xml .= 'daily';

$xml .= '0.9';

$xml .= '';

}

}

$xml .= '';

return Response::create($xml)->contentType('text/xml');;

}

function baiduSitemapPc() {

$count = model('app\article\model\Articles')->getCount(0);

$pageNum = ceil($count / 200)+1;

$sitemap = '<?xml version="1.0" encoding="utf-8"?>';

$sitemap .= '';

for ($i=1; $i < $pageNum; $i++) {

$sitemap .= '';

$sitemap .= '' . $this->domain . '/pcXml/' . $i . '.xml' . '';

$sitemap .= '' . date("Y-m-d") . '';

$sitemap .= '';

}

$sitemap .= '';

return Response::create($sitemap)->contentType('text/xml');;

}

function pcXml() {

$page = input('param.id');

$page = $page ? $page : 1;

$itemList = model('app\article\model\Articles')->getItemPushList('', $page, 200, 'publish_time', 'desc');

$xml = '<?xml version="1.0" encoding="utf-8"?>';

$xml .= '';

foreach($itemList as $k => $v) {

$xml .= '';

$xml .= '' . $v["url"] . '';

$xml .= '' . date("Y-m-d", $v["publish_time"]) . '';

$xml .= 'daily';

$xml .= '0.6';

$xml .= '';

$xml .= '';

$xml .= '

' . $v['title'] . '';

$xml .= '';

$xml .= '';

$xml .= '';

}

$xml .= '';

return Response::create($xml)->contentType('text/xml');;

}

//百度小程序输出

function badiuSmart() {

$page = input('param.id');

$page = $page ? $page : 1;

$itemList = model('app\article\model\Articles')->getItemPushList('', $page, 3000, 'publish_time', 'desc');

//添加小程序路径

$smarturl="pages/articleDetail/articleDetail?uuid=";

foreach($itemList as $k => $v) {

$smart .=$smarturl. $v["uuid"]."\r\n";

}

return Response::create($smart)->contentType('text/html');;

}

}

 类似资料: