appbels / org
schema.org 的 PHP 库
v1.4.1
2017-02-25 00:00 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-29 03:16:10 UTC
README
schema.org 的 PHP 库
需求
- PHP >=5.3
安装
使用 composer
$ composer require appbels/org
<?php require_once('path/to/vendor/autoload.php'); // Start using Org/Schema library.
或者手动安装,下载 ZIP 文件并复制所有 Org 文件夹。在您的脚本中包含 autoload.php 并开始使用它。
<?php require_once('path/to/org/autoload.php'); // Start using Org/Schema library.
示例
示例来自 https://developers.google.com/search/docs/data-types/articles
// ... $newsArticle = new \Org\Schema\Thing\CreativeWork\Article\NewsArticle(); $webPage = new \Org\Schema\Thing\CreativeWork\WebPage(); $webPage->id = "https://google.com/article"; $newsArticle->mainEntityOfPage = $webPage; $newsArticle->headline = "Article headline"; $newsArticle->image = array( "https://example.com/photos/1x1/photo.jpg", "https://example.com/photos/4x3/photo.jpg", "https://example.com/photos/16x9/photo.jpg" ); $newsArticle->datePublished = "2015-02-05T08:00:00+08:00"; $newsArticle->dateModified = "2015-02-05T09:20:00+08:00"; $person = new \Org\Schema\Thing\Person(); $person->name = "John Doe"; $newsArticle->author = $person; $organization = new \Org\Schema\Thing\Organization(); $organization->name = "Google"; $logo = new \Org\Schema\Thing\CreativeWork\MediaObject\ImageObject(); $logo->url = "https://google.com/logo.jpg"; $organization->logo = $logo; $newsArticle->publisher = $organization; $newsArticle->description = "A most wonderful article"; echo $newsArticle->toJson(JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
输出结果
{
"author": {
"name": "John Doe",
"@type": "Person"
},
"dateModified": "2015-02-05T09:20:00+08:00",
"datePublished": "2015-02-05T08:00:00+08:00",
"headline": "Article headline",
"publisher": {
"logo": {
"url": "https://google.com/logo.jpg",
"@type": "ImageObject"
},
"name": "Google",
"@type": "Organization"
},
"description": "A most wonderful article",
"image": [
"https://example.com/photos/1x1/photo.jpg",
"https://example.com/photos/4x3/photo.jpg",
"https://example.com/photos/16x9/photo.jpg"
],
"mainEntityOfPage": {
"@id": "https://google.com/article",
"@type": "WebPage"
},
"@type": "NewsArticle",
"@context": "http://schema.org"
}
更多示例请查看 examples 目录。