承接 nilportugues/sitemap-component 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nilportugues/sitemap-component

Composer 安装命令:

composer require nilportugues/sitemap-component

包简介

Standalone sitemap builder 100% standards compliant. Build for PHP5.3 and above.

README 文档

README

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Total Downloads License SensioLabsInsight Donate

Builds sitemaps for pages, images and media files and provides a class to submit them to search engines.

1.Installation

The recommended way to install the Sitemap Component is through Composer. Run the following command to install it:

php composer.phar require nilportugues/sitemap-component

2. Features

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:

Sitemap Index

A sitemap that serves as a index containing references to other sitemap.xml files. More documentation can be found here.

Basic Sitemap

Text content sitemaps, the most common type of sitemap found around the Internet. More documentation can be found here.

Image Sitemap

Sitemap for for images. More documentation can be found here.

Video Sitemap

Sitemap for for videos. More documentation can be found here.

Media Sitemap

Alternative for video sitemaps. More documentation can be found here.

News Sitemap

Sitemap for news articles. More documentation can be found here.

Standard compilant

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.

3. Automatic sitemap submission

This component also provides a method to submit the generated sitemaps to the following search engines:

  • Google
  • Bing

4. Usage

4.1 - Submit to search engines

<?php
use NilPortugues\Sitemap\SubmitSitemap;

// $status = ['google' => true, 'bing' => true]; if everything went OK.
$status = SubmitSitemap::send('http://example.com/sitemap-index.xml');

4.2 - Build a Sitemap Index

In order to use a Sitemap Index, you need to build sitemap files first. Check out 4.3, 4.4 and 4.5.

Creation

use NilPortugues\Sitemap\IndexSitemap;
use NilPortugues\Sitemap\Item\Index\IndexItem;
use NilPortugues\Sitemap\SitemapException;

try {
    $sitemap = new IndexSitemap('path/to/folder','sitemap.index.xml');

    $item = new IndexItem('http://www.example.com/sitemap.content.xml');
    $item->setLastMod('2005-05-10T17:33:30+08:00'); //Optional
    $sitemap->add($item);

    $item = new IndexItem('http://www.example.com/sitemap.media.xml');
    $item->setLastMod('2005-05-10T17:33:30+08:00');
    $sitemap->add($item);

    $sitemap->build();

} catch (SitemapException $e) {

    echo $e->getMessage();
}

Output

<?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>

4.3 - Build a simple Sitemap

Creation

use NilPortugues\Sitemap\Sitemap;
use NilPortugues\Sitemap\Item\Url\UrlItem;
use NilPortugues\Sitemap\SitemapException;

try {
    $sitemap = new Sitemap('path/to/folder','sitemap.index.xml');

    $item = new UrlItem('http://www.example.com/');
    $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('http://www.example.com/blog');
    $item->setPriority('0.9');
    $item->setChangeFreq('monthly');
    $item->setLastMod('2014-05-10T17:33:30+08:00');
    $sitemap->add($item);

    $item = new UrlItem('http://www.example.com/contact');
    $item->setPriority('0.8');
    $item->setChangeFreq('never');
    $item->setLastMod('2014-05-10T17:33:30+08:00');
    $sitemap->add($item);

    $sitemap->build();

} catch (SitemapException $e) {

    echo $e->getMessage();
}

Output

<?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>

4.4 - Build a Sitemap with Images

Creation

use NilPortugues\Sitemap\ImageSitemap;
use NilPortugues\Sitemap\Item\Image\ImageItem;
use NilPortugues\Sitemap\SitemapException;

try {
    $sitemap = new ImageSitemap('path/to/folder','sitemap.image.xml');

    $item = new ImageItem('http://www.example.com/logo.png');
    $item->setTitle('Example.com logo'); //Optional
    $sitemap->add($item,'http://www.example.com/');

    $item = new ImageItem('http://www.example.com/main.png');
    $item->setTitle('Main image'); //Optional
    $sitemap->add($item,'http://www.example.com/');

    $sitemap->build();

} catch (SitemapException $e) {

    echo $e->getMessage();
}

Output

<?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>

4.5 - Build a Sitemap with videos

Creation

use NilPortugues\Sitemap\VideoSitemap;
use NilPortugues\Sitemap\Item\Video\VideoItem;
use NilPortugues\Sitemap\SitemapException;

