 |
Super-Logiciels pour vous |
 |
 |
AsSaydalani Un logiciel pour la gestion pharmaceutique ...
- ventes aux guichets, reglements - utilisation de codes barres - Base de données des médicament saisie et ajour - Gestion des achats et des fournisseurs - Gestion des peromptions - ... | | | |
Al Fondoki Logiciel de la gestion
hôteliere:
Réservations, main courante, poits de vente, dépôts, occupation des chambres, ... | | | |
Al Morakib Logiciel de serveillance par
camera et par internet ... | | | |
Al Adae Logiciel de gestion de paiy et de
personnel ... | | | |
| Plus |
|
Avenue des F.A.R. Rsidence Niama N6 - Ttouan (Maroc)
Tl :(+212) 8 90 60 52 25
Fax :(+212) 5 39 97 25 15
Mobile :(+212) 6 61 10 74 37
Email :mce@meta-computer.com
|
|
|
|
Acceuil php, js(426) [PHP5] Google Sitemap
|
| [PHP5] Google Sitemap |
2010-12-25 |
|
Voici une classe permettant de générer un Google Sitemap en PHP5. Vous devez activer l'extension DOM pour pouvoir l'utiliser.
Sitemap.php
class Sitemap extends DOMDocument
{
protected $root;
protected $urls;
const FREQ_ALWAYS = 'always';
const FREQ_HOURLY = 'hourly';
const FREQ_DAILY = 'daily';
const FREQ_WEEKLY = 'weekly';
const FREQ_MONTHLY = 'monthly';
const FREQ_YEARLY = 'yearly';
const FREQ_NEVER = 'never';
public function __construct()
{
parent::__construct('1.0', 'utf-8');
$this->root = $this->createElement('urlset');
$this->root->setAttribute('xmlns', 'http://www.google.com/schemas/sitemap/0.84');
$this->appendChild($this->root);
}
public function createUrl($loc, $lastMod = NULL, $changeFreq = NULL, $priority = NULL)
{
if(!is_string($loc))
{
throw new Exception("L'URL devrait être une chaîne");
}
$DOMUrl = $this->createElement('url');
$this->root->appendChild($DOMUrl);
++$this->urls;
if($this->urls >= 50000)
{
throw new Exception("Un Sitemap ne peut contenir plus de 50.000 URLs");
}
$DOMLoc = $this->createElement('loc', $loc);
$DOMUrl->appendChild($DOMLoc);
if($lastMod)
{
if(!is_int($lastMod))
{
throw new Exception("Date au format incorrect");
}
$DOMLastMod = $this->createElement('lastmod', date('Y-m-d', $lastMod));
$DOMUrl->appendChild($DOMLastMod);
}
if($changeFreq)
{
if(!in_array($changeFreq,
array
(
self::FREQ_ALWAYS, self::FREQ_DAILY, self::FREQ_HOURLY,
self::FREQ_MONTHLY, self::FREQ_NEVER, self::FREQ_WEEKLY,
self::FREQ_YEARLY
)))
{
throw new Exception("Fréquence incorrecte");
}
$DOMChangeFreq = $this->createElement('changefreq', $changeFreq);
$DOMUrl->appendChild($DOMChangeFreq);
}
if($priority)
{
if(!is_float($priority))
{
throw new Exception("Priorité devrait être un entier");
}
if($priority < 0.1)
{
throw new Exception("Priorité ne devrait pas être négatif");
}
if($priority > 1)
{
throw new Exception("Priorité ne devrait pas être supérieur à 1");
}
$DOMLastMod = $this->createElement('priority', $priority);
$DOMUrl->appendChild($DOMLastMod);
}
}
public function createElement($element, $contents = NULL)
{
return parent::createElement($element, utf8_encode($contents));
}
public function setAttribute($attribute, $contents = NULL)
{
return parent::setAttribute($attribute, utf8_encode($contents));
}
public static function getTime($date)
{
list($day, $month, $year) = split('[./-]', $date);
return mktime(0, 0, 0, $month, $day, $year);
}
}
?>
La méthode getTime() prend un paramètre chaîne et le convertit en timestamp. Elle accepte les formats :
•JJ-MM-AAAA
•JJ/MM/AAAA
•JJ.MM.AAAA
Exemple d'utilisation de la classe Sitemap :
require 'Sitemap.php';
$sitemap = new Sitemap();
$sitemap->createUrl(
'http://g-rossolini.developpez.com/',
Sitemap::getTime('16/08/2007'),
Sitemap::FREQ_MONTHLY,
0.8
);
$sitemap->createUrl(
'http://g-rossolini.developpez.com/tutoriels/php/zend-framework/debuter/',
Sitemap::getTime('05/06/2007'),
Sitemap::FREQ_YEARLY
);
$sitemap->createUrl(
'http://g-rossolini.developpez.com/tutoriels/php/les-formulaires-et-php5/',
Sitemap::getTime('11/11/2006'),
Sitemap::FREQ_YEARLY
);
$sitemap->createUrl(
'http://g-rossolini.developpez.com/comparatifs/php/templates/',
Sitemap::getTime('19/03/2007'),
Sitemap::FREQ_YEARLY
);
header('Content-Type: text/xml; charset=utf-8');
echo $sitemap->saveXML();
?>
|
| Commentaires des visiteurs : |
|
|
|
|
|