ホームページ  >  記事  >  バックエンド開発  >  PHP が sitemap.xml マップ関数を生成する_PHP チュートリアル

PHP が sitemap.xml マップ関数を生成する_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:25:12857ブラウズ

コードをコピーします コードは次のとおりです:


/**
* * サイトマップ更新コントローラー
*
* @author Garbin
* @usage none
*/
class SitemapApp extends FrontendApp
{
function __construct()
{
$this->SitemapApp();
}
function SitemapApp()
{
parent::__construct();
$this->_google_sitemmap_file = ROOT_PATH 。 '/data/google_sitemmap.xml';
}

関数index()
{
if (!Conf::get('sitemap_enabled'))
{
return;
}
$from = empty($_GET['から']) ? 'google' : trim($_GET['from']);
switch ($from)
{
case 'google':
$this->_output_google_sitemap();
Break;
}
}

/* *O *Google サイトマップを出力
*
*@author Garbin
*@Return void
*/
function _output_google_sitemap()
{
header("Content-type: application/xml");
echo $this->_get_google_sitemap();
}

/**
*Google SiteMapを入手してください *
*/
function _get_google_sitemap()
{
$sitemap = "";
if ($this->_google_sitemap_expired())
{
/* 已过期、再生成 */

/* 获取有更新的项目 */
$updated_items = $this->_get_updated_items($this->_get_google_sitemap_lastupdate());

/* サイトマップの再構築 */
$sitemap = $this->_build_google_sitemap($updated_items);

/* 書き込み文件*/
$this->_write_google_sitemap($sitemap);
}
else
{
/* 古いサイトマップを直接返す */
$sitemap = _contents($this->_google_sitemmap_file);
}

return $ sitemap;
}

/**O *Google サイトマップの有効期限が切れているかどうかを判断します
*
*@author Garbin
*@Return Boolean
*/
function _google_sitemap_expired()
{
if (!is_file($this->_google_sitemmap_file))
{
return true ;
}
$frequency = Conf::get('sitemap_frequency') * 3600;
$filemtime = $this->_get_google_sitemap_lastupdate();

return (time() >= $filemtime + $frequency);
}

/**
*最後の更新日を取得
*
*/
function _get_google_sitemap_lastupdate()
{
return is_file($this->_google_sitemap_file) ? filemtime($this->_google_sitemmap_file) : 0;
}

/**
*更新されたアイテムを入手してください
*
*/
function _get_updated_items($timeline = 0)
{
$timeline && $timeline -= date('Z');
$limit = 5000;
$result = array();
/* 更新された店铺 */
$model_store =& m('store');
$updated_store = $model_store->find(array(
'フィールド' => 'store_id, add_time',
'conditions' => "add_time >= {$timeline} AND state=" . STORE_OPEN,
=> ));

if (!empty($updated_store))
{
foreach ($updated_store as $_store_id => $_v)
{
$result[ ] = array(
'url' => SITE_URL . ' /index.php?app=store&id=' . $_store_id,
'lastmod' => date("Y-m-d", $_v['add_time']),
'changefreq'=> '優先順位' => '1',
);
}
}
/* 更新された文章 */
$model_article =& m('article');
$updated _article = $model_article->find(array(
'フィールド' => 'article_id, add_time',
'条件'=> "add_time >= {$timeline} AND if_show=1",
'limit' => 「0、{$limit}",
));
if (!empty($updated_article))
{
foreach ($updated_article as $_article_id => $_v)
{
{
$result[] = array(
'url' => SITE_URL . '/index.php?app=article&act=view&article_id=' . $_article_id,
=> date("Y-m-d", $_v['add_time']),
'changefreq'=> 「毎日」 ,
'優先度' =>0.8',
);
}
}

/* 更新された商品 */
$model_goods =& m('goods');
$updated_goods = $model_goods->find (array(
'fields' => 'goods_id, last_update',
'conditions' => "last_update >= {$timeline} AND if_show=1 AND Closed=0",
'制限' => 0, {$limit}",
));
if (!empty($updated_goods))
{
foreach ($updated_goods as $_goods_id => $_v)
{
$result[] = array(
' URL' =>サイトのURL 。 '/index.php?app=goods&id=' 。 $_goods_id,
'lastmod' => date("Y-m-d", $_v['last_update']),
'changefreq'=> '毎日',
'優先度' => '0.8',
);
}
}

return $result;🎜 }

/**
*GoogleSiteMap
*を生成します*/
function _build_google_sitemap($items)
{
$sitemap = "rnrn";
$sitemap .= " rn " 。 htmlentities(SITE_URL, ENT_QUOTES) 。 "rn " 。 date('Y-m-d', gmtime()) 。 "rn alwaysrn 1rn ";
if (!empty($items))
{
foreach ($ $item)
{
$sitemap .= "rn rn " 。 htmlentities($item['url'], ENT_QUOTES) 。 "rn {$item['lastmod']}rn {$item['changefreq']}rn { $item['priority']}rn ";
}
}
$sitemap .= "rn
";

return $sitemap;
}

/* *O *Google サイトマップ ファイルに書き込みます
*
*@AutHor Garbin
*@param String $ Sitemap
*@Return void
*/
function _write_google_sitemap($sitemap)
{
file_put_contents($this->_google_sitemmap_file, $sitemap);
}
}

?>



http://www.bkjia.com/PHPjc/825178.htmlwww.bkjia.com

tru​​ehttp://www.bkjia.com/PHPjc/825178.html技術記事复制代码代码如下: ?php /*** サイトマップ更新コントローラー * * @author Garbin * @usage none*/ class SitemapApp extends FrontendApp { function __construct() { $this-SitemapA...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。