broqit/seo
Composer 安装命令:
composer require broqit/seo
包简介
Simple PHP library to help developers 🍻 do better on-page SEO optimization
关键字:
README 文档
README
Simple PHP library to help developers 🍻 do better on-page SEO optimization
PHP SEO features:
- [👷] Generate schema.org ld+json
- [🛀] Generate meta tags with twitter and open graph support
- [🌐] Generate sitemaps xml and indexes (supports: 🖺 news, 🖼 images, 📽 videos)
- [📤] Submit new sitemaps to search engines
- [📤] Indexing API
- [🙈] No dependencies
Installation:
composer require melbahja/seo
Usage:
Check this simple examples. (of course the composer autoload.php file is required)
👷 Generate schema.org
use Melbahja\Seo\Schema; use Melbahja\Seo\Schema\Thing; $schema = new Schema( new Thing('Organization', [ 'url' => 'https://example.com', 'logo' => 'https://example.com/logo.png', 'contactPoint' => new Thing('ContactPoint', [ 'telephone' => '+1-000-555-1212', 'contactType' => 'customer service' ]) ]) ); echo $schema;
Results: (formatted)
<script type="application/ld+json"> { "@context": "https://schema.org", "@graph": [ { "url": "https://example.com", "logo": "https://example.com/logo.png", "contactPoint": { "telephone": "+1-000-555-1212", "contactType": "customer service", "@type": "ContactPoint", "@context": "https://schema.org/" }, "@type": "Organization", "@context": "https://schema.org/" } ] } </script>
use Melbahja\Seo\Schema; use Melbahja\Seo\Schema\Thing; $product = new Thing('Product'); $product->name = "Foo Bar"; $product->sku = "sk12"; $product->image = "/image.jpeg"; $product->description = "testing"; $product->offers = new Thing('Offer', [ 'availability' => 'https://schema.org/InStock', 'priceCurrency' => 'USD', "price" => "119.99", 'url' => 'https://gool.com', ]); $webpage = new Thing("WebPage", [ '@id' => "https://example.com/product/#webpage", 'url' => "https://example.com/product", 'name' => 'Foo Bar', ]); $schema = new Schema( $product, $webpage, ); echo json_encode($schema, JSON_PRETTY_PRINT);
Results:
{
"@context": "https://schema.org",
"@graph": [
{
"name": "Foo Bar",
"sku": "sk12",
"image": "/image.jpeg",
"description": "testing",
"offers": {
"availability": "https://schema.org/InStock",
"priceCurrency": "USD",
"price": "119.99",
"url": "https://gool.com",
"@type": "Offer",
"@context": "https://schema.org/"
},
"@type": "Product",
"@context": "https://schema.org/"
},
{
"@id": "https://example.com/product/#webpage",
"url": "https://example.com/product",
"name": "Foo Bar",
"@type": "WebPage",
"@context": "https://schema.org/"
}
]
}
🛀 Meta Tags
use Melbahja\Seo\MetaTags; $metatags = new MetaTags(); $metatags ->title('PHP SEO') ->description('This is my description') ->meta('author', 'Mohamed Elabhja') ->image('https://avatars3.githubusercontent.com/u/8259014') ->mobile('https://m.example.com') ->canonical('https://example.com') ->shortlink('https://git.io/phpseo') ->amp('https://apm.example.com'); echo $metatags;
Results:
<title>PHP SEO</title> <meta name="title" content="PHP SEO" /> <meta name="description" content="This is my description" /> <meta name="author" content="Mohamed Elabhja" /> <link href="https://m.example.com" rel="alternate" media="only screen and (max-width: 640px)" /> <link rel="canonical" href="https://example.com" /> <link rel="shortlink" href="https://git.io/phpseo" /> <link rel="amphtml" href="https://apm.example.com" /> <meta property="twitter:title" content="PHP SEO" /> <meta property="twitter:description" content="This is my description" /> <meta property="twitter:card" content="summary_large_image" /> <meta property="twitter:image" content="https://avatars3.githubusercontent.com/u/8259014" /> <meta property="og:title" content="PHP SEO" /> <meta property="og:description" content="This is my description" /> <meta property="og:image" content="https://avatars3.githubusercontent.com/u/8259014" />
🗺 Sitemaps
$yourmap = new Sitemap(string $url, array $options = []): SitemapIndexInterface
| Option name | Description | Required ? | Default |
|---|---|---|---|
| save_path | Generated sitemaps storage path | YES | |
| sitemaps_url | Sitemap index custom url for generated sitemaps | NO | $url |
| index_name | Custom sitemap index name | NO | sitemap.xml |
Simple Example
use Melbahja\Seo\Sitemap; $sitemap = new Sitemap('https://example.com', ['save_path' => '/path/to_save/files']); $sitemap->links('blog.xml', function($map) { $map->loc('/blog')->freq('daily')->priority('0.8') ->loc('/blog/my-new-article')->freq('weekly')->lastMod('2019-03-01') ->loc('/اهلا-بالعالم')->freq('weekly'); $map->loc('/blog/hello')->freq('monthly'); }); // return bool // throws SitemapException if save_path options not exists $sitemap->save();
Results: (📂 in: /path/to_save/files/)
📁: sitemap.xml (formatted)
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated by https://git.io/phpseo --> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>https://example.com/blog.xml</loc> <lastmod>2019-03-01T14:38:02+01:00</lastmod> </sitemap> </sitemapindex>
📁: blog.xml (formatted)
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated by https://git.io/phpseo --> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>https://example.com/blog</loc> <changefreq>daily</changefreq> <priority>0.8</priority> </url> <url> <loc>https://example.com/blog/my-new-article</loc> <changefreq>weekly</changefreq> <lastmod>2019-03-01T00:00:00+01:00</lastmod> </url> <url> <loc>https://example.com/%D8%A7%D9%87%D9%84%D8%A7-%D8%A8%D8%A7%D9%84%D8%B9%D8%A7%D9%84%D9%85</loc> <changefreq>weekly</changefreq> </url> <url> <loc>https://example.com/blog/hello</loc> <changefreq>monthly</changefreq> </url> </urlset>
Multipe Sitemaps && Images
use Melbahja\Seo\Sitemap; $sitemap = new Sitemap('https://example.com'); // Instead of passing save_path to the factory you can set it later via setSavePath // also $sitemap->getSavePath() method to get the current save_path $sitemap->setSavePath('your_save/path'); // changing sitemap index name $sitemap->setIndexName('index.xml'); // For images you need to pass a option images => true $sitemap->links(['name' => 'blog.xml', 'images' => true], function($map) { $map->loc('/blog')->freq('daily')->priority('0.8') ->loc('/blog/my-new-article') ->freq('weekly') ->lastMod('2019-03-01') ->image('/uploads/image.jpeg', ['caption' => 'My caption']) ->loc('/اهلا-بالعالم')->freq('weekly'); // image(string $url, array $options = []), image options: caption, geo_location, title, license // see References -> images $map->loc('/blog/hello')->freq('monthly')->image('https://cdn.example.com/image.jpeg'); }); // another file $sitemap->links('blog_2.xml', function($map) { // Mabye you need to loop through posts form your database ? foreach (range(0, 4) as $i) { $map->loc("/posts/{$i}")->freq('weekly')->priority('0.7'); } }); $sitemap->save();
Results
📁: index.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated by https://git.io/phpseo --> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>https://example.com/blog.xml</loc> <lastmod>2019-03-01T15:13:22+01:00</lastmod> </sitemap> <sitemap> <loc>https://example.com/blog_2.xml</loc> <lastmod>2019-03-01T15:13:22+01:00</lastmod> </sitemap> </sitemapindex>
📁: blog.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated by https://git.io/phpseo --> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"> <url> <loc>https://example.com/blog</loc> <changefreq>daily</changefreq> <priority>0.8</priority> </url> <url> <loc>https://example.com/blog/my-new-article</loc> <changefreq>weekly</changefreq> <lastmod>2019-03-01T00:00:00+01:00</lastmod> <image:image> <image:caption>My caption</image:caption> <image:loc>https://example.com/uploads/image.jpeg</image:loc> </image:image> </url> <url> <loc>https://example.com/%D8%A7%D9%87%D9%84%D8%A7-%D8%A8%D8%A7%D9%84%D8%B9%D8%A7%D9%84%D9%85</loc> <changefreq>weekly</changefreq> </url> <url> <loc>https://example.com/blog/hello</loc> <changefreq>monthly</changefreq> <image:image> <image:loc>https://cdn.example.com/image.jpeg</image:loc> </image:image> </url> </urlset>
📁: blog_2.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated by https://git.io/phpseo --> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>https://example.com/posts/0</loc> <changefreq>weekly</changefreq> <priority>0.7</priority> </url> <url> <loc>https://example.com/posts/1</loc> <changefreq>weekly</changefreq> <priority>0.7</priority> </url> <url> <loc>https://example.com/posts/2</loc> <changefreq>weekly</changefreq> <priority>0.7</priority> </url> <url> <loc>https://example.com/posts/3</loc> <changefreq>weekly</changefreq> <priority>0.7</priority> </url> <url> <loc>https://example.com/posts/4</loc> <changefreq>weekly</changefreq> <priority>0.7</priority> </url> </urlset>
Sitemap with videos
$sitemap = (new Sitemap('https://example.com')) ->setSavePath('./storage/sitemaps') ->setSitemapsUrl('https://example.com/sitemaps') ->setIndexName('index.xml'); $sitemap->links(['name' => 'posts.xml', 'videos' => true], function($map) { $map->loc('/posts/clickbait-video')->video('My Clickbait Video title', [ // or thumbnail_loc 'thumbnail' => 'https://example.com/thumbnail.jpeg', 'description' => 'My description', // player_loc or content_loc one of them is required 'player_loc' => 'https://example.com/embed/81287127' // for all available options see References -> videos ]); $map->loc('posts/bla-bla'); }); $sitemap->save();
Results
📁: index.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated by https://git.io/phpseo --> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>https://example.com/sitemaps/posts.xml</loc> <lastmod>2019-03-01T15:30:02+01:00</lastmod> </sitemap> </sitemapindex>
Note: lastmod in sitemap index files are generated automatically
📁: posts.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated by https://git.io/phpseo --> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> <url> <loc>https://example.com/posts/clickbait-video</loc> <video:video> <video:description>My description</video:description> <video:player_loc>https://example.com/embed/81287127</video:player_loc> <video:title>My Clickbait Video title</video:title> <video:thumbnail_loc>https://example.com/thumbnail.jpeg</video:thumbnail_loc> </video:video> </url> <url> <loc>https://example.com/posts/bla-bla</loc> </url> </urlset>
News Sitemaps
use Melbahja\Seo\Factory; $sitemap = Factory::sitemap('https://example.com', [ // You can also customize your options by passing array to the factory like this 'save_path' => './path', 'sitemaps_url' => 'https://example.com/maps', 'index_name' => 'news_index.xml' ]); $sitemap->news('my_news.xml', function($map) { // publication: name, language // Google quote about the name: "It must exactly match the name as // it appears on your articles on news.google.com" $map->setPublication('PHP NEWS', 'en'); $map->loc('/news/12')->news( [ 'title' => 'PHP 8 Released', 'publication_date' => '2019-03-01T15:30:02+01:00', ]); $map->loc('/news/13')->news( [ 'title' => 'PHP 8 And High Performance', 'publication_date' => '2019-04-01T15:30:02+01:00' ]); }); $sitemap->save();
Results
📁: news_index.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated by https://git.io/phpseo --> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>https://example.com/maps/my_news.xml</loc> <lastmod>2019-03-01T15:57:10+01:00</lastmod> </sitemap> </sitemapindex>
📁: my_news.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated by https://git.io/phpseo --> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="https://www.google.com/schemas/sitemap-news/0.9"> <url> <loc>https://example.com/news/12</loc> <news:news> <news:publication> <news:name>PHP NEWS</news:name> <news:language>en</news:language> </news:publication> <news:title>PHP 8 Released</news:title> <news:publication_date>2019-03-01T15:30:02+01:00</news:publication_date> </news:news> </url> <url> <loc>https://example.com/news/13</loc> <news:news> <news:publication> <news:name>PHP NEWS</news:name> <news:language>en</news:language> </news:publication> <news:title>PHP 8 And High Performance</news:title> <news:publication_date>2019-04-01T15:30:02+01:00</news:publication_date> </news:news> </url> </urlset>
Google quote: ⚠ "If you submit your News sitemap before your site has been reviewed and approved by our team, you may receive errors." ⚠
🤖 Send Sitemaps To Search Engines
According to the sitemaps protocol, search engines should have a url that allow you to inform them about your new sitemap files. like: <searchengine_URL>/ping?sitemap=sitemap_url
use Melbahja\Seo\Ping; $ping = new Ping; // the void method send() will inform via CURL: google, bing and yandex about your new file $ping->send('https://example.com/sitemap_file.xml');
Indexing API
This is the first PHP library to support the new search engines indexing API (aka indexnow.org).
use Melbahja\Seo\Indexing; $indexer = new Indexing('www.example.cpm', [ 'bing.com' => 'your_api_key_here', 'yandex.com' => 'your_api_key_here', ]); // index single url. $indexer->indexUrl('https://www.example.com/page'); // index multi urls. $indexer->indexUrls(['https://www.example.com/page']);
Sponsors
Special thanks to friends who support this work financially:
References
- Sitemaps protocol (https://www.sitemaps.org/protocol.html)
- Build Sitemaps (https://support.google.com/webmasters/answer/183668?hl=en)
- News Sitemaps (https://support.google.com/webmasters/answer/74288)
- Image Sitemaps (https://support.google.com/webmasters/answer/178636)
- Video Sitemaps (https://support.google.com/webmasters/answer/80471)
- Mobile (https://developers.google.com/search/mobile-sites/mobile-seo/other-devices)
License
MIT Copyright (c) 2019-present Mohamed Elbahja
broqit/seo 适用场景与选型建议
broqit/seo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 57 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「seo」 「sitemaps」 「open graph」 「sitemap index」 「schema.org」 「meta tags」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 broqit/seo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 broqit/seo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 broqit/seo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Save time and get your Statamic site to rank better with the SEO addon for Statamic.
Netgen Open Graph Bundle is an Ibexa Platform bundle that allows simple integration with Open Graph protocol.
Help to manage meta data on Laravel Eloquent model
A tool for scraping URL resources (oEmbed, OpenGraph, Twitter cards, JSON-LD)
Save time and get your Statamic site to rank better with the SEO addon for Statamic.
Statamic Fine Seo addon
统计信息
- 总下载量: 57
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-20