try {
    $sitemap = new VideoSitemap('path/to/folder','sitemap.video.xml');

    $item = new VideoItem(
        'Grilling steaks for summer', //Title
        'http://www.example.com/video123.flv', //URL
        'http://www.example.com/videoplayer.swf?video=123', //Player URL
        'yes', //Optional
        'ap=1' //Optional
    );

    //Optional Values
    $item->setDescription('Alkis shows you how to get perfectly done steaks everytime');
    $item->setThumbnailLoc('http://www.example.com/thumbs/123.jpg');
    $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', 'allow');
    $item->setGalleryLoc('http://cooking.example.com', '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', 'http://www.example.com/users/grillymcgrillerson');
    $item->setPlatform('web mobile tv', 'allow');
    $item->setLive('no');

    $sitemap->add($item,'http://www.example.com/');

    $files = $sitemap->build();

} catch (SitemapException $e) {
    echo $e->getMessage();
}

Output

<?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>

4.6 - Build a Media Sitemap (mRSS feed as a Sitemap)

Creation

use NilPortugues\Sitemap\MediaSitemap;
use NilPortugues\Sitemap\Item\Media\MediaItem;
use NilPortugues\Sitemap\SitemapException;

try {
    $sitemap = new MediaSitemap('path/to/folder','sitemap.media.xml');

    $sitemap->setTitle('Media RSS de ejemplo');
    $sitemap->setLink('http://www.example.com/ejemplos/mrss/');
    $sitemap->setDescription('Ejemplo de MRSS');

    $item = new MediaItem('http://www.example.com/examples/mrss/example1.html');

    //Optional
    $item->setContent('video/x-flv', 120);
    $item->setPlayer('http://www.example.com/shows/example/video.swf?flash_params');
    $item->setTitle('Barbacoas en verano');
    $item->setDescription('Consigue que los filetes queden perfectamente hechos siempre');
    $item->setThumbnail('http://www.example.com/examples/mrss/example1.png', 120, 160);

    $sitemap->add($item);

    $item = new MediaItem('http://www.example.com/examples/mrss/example2.html');
    $item->setContent('video/x-flv', 120);
    $item->setPlayer('http://www.example.com/shows/example/video.swf?flash_params');
    $item->setTitle('Barbacoas en invierno');
    $item->setDescription('Consigue unos filetes frios');
    $item->setThumbnail('http://www.example.com/examples/mrss/example2.png', 120, 160);
    $sitemap->add($item);

    $sitemap->build();

} catch (SitemapException $e) {

    echo $e->getMessage();
}

Output

<?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>

4.7 - Build a Sitemap for News

Creation

use NilPortugues\Sitemap\NewsSitemap;
use NilPortugues\Sitemap\Item\News\NewsItem;
use NilPortugues\Sitemap\SitemapException;

try {
    $sitemap = new NewsSitemap('path/to/folder','sitemap.news.xml');

    $item = new NewsItem(
        'http://www.example.org/business/article55.html', //URL
        'Companies A, B in Merger Talks', //Title
        '2008-12-23', //Publication Date
        'The Example Times', //Publication Name
        'en' //locale
    );

    //Optional Values
    $item->setAccess('Subscription');
    $item->setKeywords('business, merger, acquisition, A, B');
    $item->setStockTickers('NASDAQ:A, NASDAQ:B');
    $item->setGenres('PressRelease, Blog');

    $sitemap->add($item);
    $sitemap->build();

} catch (SitemapException $e) {
    echo $e->getMessage();
}

Output

<?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>

5. Fully tested.

Testing has been done using PHPUnit and Travis-CI. All code has been tested to be compatible from PHP 5.4 up to PHP 5.6 and Facebook's HHVM.

6. Questions?

Drop me an e-mail or get in touch with me using Gitter

--

7. Author

Nil Portugués Calderó

nilportugues/sitemap-component 适用场景与选型建议

nilportugues/sitemap-component 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 56.77k 次下载、GitHub Stars 达 50, 最近一次更新时间为 2014 年 12 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「xml」 「media」 「image」 「Sitemap」 「news」 「video」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nilportugues/sitemap-component 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nilportugues/sitemap-component 我们能提供哪些服务?
定制开发 / 二次开发

基于 nilportugues/sitemap-component 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 56.77k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 50
  • 点击次数: 30
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 50
  • Watchers: 2
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-21