ajur-media / json-ld-generator
极其简单的 JSON-LD 标记生成器(AJUR Media 分支)
1.0.0
2024-06-28 01:33 UTC
Requires
- php: >=7.4 | 8.*
- ext-json: *
- ext-mbstring: *
Replaces
This package is not auto-updated.
Last update: 2024-09-21 01:18:49 UTC
README
极其简单的 JSON-LD 生成器。
AJUR Media 分支,兼容 PHP7.4 & PHP 8.*
原始代码
安装
从命令行运行
$ composer require ajur-media/json-ld-generator
方法
/Context.php
create($context, array $data = [])
getProperties()
generate($flags)
上下文类型
- 文章
- 有声读物
- 海滩
- 博客文章
- 书籍
- 面包屑列表
- 联系信息
- 公司
- 创意作品
- 持续时间
- 事件
- 地理坐标
- 图像对象
- 发票
- 列表项
- 本地商家
- 媒体对象
- 音乐专辑
- 音乐团体
- 音乐播放列表
- 音乐录音
- 新闻文章
- 报价
- 订单
- 组织
- 人
- 地点
- 邮政地址
- 价格规范
- 产品
- 评分
- 食谱
- 评论
- 雕塑
- 搜索框
- 事物
- 视频对象
- 网页
- 网站
示例
快速示例
商业
$context = \AJUR\Toolkit\JsonLd\Context::create('local_business', [ 'name' => 'Consectetur Adipiscing', 'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor', 'telephone' => '555-555-5555', 'openingHours' => 'mon,tue,fri', 'address' => [ 'streetAddress' => '112 Apple St.', 'addressLocality' => 'Hamden', 'addressRegion' => 'CT', 'postalCode' => '06514', ], 'geo' => [ 'latitude' => '41.3958333', 'longitude' => '-72.8972222', ], ]); echo $context; // Will output the script tag
新闻文章
$context = \JsonLd\Context::create('news_article', [ 'headline' => 'Article headline', 'description' => 'A most wonderful article', 'mainEntityOfPage' => [ 'url' => 'https://google.com/article', ], 'image' => [ 'url' => 'https://google.com/thumbnail1.jpg', 'height' => 800, 'width' => 800, ], 'datePublished' => '2015-02-05T08:00:00+08:00', 'dateModified' => '2015-02-05T09:20:00+08:00', 'author' => [ 'name' => 'John Doe', ], 'publisher' => [ 'name' => 'Google', 'logo' => [ 'url' => 'https://google.com/logo.jpg', 'width' => 600, 'height' => 60, ] ], ]); echo $context; // Will output the script tag
自定义上下文类型
create($context, array $data = [])
方法的第一个参数也接受类名。这对于自定义上下文类型很有帮助。
<?php namespace App\JsonLd; use JsonLd\ContextTypes\AbstractContext; class FooBar extends AbstractContext { /** * Property structure * * @var array */ protected $structure = [ 'name' => null, 'description' => null, 'image' => null, 'url' => null, ]; }
$context = \JsonLd\Context::create(\App\JsonLd\FooBar::class, [ 'name' => 'Foo Foo headline', 'description' => 'Bar bar article description', 'url' => 'http://google.com', ]); echo $context; // Will output the script tag
面包屑
页面上的面包屑路径指示页面在网站层次结构中的位置。用户可以从面包屑路径的最后一级开始,逐级向上导航网站层次结构。
示例
$context = \JsonLd\Context::create('breadcrumb_list', [ 'itemListElement' => [ [ 'url' => 'https://example.com/arts', 'name' => 'Arts', ], [ 'url' => 'https://example.com/arts/books', 'name' => 'Books', ], [ 'url' => 'https://example.com/arts/books/poetry', 'name' => 'Poetry', ], ] ]);
输出
<script type="application/ld+json"> { "@context": "http:\/\/schema.org", "@type": "BreadcrumbList", "itemListElement": [ { "@type": "ListItem", "position": 1, "item": { "@id": "https:\/\/example.com\/arts", "name": "Arts" } }, { "@type": "ListItem", "position": 2, "item": { "@id": "https:\/\/example.com\/arts\/books", "name": "Books" } }, { "@type": "ListItem", "position": 3, "item": { "@id": "https:\/\/example.com\/arts\/books\/poetry", "name": "Poetry" } } ] } </script>
搜索框
带有 Google 站点链接搜索框,从搜索结果中。搜索用户有时会使用导航查询,输入已知网站或应用的名称或 URL,但一旦到达目的地,就会进行更详细的搜索。例如,在 Pinterest 上搜索披萨图钉的用户会在 Google 搜索中输入 Pinterest 或 pinterest.com(无论是从 Google 应用还是从他们的网络浏览器),然后加载网站或 Android 应用,最后搜索披萨。
示例
$context = \JsonLd\Context::create('search_box', [ 'url' => 'https://www.example.com/', 'potentialAction' => [ 'target' => 'https://query.example.com/search?q={search_term_string}', 'query-input' => 'required name=search_term_string', ], ]);
输出
<script type="application/ld+json"> { "@context": "http:\/\/schema.org", "@type": "WebSite", "url": "https:\/\/www.example.com\/", "potentialAction": { "@type": "SearchAction", "target": "https:\/\/query.example.com\/search?q={search_term_string}", "query-input": "required name=search_term_string" } } </script>