PHP generates sitemap. xml map function _ php instance

Source: Internet
Author: User
Find a class for generating sitemap by PHP with complete functions and clear Code. For details, refer The Code is as follows:



/**
* Website 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, regenerate */

/* Get the updated project */
$ Updated_items = $ this-> _ get_updated_items ($ this-> _ get_google_sitemap_lastupdate ());

/* Rebuilding sitemap */
$ Sitemap = $ this-> _ build_google_sitemap ($ updated_items );

/* Write File */
$ This-> _ write_google_sitemap ($ sitemap );
}
Else
{
/* Directly return the old sitemap */
$ Sitemap = file_get_contents ($ this-> _ google_sitemmap_file );
}

Return $ sitemap;
}

/**
* Determine if Google sitemap has expired
*
* @ 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 the last update 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 Projects
*
* @ Author Garbin
* @ Return array
*/
Function _ get_updated_items ($ timeline = 0)
{
$ Timeline & $ timeline-= date ('Z ');
$ Limit = 5000;
$ Result = array ();
/* Updated store */
$ Model_store = & m ('store ');
$ Updated_store = $ model_store-> find (array (
'Fields' => 'store _ id, add_time ',
'Condition' => "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 ',
'Condition' => "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 item */
$ Model_goods = & m ('goods ');
$ Updated_goods = $ model_goods-> find (array (
'Fields' => 'goods _ id, last_update ',
'Condition' => "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 Always \ R \ n 1 \ R \ n ";
If (! Empty ($ items ))
{
Foreach ($ items as $ item)
{
$ Sitemap. = "\ r \ n \ R \ n ". Htmlentities ($ item ['url'], ENT_QUOTES )." \ R \ n {$ Item ['lastmod']} \ R \ n {$ Item ['changefreq']} \ R \ n {$ Item ['priority ']} \ R \ n ";
}
}
$ Sitemap. = "\ r \ n ";

Return $ sitemap;
}

/**
* Write the Google sitemap File
*
* @ Author Garbin
* @ Param string $ sitemap
* @ Return void
*/
Function _ write_google_sitemap ($ sitemap)
{
File_put_contents ($ this-> _ google_sitemmap_file, $ sitemap );
}
}

?>

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.