自动生成网站地图sitemap的php程序

WhatsApp 聊天记录保险【99元/年】,聊天记录不怕丢。

WhatsApp 免墙、翻译、多开、号码抓取、群发【99元/年】

网站地图(sitemap),相信你不会陌生吧。做SEO的,向搜索引擎提交sitemap是最保证收录的最基本的手法了。小型网站的sitemap生成一般都很简单,我一般用http://www.xml-sitemaps.com/来生成网站地图。但稍微大点的网站就不能用这个工具来生成了,而且对于动态网站来说,sitemap的最佳方式是随着内容的变化而变化,当内容变化的时候,sitemap自动重新生成并自动ping搜索引擎。今天跟大家分享一下关于网站地图sitemap生成的一段php程序。

sitemap.inc.php:是主要的生成sitemap的类。

<?php
// sitemap generator class
class Sitemap
{
// constructor receives the list of URLs to include in the sitemap
function Sitemap($items = array())
{
$this->_items = $items;
}

// add a new sitemap item
function addItem($url,
$lastmod = ”,
$changefreq = ”,
$priority = ”,
$additional_fields = array())
{
$this->_items[] = array_merge(array(‘loc’ => $url,
‘lastmod’ => $lastmod,
‘changefreq’ => $changefreq,
‘priority’ => $priority),
$additional_fields);
}

// get Google sitemap
function getGoogle()
{
ob_start();
header(‘Content-type: text/xml’);
echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>’;
echo ‘<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9″
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd”>’;
foreach ($this->_items as $i)
{
echo ‘<url>’;
foreach ($i as $index => $_i)
{
if (!$_i) continue;
echo “<$index>” . $this->_escapeXML($_i) . “</$index>”;
}
echo ‘</url>’;
}
echo ‘</urlset>’;
return ob_get_clean();
}

// escape string characters for inclusion in XML structure
function _escapeXML($str)
{
$translation = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($translation as $key => $value)
{
$translation[$key] = ‘&#’ . ord($key) . ‘;’;
}
$translation[chr(38)] = ‘&’;
return preg_replace(“/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/”,”&#38;” ,
strtr($str, $translation));
}

}
?>

sitemap.php:调用sitemap.inc.php,具体实现sitemap。

<?php
// redirect requests to dynamic to their keyword rich versions
require_once ‘/sitemap.inc.php’;

define(‘SITE_DOMAIN’, ‘http://www.jiadingqiang.com’);

// create the Sitemap object
$s = new Sitemap();

// add sitemap items
$s->addItem(SITE_DOMAIN);
$s->addItem(SITE_DOMAIN.”/aboutus.html”);
$s->addItem(SITE_DOMAIN.”/whatnew.php”);

//在这里可以通过连接数据库,生成URL并通过条用$s->addItem()加入到sitemap中。
// output sitemap
if (isset($_GET[‘target’]))
{
// generate Google sitemap
if (($target = $_GET[‘target’]) == ‘google’)
{
echo $s->getGoogle();
}
}
?>

.htaccess文件,重定向sitemap.xml文件到sitemap.php。

RewriteEngine on

RewriteRule ^sitemap.xml$ sitemap.php?target=google [L]

ping_google()函数,在网站内容更新的地方调用这个函数,可以自动通知Google网站地图更新。

function ping_google(){
$sitemapUrl = ‘http://www.miningequipments.net/sitemap.xml’;
$pingUrl = “http://www.google.com/webmasters/sitemaps/ping?sitemap=”.urlencode($sitemapUrl);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pingUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch) or die (curl_error()); //执行curl请求
//echo $result;
curl_close($ch);
}

注意:这个函数需要开启php的curl模块,具体的开启方式根据服务器的不同自己在网上找方法吧,或者直接联系主机提供商要求开启。

将这四部分加入到你的网站,原来的静态的sitemap.xml就可以实现动态化自动更新了,而且会随时通知Google服务器网站内容更新,不错吧。欢迎给我留言交流,您的支持是我继续分享的最大动力。

微信扫一扫 或 点击链接加好友

仍有疑问,点击 链接,加个 微信 好友,一起交流。

《自动生成网站地图sitemap的php程序》有16条评论

  1. 很巧,最近网站收录有问题,正想找些自动生成site map的工具,这个对zencart有用吗?

发表评论