PHP build Baidu Sitemap Sitemap.xml

Source: Internet
Author: User

<?php

Header ("Content-type:text/html;charset=utf-8");

PHP build Baidu Sitemap Sitemap.xml

Http://www.baidu.com/search/sitemaptools_help.html

Include (".. /config.inc.php ");

mysql_connect ($CFG [' Db_host '], $CFG [' Db_user '], $CFG [' Db_pass ']) or Die (' Database connection Error! ‘);

mysql_select_db ($CFG [' db_name ']);

mysql_query ("Set names UTF8");

$p = $CFG [' Tb_pre '];

Querying and getting data

$query = "Select ' Linkurl ', ' editdate ' from ' {$p}table ' where ' status ' = ' 3 ' ORDER by ' itemid ' desc limit 1000";

$result =mysql_query ($query);

$m = 0;

while ($info =mysql_fetch_array ($result, Mysql_assoc)) {

$data _array[$m] [' Loc ']= ' http://www.example.net/aaa/'. $info [' Linkurl '];

$data _array[$m] [' Lastmod ']= $info [' editdate '];

$data _array[$m] [' changefreq ']= ' daily ';

$data _array[$m] [' priority ']= ' 0.8 ';

$m + +;

}

echo "Data read successfully! <br/> ";

$dom =new DomDocument (' 1.0 ', ' utf-8 ');

Urlset

$urlset = $dom->createelement (' Urlset ');

$dom->appendchild ($urlset);

echo "begins loading data for XML. <br/> ";

$attribute _array=array ();

foreach ($data _array as $data) {

$url = $dom->createelement (' url ');

$urlset->appendchild ($url);

Create_item ($dom, $url, $data, $attribute _array);

}

echo "Data loading is complete. <br/> ";

$filesize = $dom->save (".. /sitemaps/sell_sitemap.xml ");

echo "Sell_sitemap Create success! <br/> ";

echo "Size:". Changefilesize ($filesize). " <br/> ";

echo "<a href= ' Javascript:window.history.back (); ' >go back</a><br/> ";

echo "<a href=". /sitemaps/sell_sitemap.xml ' target= ' _blank ' >view it</a> ';

function Create_item ($dom, $item, $data, $attribute) {

if (Is_array ($data)) {

foreach ($data as $key = = $val) {

Creating elements

$ $key = $dom->createelement ($key);

$item->appendchild ($ $key);

Creating element values

$text = $dom->createtextnode ($val);

$ $key->appendchild ($text);

if (Isset ($attribute [$key])) {

If there are related attributes in this field, you need to set

foreach ($attribute [$key] as $akey = = $row) {

To create an attribute node

$ $akey = $dom->createattribute ($akey);

$ $key->appendchild ($ $akey);

Creating attribute Value Nodes

$aval = $dom->createtextnode ($row);

$ $akey->appendchild ($aval);

}

}//End If

}

}//End If

}//End Function

function Changefilesize ($filesize)

{

if ($filesize >= 1073741824)

{

$filesize = Round ($filesize/1073741824, 2). ' Gb ';

} elseif ($filesize >= 1048576)

{

$filesize = Round ($filesize/1048576, 2). ' Mb ';

} elseif ($filesize >= 1024)

{

$filesize = Round ($filesize/1024, 2). ' Kb ';

} else

{

$filesize = $filesize. ' Bytes ';

}

return $filesize;

}

?>

PHP build Baidu Sitemap Sitemap.xml

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.