所以,我在这里有一个音乐搜索引擎:http://zenek.neocsatblog.mblx.hu
当您想搜索某些内容时,搜索网址如下所示:
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>
我错了什么?
答案 0 :(得分:2)
在打印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>';
答案 1 :(得分:1)
如果您只想打印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>