Builds sitemaps for pages, images and media files and provides a class to submit them to search engines.
- 1.Installation
- 2. Features
- 3. Automatic sitemap submission
- 4. Usage
- 5. Fully tested
- 6. Author
Add the following to your composer.json
file :
{
"require": {
"sonrisa/sitemap-component":"dev-master"
}
}
This component builds sitemaps supported by the main search engines, Google and Bing, in xml and gzip formats.
The Sitemap Component is able of building the following types of sitemaps:
A sitemap that serves as a index containing references to other sitemap.xml files. More documentation can be found here.
Text content sitemaps, the most common type of sitemap found around the Internet. More documentation can be found here.
Sitemap for for images. More documentation can be found here.
Sitemap for for videos. More documentation can be found here.
Alternative for video sitemaps. More documentation can be found here.
Sitemap for news articles. More documentation can be found here.
The sitemap component follow 100% the standards, meaning that it follows strictly the contrains:
- A sitemap file cannot contain 50000 items per file.
- A sitemap file cannot be larger than 50 MBytes, uncompressed.
- An Image Sitemap file cannot contain more than 1000 images per
<url>
element.
This component also provides a method to submit the generated sitemaps to the following search engines:
- Bing
<?php
use Sonrisa\Component\Sitemap\SubmitSitemap;
// $status = array('google' => true, 'bing' => true); if everything went OK.
$status = SubmitSitemap::send('http://example.com/sitemap-index.xml');
In order to use a Sitemap Index, you need to build sitemap files first. Check out 4.3, 4.4 and 4.5.
<?php
include 'vendor/autoload.php';
use \Sonrisa\Component\Sitemap\IndexSitemap;
use \Sonrisa\Component\Sitemap\Items\IndexItem;
use \Sonrisa\Component\Sitemap\Exceptions\SitemapException;
try {
$sitemap = new IndexSitemap();
$item = new IndexItem();
$item->setLoc('http://www.example.com/sitemap.content.xml'); //Mandatory
$item->setLastMod('2005-05-10T17:33:30+08:00'); //Optional
$sitemap->add($item);
$item = new IndexItem();
$item->setLoc('http://www.example.com/sitemap.media.xml'); //Mandatory
$item->setLastMod('2005-05-10T17:33:30+08:00'); //Optional
$sitemap->add($item);
//var_dump($files) should be an array holding the sitemap files created.
$files = $sitemap->build();
$sitemap->write('path/to/public/www','sitemap.index.xml');
} catch (SitemapException $e) {
echo $e->getMessage();
}
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://www.example.com/sitemap.content.xml</loc>
<lastmod>2005-05-10T17:33:30+08:00</lastmod>
</sitemap>
<sitemap>
<loc>http://www.example.com/sitemap.media.xml</loc>
<lastmod>2005-05-10T17:33:30+08:00</lastmod>
</sitemap>
</sitemapindex>
<?php
include 'vendor/autoload.php';
use \Sonrisa\Component\Sitemap\Sitemap;
use \Sonrisa\Component\Sitemap\Items\UrlItem;
use \Sonrisa\Component\Sitemap\Exceptions\SitemapException;
try {
$sitemap = new Sitemap();
$item = new UrlItem();
$item->setLoc('http://www.example.com/'); //Mandatory
$item->setPriority('1.0'); //Optional
$item->setChangeFreq('daily'); //Optional
$item->setLastMod('2014-05-10T17:33:30+08:00'); //Optional
$sitemap->add($item);
$item = new UrlItem();
$item->setLoc('http://www.example.com/blog'); //Mandatory
$item->setPriority('0.9'); //Optional
$item->setChangeFreq('monthly'); //Optional
$item->setLastMod('2014-05-10T17:33:30+08:00'); //Optional
$sitemap->add($item);
$item = new UrlItem();
$item->setLoc('http://www.example.com/contact'); //Mandatory
$item->setPriority('0.8'); //Optional
$item->setChangeFreq('never'); //Optional
$item->setLastMod('2014-05-10T17:33:30+08:00'); //Optional
$sitemap->add($item);
//var_dump($files) should be an array holding the sitemap files created.
$files = $sitemap->build();
$sitemap->write('path/to/public/www','sitemap.index.xml');
} catch (SitemapException $e) {
echo $e->getMessage();
}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/</loc>
<lastmod>2014-05-10T17:33:30+08:00</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>http://www.example.com/blog</loc>
<lastmod>2014-05-10T17:33:30+08:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>http://www.example.com/contact</loc>
<lastmod>2014-05-10T17:33:30+08:00</lastmod>
<changefreq>never</changefreq>
<priority>0.8</priority>
</url>
</urlset>
<?php
include 'vendor/autoload.php';
use \Sonrisa\Component\Sitemap\ImageSitemap;
use \Sonrisa\Component\Sitemap\Items\ImageItem;
use \Sonrisa\Component\Sitemap\Exceptions\SitemapException;
try {
$sitemap = new ImageSitemap();
$item = new ImageItem();
$item->setLoc('http://www.example.com/logo.png'); //Mandatory
$item->setTitle('Example.com logo'); //Optional
$sitemap->add($item,'http://www.example.com/');
$item = new ImageItem();
$item->setLoc('http://www.example.com/main.png'); //Mandatory
$item->setTitle('Main image'); //Optional
$sitemap->add($item,'http://www.example.com/');
//var_dump($files) should be an array holding the sitemap files created.
$files = $sitemap->build();
$sitemap->write('path/to/public/www','sitemap.image.xml');
} catch (SitemapException $e) {
echo $e->getMessage();
}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://www.example.com/</loc>
<image:image>
<image:loc><![CDATA[http://www.example.com/logo.png]]></image:loc>
<image:title><![CDATA[Example.com logo]]></image:title>
</image:image>
<image:image>
<image:loc><![CDATA[http://www.example.com/main.png]]></image:loc>
<image:title><![CDATA[Main image]]></image:title>
</image:image>
</url>
</urlset>
<?php
include 'vendor/autoload.php';
use \Sonrisa\Component\Sitemap\VideoSitemap;
use \Sonrisa\Component\Sitemap\Items\VideoItem;
use \Sonrisa\Component\Sitemap\Exceptions\SitemapException;
try {
$sitemap = new VideoSitemap();
$item = new VideoItem();
//Mandatory values
$item->setTitle('Grilling steaks for summer');
$item->setContentLoc('http://www.example.com/video123.flv');
$item->setPlayerLoc('http://www.example.com/videoplayer.swf?video=123');
//Optional Values
$item->setDescription('Alkis shows you how to get perfectly done steaks everytime');
$item->setThumbnailLoc('http://www.example.com/thumbs/123.jpg');
$item->setPlayerLocAllowEmbedded('yes');
$item->setPlayerLocAutoplay('ap=1');
$item->setDuration(600);
$item->setExpirationDate('2009-11-05T19:20:30+08:00');
$item->setRating(4.2);
$item->setViewCount(12345);
$item->setPublicationDate('2007-11-05T19:20:30+08:00');
$item->setFamilyFriendly('yes');
$item->setRestriction('IE GB US CA');
$item->setRestrictionRelationship('allow');
$item->setGalleryLoc('http://cooking.example.com');
$item->setGalleryTitle('Cooking Videos');
$item->setPrice('0.99','EUR','rent','HD');
$item->setPrice('0.75','EUR','rent','SD');
$item->setCategory('cooking');
$item->setTag(array('action','drama','entrepreneur'));
$item->setRequiresSubscription('yes');
$item->setUploader('GrillyMcGrillerson');
$item->setUploaderInfo('http://www.example.com/users/grillymcgrillerson');
$item->setPlatform('web mobile tv');
$item->setPlatformRelationship('allow');
$item->setLive('no');
$sitemap->add($item,'http://www.example.com/');
//var_dump($files) should be an array holding the sitemap files created.
$files = $sitemap->build();
$sitemap->write('path/to/public/www','sitemap.video.xml');
} catch (SitemapException $e) {
echo $e->getMessage();
}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url>
<loc>http://www.example.com/</loc>
<video:video>
<video:thumbnail_loc><![CDATA[http://www.example.com/thumbs/123.jpg]]></video:thumbnail_loc>
<video:title><![CDATA[Grilling steaks for summer]]></video:title>
<video:description><![CDATA[Alkis shows you how to get perfectly done steaks everytime]]></video:description>
<video:content_loc><![CDATA[http://www.example.com/video123.flv]]></video:content_loc>
<video:duration><![CDATA[600]]></video:duration>
<video:expiration_date><![CDATA[2009-11-05T19:20:30+08:00]]></video:expiration_date>
<video:publication_date><![CDATA[2007-11-05T19:20:30+08:00]]></video:publication_date>
<video:restriction relationship="allow">IE GB US CA</video:restriction>
<video:gallery_loc title="Cooking Videos">http://cooking.example.com</video:gallery_loc>
<video:price currency="EUR" type="rent" resolution="HD" >0.99</video:price>
<video:price currency="EUR" type="rent" resolution="SD" >0.75</video:price>
<video:tag>action</video:tag>
<video:tag>drama</video:tag>
<video:tag>entrepreneur</video:tag>
<video:requires_subscription><![CDATA[yes]]></video:requires_subscription>
<video:uploader>GrillyMcGrillerson</video:uploader>
<video:platform relationship="allow">web mobile tv</video:platform>
<video:live><![CDATA[no]]></video:live>
</video:video>
</url>
</urlset>
<?php
include 'vendor/autoload.php';
use \Sonrisa\Component\Sitemap\MediaSitemap;
use \Sonrisa\Component\Sitemap\Items\MediaItem;
use \Sonrisa\Component\Sitemap\Exceptions\SitemapException;
try {
$sitemap = new MediaSitemap();
$sitemap->setTitle('Media RSS de ejemplo');
$sitemap->setLink('http://www.example.com/ejemplos/mrss/');
$sitemap->setDescription('Ejemplo de MRSS');
$item = new MediaItem();
//Mandatory
$item->setLink('http://www.example.com/examples/mrss/example1.html');
//Optional
$item->setContentMimeType('video/x-flv');
$item->setPlayer('http://www.example.com/shows/example/video.swf?flash_params');
$item->setContentDuration(120);
$item->setTitle('Barbacoas en verano');
$item->setDescription('Consigue que los filetes queden perfectamente hechos siempre');
$item->setThumbnailUrl('http://www.example.com/examples/mrss/example1.png');
$item->setThumbnailHeight(120);
$item->setThumbnailWidth(160);
$sitemap->add($item);
$item = new MediaItem();
$item->setLink('http://www.example.com/examples/mrss/example2.html');
$item->setContentMimeType('video/x-flv');
$item->setPlayer('http://www.example.com/shows/example/video.swf?flash_params');
$item->setContentDuration(240);
$item->setTitle('Barbacoas en invierno');
$item->setDescription('Consigue unos filetes frios');
$item->setThumbnailUrl('http://www.example.com/examples/mrss/example2.png');
$item->setThumbnailHeight(120);
$item->setThumbnailWidth(160);
$sitemap->add($item);
//var_dump($files) should be an array holding the sitemap files created.
$files = $sitemap->build();
$sitemap->write('path/to/public/www','sitemap.media.xml');
} catch (SitemapException $e) {
echo $e->getMessage();
}
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:dcterms="http://purl.org/dc/terms/">
<channel>
<title>Media RSS de ejemplo</title>
<link>http://www.example.com/ejemplos/mrss/</link>
<description>Ejemplo de MRSS</description>
<item xmlns:media="http://search.yahoo.com/mrss/" xmlns:dcterms="http://purl.org/dc/terms/">
<link>http://www.example.com/examples/mrss/example1.html</link>
<media:content type="video/x-flv" duration="120">
<media:player url="http://www.example.com/shows/example/video.swf?flash_params" />
<media:title>Barbacoas en verano</media:title>
<media:description>Consigue que los filetes queden perfectamente hechos siempre</media:description>
<media:thumbnail url="http://www.example.com/examples/mrss/example1.png" height="120" width="160"/>
</media:content>
</item>
<item xmlns:media="http://search.yahoo.com/mrss/" xmlns:dcterms="http://purl.org/dc/terms/">
<link>http://www.example.com/examples/mrss/example2.html</link>
<media:content type="video/x-flv" duration="240">
<media:player url="http://www.example.com/shows/example/video.swf?flash_params" />
<media:title>Barbacoas en invierno</media:title>
<media:description>Consigue unos filetes frios</media:description>
<media:thumbnail url="http://www.example.com/examples/mrss/example2.png" height="120" width="160"/>
</media:content>
</item>
</channel>
</rss>
<?php
include 'vendor/autoload.php';
use \Sonrisa\Component\Sitemap\NewsSitemap;
use \Sonrisa\Component\Sitemap\Items\NewsItem;
use \Sonrisa\Component\Sitemap\Exceptions\SitemapException;
try {
$sitemap = new NewsSitemap();
$item = new NewsItem();
//Mandatory values
$item->setLoc('http://www.example.org/business/article55.html');
$item->setTitle('Companies A, B in Merger Talks');
$item->setPublicationDate('2008-12-23');
$item->setPublicationName('The Example Times');
$item->setPublicationLanguage('en');
//Optional Values
$item->setAccess('Subscription');
$item->setKeywords('business, merger, acquisition, A, B');
$item->setStockTickers('NASDAQ:A, NASDAQ:B');
$item->setGenres('PressRelease, Blog');
$this->sitemap->add($item);
$sitemap->add($item);
//var_dump($files) should be an array holding the sitemap files created.
$files = $sitemap->build();
$sitemap->write('path/to/public/www','sitemap.news.xml');
} catch (SitemapException $e) {
echo $e->getMessage();
}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
<url>
<loc>http://www.example.org/business/article55.html</loc>
<news:news>
<news:publication>
<news:name>The Example Times</news:name>
<news:language>en</news:language>
</news:publication>
<news:access>Subscription</news:access>
<news:genres>PressRelease, Blog</news:genres>
<news:publication_date>2008-12-23</news:publication_date>
<news:title>Companies A, B in Merger Talks</news:title>
<news:keywords>business, merger, acquisition, A, B</news:keywords>
<news:stock_tickers>NASDAQ:A, NASDAQ:B</news:stock_tickers>
</news:news>
</url>
</urlset>
Testing has been done using PHPUnit and Travis-CI. All code has been tested to be compatible from PHP 5.3 up to PHP 5.6 and Facebook's PHP Virtual Machine: HipHop.
Nil Portugués Calderó