2015-12-21 56 views
0

我在用「php-json-ld」打印正確的JSONLD for Google Accelerated Pages(AMP,www.ampproject.org)時遇到問題(github.com/digitalbazaar/php- json-ld),如以下示例所示:github.com/ampproject/amphtml/blob/master/examples/metadata-examples/article-json-ld.amp.html針對Google AMP的JSONLD with php-json-ld

更具體地說:我想知道如何添加在「@type」: 「NewsArticle」使用PHP-JSON-LD的功能:

$doc = (object)array(  
    "https://schema.org/article" => 'Article', 
    "http://schema.org/name" => "Manu Sporny", 
    "http://schema.org/url" => (object)array("@id" =>  "http://manu.sporny.org/"), 
    "http://schema.org/image" => (object)array("@id" => "http://manu.sporny.org/images/manu.png") 
); 

    $context = (object)array(
    "article" => (object)array("https://schema.org/Article"), 
    "name" => "http://schema.org/name", 
    "homepage" => (object)array("@id" => "http://schema.org/url", "@type" => "@id"), 
    "image" => (object)array("@id" => "http://schema.org/image", "@type" => "@id") 
); 

    //Print Json-LP 
    echo '<script type="application/ld+json">'; 
    echo json_encode($jsonld_compacted, 
       JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); 
    echo '</script>'; 

    //Result: 
    <script type="application/ld+json">{ 
     "@context": "http://schema.org", 
     "image": "http://manu.sporny.org/images/manu.png", 
     "name": "Manu Sporny", 
     "url": "http://manu.sporny.org/" 
    }</script> 

誰能幫助?

回答

6

如果您不需要轉換JSON-LD,則不需要php-json-ld(或任何其他庫)。一個簡單的關聯數組,您序列化爲JSON就足夠了:

$data = array(
    "@context" => "http://schema.org", 
    "@type" => "NewsArticle", 
    ... 
); 

... 

echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);