繁体   English   中英

如何在PHP中为搜索结果创建JSON结构化数据?

[英]How can I create JSON structured data for search results in PHP?

因此,我在这里有一个音乐搜索引擎:http: //zenek.neocsatblog.mblx.hu

当您要搜索某些内容时,搜索URL如下所示:

http://zenek.neocsatblog.mblx.hu/search/love%20is%20gone%20david%20guetta

我想为该页面上的每个元素提供结构化数据,并带有JSON和script标签。 不幸的是,这实际上是行不通的。

我的意思是,我仅获得此页面的第一个元素,但没有任何循环。 因此,这种情况下的HTML如下所示:

<script>

{
  "@context": "http://schema.org",
  "@type": "ImageObject",
  "author": "David Guetta - Love Is Gone...",
  "contentLocation": "Budapest, Hungary",
  "contentUrl": "http://i1.sndcdn.com/artworks-000112948246-z6q0y7-large.jpg",
  "description": "David Guetta - Love Is Gone...",
  "name": "David Guetta - Love Is Gone..."
      }
</script>

我的PHP代码如下所示:

<?php
$avatar = $GLOBALS['sys']->img($search->artwork_url, "100", $search->user->avatar_url); 
$title =$GLOBALS["sys"]->sh_title(urldecode($search->title), "25"); 
?>
<script>
<?php echo '
{
  "@context": "http://schema.org",
  "@type": "ImageObject",
  "author": "'.$title.'",
  "contentLocation": "Budapest, Hungary",
  "contentUrl": "'.$avatar.'",
  "description": "'.$title.'",
  "name": "'.$title.'"
      }
'
?>
</script>

我怎么了

在打印json之前,应该使用标头。

<?php
$avatar = $GLOBALS['sys']->img($search->artwork_url, "100", $search->user->avatar_url); 
$title =$GLOBALS["sys"]->sh_title(urldecode($search->title), "25"); 
$result = array(
    "@context" => "http =>//schema.org",
    "@type" => "ImageObject",
    "author" => "'.$title.'",
    "contentLocation" => "Budapest, Hungary",
    "contentUrl" => "'.$avatar.'",
    "description" => "'.$title.'",
    "name" => "'.$title.'");

header('Content-type: application/json');
echo json_encode($result);
// Alternatively
echo '<script>var result = ' . json_encode($result) . ';</script>';

如果您只想打印对象的json数组,则可以执行以下操作:

<?php
$array = [
    [
        "@context" =>  "http://schema.org",
        "@type"  => "ImageObject",
        "author"  => "title",
        "contentLocation" => "Budapest, Hungary",
        "contentUrl" => "http://content.url",
        "description" => "description text",
        "name" => "title"
    ],
    [
        "@context" =>  "http://schema.org",
        "@type"  => "ImageObject",
        "author"  => "title",
        "contentLocation" => "Budapest, Hungary",
        "contentUrl" => "http://content.url",
        "description" => "description text",
        "name" => "title"
    ]
];

header('Content-type: application/json');
echo json_encode($array); // use json_encode($array, JSON_PRETTY_PRINT) for debugging

?>

JSON_PRETTY_PRINT的结果将是

[
    {
        "@context": "http:\/\/schema.org",
        "@type": "ImageObject",
        "author": "title",
        "contentLocation": "Budapest, Hungary",
        "contentUrl": "http:\/\/content.url",
        "description": "description text",
        "name": "title"
    },
    {
        "@context": "http:\/\/schema.org",
        "@type": "ImageObject",
        "author": "title",
        "contentLocation": "Budapest, Hungary",
        "contentUrl": "http:\/\/content.url",
        "description": "description text",
        "name": "title"
    }
]

如果要直接将json数组打印到脚本中,可以执行以下操作:

<?php
$array = [
    [
        "@context" =>  "http://schema.org",
        "@type"  => "ImageObject",
        "author"  => "title",
        "contentLocation" => "Budapest, Hungary",
        "contentUrl" => "http://content.url",
        "description" => "description text",
        "name" => "title"
    ],
    [
        "@context" =>  "http://schema.org",
        "@type"  => "ImageObject",
        "author"  => "title",
        "contentLocation" => "Budapest, Hungary",
        "contentUrl" => "http://content.url",
        "description" => "description text",
        "name" => "title"
    ]
];
?>
<button type="button" id="button">CLICK ME</button>
<script>
    var array = <?php echo json_encode($array) ?>;

    document.getElementById('button').addEventListener('click', function () {
        alert(JSON.stringify(array));
    });
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM