evabee / schema-org-json-ld
用于生成json-ld丰富结果的库
0.0.4
2024-06-13 18:49 UTC
Requires
- php: >=8.1.0
Requires (Dev)
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2024-09-13 19:30:33 UTC
README
用于生成json-ld丰富结果的库
使用Google“丰富结果测试”进行测试和验证: https://search.google.com/test/rich-results/result?id=eeuHnX6wLe1IqxNkqd42xA
正在进行中;目前仅支持 Product 和 Offer schema.org Thing,以及它们的一些(但不是全部)基本属性。欢迎提交Pull Requests!
安装
composer require evabee/schema-org-json-ld
请注意,替代的供应商名称是正确的。由于Packagist想要过度的GitHub权限,我无法访问默认的供应商命名空间。
示例用法
$product = new Product( name: "Executive Anvil", image: [ "https://example.com/photos/1x1/photo.jpg", "https://example.com/photos/4x3/photo.jpg", "https://example.com/photos/16x9/photo.jpg" ], description: "Sleeker than ACME's Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.", sku: "0446310786", brand: new Brand( name: "ACME (tm)", ), mpn: "ACME0444246625", weight: new QuantitativeValue( value: 55.67, unitCode: "LBR" ), offers: [ new Offer( url: "https://example.com/anvil", priceCurrency: "USD", price: 119.99, itemCondition: OfferItemCondition::NewCondition, availability: ItemAvailability::InStock, shippingDetails: [ new OfferShippingDetails( shippingDestination: new DefinedRegion( addressCountry: "US", addressRegion: [ "CA", "NV", "AZ" ] ), shippingRate: new MonetaryAmount( value: 3.49, currency: "USD", ), deliveryTime: new ShippingDeliveryTime( handlingTime: new QuantitativeValue( unitCode: "DAY", minValue: 0, maxValue: 1 ), transitTime: new QuantitativeValue( unitCode: "DAY", minValue: 1, maxValue: 5 ) ) ), new OfferShippingDetails( shippingDestination: new DefinedRegion( addressCountry: "US", addressRegion: [ "HI" ] ), shippingRate: new MonetaryAmount( value: 77.49, currency: "USD", ), deliveryTime: new ShippingDeliveryTime( handlingTime: new QuantitativeValue( unitCode: "DAY", minValue: 0, maxValue: 1 ), transitTime: new QuantitativeValue( unitCode: "DAY", minValue: 4, maxValue: 10 ) ) ), new OfferShippingDetails( shippingDestination: new DefinedRegion( addressCountry: "US", addressRegion: [ "AK" ] ), doesNotShip: true, ), ] ) ] ); $json = JsonLdGenerator::SchemaToJson( schema: $product );
... 这将输出
{
"@context": "https://schema.org/",
"@type": "Product",
"name": "Executive Anvil",
"image": [
"https://example.com/photos/1x1/photo.jpg",
"https://example.com/photos/4x3/photo.jpg",
"https://example.com/photos/16x9/photo.jpg"
],
"description": "Sleeker than ACME's Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.",
"sku": "0446310786",
"offers": [
{
"@type": "Offer",
"url": "https://example.com/anvil",
"priceCurrency": "USD",
"price": 119.99,
"itemCondition": "https://schema.org/NewCondition",
"availability": "https://schema.org/InStock",
"shippingDetails": [
{
"@type": "OfferShippingDetails",
"shippingDestination": {
"@type": "DefinedRegion",
"addressCountry": "US",
"addressRegion": [
"CA",
"NV",
"AZ"
]
},
"shippingRate": {
"@type": "MonetaryAmount",
"currency": "USD",
"value": 3.49
},
"deliveryTime": {
"@type": "ShippingDeliveryTime",
"handlingTime": {
"@type": "QuantitativeValue",
"unitCode": "DAY",
"minValue": 0,
"maxValue": 1
},
"transitTime": {
"@type": "QuantitativeValue",
"unitCode": "DAY",
"minValue": 1,
"maxValue": 5
}
}
},
{
"@type": "OfferShippingDetails",
"shippingDestination": {
"@type": "DefinedRegion",
"addressCountry": "US",
"addressRegion": [
"HI"
]
},
"shippingRate": {
"@type": "MonetaryAmount",
"currency": "USD",
"value": 77.49
},
"deliveryTime": {
"@type": "ShippingDeliveryTime",
"handlingTime": {
"@type": "QuantitativeValue",
"unitCode": "DAY",
"minValue": 0,
"maxValue": 1
},
"transitTime": {
"@type": "QuantitativeValue",
"unitCode": "DAY",
"minValue": 4,
"maxValue": 10
}
}
},
{
"@type": "OfferShippingDetails",
"shippingDestination": {
"@type": "DefinedRegion",
"addressCountry": "US",
"addressRegion": [
"AK"
]
},
"doesNotShip": true
}
]
}
],
"brand": {
"@type": "Brand",
"name": "ACME (tm)"
},
"mpn": "ACME0444246625",
"weight": {
"@type": "QuantitativeValue",
"value": 55.67,
"unitCode": "LBR"
}
}
有关更多详细信息,请参阅单元测试目录: https://github.com/EvaLok/schema-org-json-ld/tree/master/test/unit