| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- class SiteMap {
- public $xmlBegin = '<?xml version="1.0" encoding="UTF-8"?>
- <?xml-stylesheet type="text/xsl" href="sitemap.xsl"?>
- <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
- public $xmlEnd = '</urlset>';
- public $xmlData = '';
- public $txtData = '';
- public $defaultDate;
- public $defaultChangeFreq;
- public $defaultPriority;
- /**
- * @var int 官网类型 与 Article表use_type 同步 0 乐外卖官网 1 逐趣同城 2 逐趣校园
- */
- private $type;
- public function __construct($type)
- {
- $this->type = $type;
- $this->defaultDate = date("Y-m-d", strtotime("-1 days"));
- $this->defaultChangeFreq = 'daily';
- $this->defaultPriority = 0.6;
- }
- /**
- * 生成 sitemap.xml 内容
- *
- * @param $data
- *
- * @author lizhi <1458705589@qq.com>
- * @date 2021/7/29
- */
- public function generateXml($data = [])
- {
- $dataXml = '';
- foreach ($data as $datum) {
- if (!isset($datum['url']) || empty($datum['url'])) {
- continue;
- }
- $url = $datum['url'];
- $time = isset($datum['time']) ? $datum['time'] : $this->defaultDate;
- $changeFreq = isset($datum['changefreq']) ? $datum['changefreq'] : $this->defaultChangeFreq;
- $priority = isset($datum['priority']) ? $datum['priority'] : $this->defaultPriority;
- $dataXml.=<<<EOF
- <url>
- <loc>{$url}</loc>
- <mobile:mobile type="htmladapt"/>
- <lastmod>{$time}</lastmod>
- <changefreq>{$changeFreq}</changefreq>
- <priority>{$priority}</priority>
- </url>
- EOF;
- }
- $this->xmlData = "{$this->xmlBegin}
- {$dataXml}
- {$this->xmlEnd}";
- }
- /**
- * 生成 sitemap.txt 内容
- *
- * @param $data
- *
- * @author lizhi <1458705589@qq.com>
- * @date 2021/7/29
- */
- public function generateTxt($data)
- {
- $dataText = '';
- foreach ($data as $datum) {
- if (!isset($datum['url']) || empty($datum['url'])) {
- continue;
- }
- $url = $datum['url'];
- $dataText.= $url . "\n";
- }
- $this->txtData = "{$dataText}";
- }
- /**
- * 网页下载 sitemap.xml
- *
- * @author lizhi <1458705589@qq.com>
- * @date 2021/7/29
- */
- public function downLoadXml()
- {
- if (empty($this->xmlData)) {
- throw new \Exception("empty xml data");
- }
- header("Content-Type:text/xml charset=UTF-8");
- header("Content-Disposition:attachment; filename=sitemap.xml");
- header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
- header('Expires:0');
- header('Pragma:public');
- $fp = fopen('php://output', 'a');
- fwrite($fp, $this->xmlData);
- fclose($fp);
- die();
- }
- /**
- * 网页下载 sitemap.txt
- *
- * @author lizhi <1458705589@qq.com>
- * @date 2021/7/29
- */
- public function downLoadTxt()
- {
- if (empty($this->txtData)) {
- throw new \Exception("empty txt data");
- }
- header("Content-Type:text/text charset=UTF-8");
- header("Content-Disposition:attachment; filename=sitemap.txt");
- header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
- header('Expires:0');
- header('Pragma:public');
- $fp = fopen('php://output', 'a');
- fwrite($fp, $this->txtData);
- fclose($fp);
- die();
- }
- public function getXmlDate()
- {
- return $this->xmlData;
- }
- public function getTxtDate()
- {
- return $this->txtData;
- }
- public function syncTxtToOffice()
- {
- if (empty($this->txtData)) {
- return "生成数据错误";
- }
- $urlList = [
- Article::TYPE_ZHUQU_CITY => 'https://www.zhuqutongcheng.com/site/UpdateSiteMapTxt',
- Article::TYPE_ZHUQU_SCHOOL => 'https://www.zhuquxiaoyuan.com/site/UpdateSiteMapTxt',
- Article::TYPE_LEWAIMAI => 'https://www.lewaimai.com/site/UpdateSiteMapTxt',
- ];
- if (!Helper::isProductEnv()) {
- // 测试环境
- $urlList = [
- Article::TYPE_ZHUQU_CITY => 'http://tc.lewaimai.com/site/UpdateSiteMapTxt',
- Article::TYPE_ZHUQU_SCHOOL => 'http://xy.lewaimai.com/site/UpdateSiteMapTxt',
- Article::TYPE_LEWAIMAI => 'http://office.lewaimai.com/site/UpdateSiteMapTxt',
- ];
- }
- if (!isset($urlList[$this->type])) {
- return '类型错误';
- }
- $gzData = gzcompress($this->txtData);
- return LewaimaiHttp::POST($urlList[$this->type], [
- 'data' => $gzData,
- 'sign' => Helper::getSign($gzData)
- ]);
- }
- public function syncXmlToOffice()
- {
- if (empty($this->xmlData)) {
- return "生成数据错误";
- }
- $urlList = [
- Article::TYPE_ZHUQU_CITY => 'https://www.zhuqutongcheng.com/site/UpdateSiteMapXml',
- Article::TYPE_ZHUQU_SCHOOL => 'https://www.zhuquxiaoyuan.com/site/UpdateSiteMapXml',
- Article::TYPE_LEWAIMAI => 'https://www.lewaimai.com/site/UpdateSiteMapXml',
- ];
- if (!Helper::isProductEnv()) {
- // 测试环境
- $urlList = [
- Article::TYPE_ZHUQU_CITY => 'http://tc.lewaimai.com/site/UpdateSiteMapXml',
- Article::TYPE_ZHUQU_SCHOOL => 'http://xy.lewaimai.com/site/UpdateSiteMapXml',
- Article::TYPE_LEWAIMAI => 'http://office.lewaimai.com/site/UpdateSiteMapXml',
- ];
- }
- if (!isset($urlList[$this->type])) {
- return '类型错误';
- }
- $gzData = gzcompress($this->xmlData);
- return LewaimaiHttp::POST($urlList[$this->type], [
- 'data' => $gzData,
- 'sign' => Helper::getSign($gzData)
- ]);
- }
- }
|