PHP Generate sitemap.xml Map function _php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:

/**
* Site Map Update Controller
*
* @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 ';
}

Function index ()
{
if (! Conf::get (' sitemap_enabled '))
{
Return
}
$from = Empty ($_get[' from ')? ' Google ': Trim ($_get[' from ');
Switch ($from)
{
Case ' Google ':
$this->_output_google_sitemap ();
Break
}
}

/**
* Output Google Sitemap
*
* @author Garbin
* @return void
*/
function _output_google_sitemap ()
{
Header ("Content-type:application/xml");
echo $this->_get_google_sitemap ();
}

/**
* Get Google Sitemap
*
* @author Garbin
* @return String
*/
function _get_google_sitemap ()
{
$sitemap = "";
if ($this->_google_sitemap_expired ())
{
/* Expired, regenerated */

/* Get an updated item */
$updated _items = $this->_get_updated_items ($this->_get_google_sitemap_lastupdate ());

/* Rebuild Sitemap */
$sitemap = $this->_build_google_sitemap ($updated _items);

/* Write file */
$this->_write_google_sitemap ($SITEMAP);
}
Else
{
/* Return to the old sitemap directly */
$sitemap = file_get_contents ($this->_google_sitemmap_file);
}

return $sitemap;
}

/**
* Determine if Google Sitemap is out of date
*
* @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);
}

/**
* Get Last Updated date
*
* @author Garbin
* @return int
*/
function _get_google_sitemap_lastupdate ()
{
Return Is_file ($this->_google_sitemmap_file)? Filemtime ($this->_google_sitemmap_file): 0;
}

/**
* Get updated items
*
* @author Garbin
* @return Array
*/
function _get_updated_items ($timeline = 0)
{
$timeline && $timeline-= date (' Z ');
$limit = 5000;
$result = Array ();
/* Updated Shop */
$model _store =& m (' store ');
$updated _store = $model _store->find (Array (
' Fields ' = ' store_id, Add_time ',
' Conditions ' = ' add_time >= {$timeline} and state= '. Store_open,
' Limit ' = ' 0, {$limit} ',
));

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 ' = ' daily ',
' Priority ' = ' 1 ',
);
}
}
/* Updated Article */
$model _article =& m (' article ');
$updated _article = $model _article->find (Array (
' Fields ' = ' article_id, Add_time ',
' Conditions ' = ' 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,
' Lastmod ' + date ("y-m-d", $_v[' Add_time '),
' Changefreq ' = ' daily ',
' Priority ' = ' 0.8 ',
);
}
}

/* Updated items */
$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 ',
' Limit ' = ' 0, {$limit} ',
));
if (!empty ($updated _goods))
{
foreach ($updated _goods as $_goods_id = $_v)
{
$result [] = Array (
' url ' = = Site_url. '/index.php?app=goods&id= '. $_GOODS_ID,
' Lastmod ' + date ("y-m-d", $_v[' last_update '),
' Changefreq ' = ' daily ',
' Priority ' = ' 0.8 ',
);
}
}

return $result;
}

/**
* Generate Google Sitemap
*
* @author Garbin
* @param array $items
* @return String
*/
function _build_google_sitemap ($items)
{
$sitemap = " \ r \ n \ r \ n ";
$sitemap. = " \ r \ n ". Htmlentities (Site_url, ent_quotes). " \ r \ n ". Date (' y-m-d ', Gmtime ()). " \ r \ n 1 \ r \ n";
if (!empty ($items))
{
foreach ($items as $item)
{
$sitemap. = "\ r \ r \ n ". Htmlentities ($item [' url '], ent_quotes). " \ r \ n {$item [' Lastmod ']} \ r \ n {$item [' Changefreq ']} \ r \ n {$item [' P Riority ']} \ r \ n ";
}
}
$sitemap. = "\ r \ n ";

return $sitemap;
}

/**
* Write to Google sitemap file
*
* @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.html www.bkjia.com true http://www.bkjia.com/PHPjc/825178.html techarticle Copy the code as follows: PHP/** * SITEMAP Update Controller * * @author Garbin * @usage None */class Sitemapapp extends Frontendapp {function __ Construct () {$this-sitemapa ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.