获取内容 json-6ren"> 获取内容 json-我找不到 Vine 的 API 来获取页面内容的标题、描述和图像。 JSON 位于页面本身的正文中的脚本标记中: 。如何使用 PHP 获取此脚本标记的内容(JSON)以便对其进行解析? 藤蔓页面: h-6ren">
gpt4 book ai didi

javascript - 如何从 <scriptscript type ="application/ld+json"> 获取内容 json

转载 作者:行者123 更新时间:2023-12-01 03:11:44 32 4
gpt4 key购买 nike

我找不到 Vine 的 API 来获取页面内容的标题、描述和图像。 JSON 位于页面本身的正文中的脚本标记中: 。如何使用 PHP 获取此脚本标记的内容(JSON)以便对其进行解析?

藤蔓页面:

https://vine.co/v/igO3EbIXDlI

来自页面来源

<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "SocialMediaPosting",
"url": "https://vine.co/v/igO3EbIXDlI",
"datePublished": "2016-03-01T00:58:35",
"author": {
"@type": "Person",
"name": "MotorAddicts\u2122",
"image": "https://v.cdn.vine.co/r/avatars/39FEFED72B1242718633613316096_pic-r-1439261422661708f3e9755.jpg.jpg?versionId=LPjQUQ4KmTIPLu3iDbXw4FipgjEpC6fw",
"url": "https://vine.co/u/989736283540746240"
},
"articleBody": "Mmm... Black black blaaaaack!! \ud83d\ude0d ( Drift \u53d1 )",
"image": "https://v.cdn.vine.co/r/videos/98C3799A811316254965085667328_SW_WEBM_14567938452154dc600dbde.webm.jpg?versionId=wPuaQvDxnpwF7KjSGao21hoddooc3eCl",
"interactionCount": [{
"@type": "UserInteraction",
"userInteractionType": "http://schema.org/UserLikes",
"value": "1382"
}, {
"@type": "UserInteraction",
"userInteractionType": "http://schema.org/UserShares",
"value": "368"
}, {
"@type": "UserInteraction",
"userInteractionType": "http://schema.org/UserComments",
"value": "41"
}, {
"@type": "UserInteraction",
"userInteractionType": "http://schema.org/UserViews",
"value": "80575"
}],

"sharedContent": {
"@type": "VideoObject",
"name" : "Mmm... Black black blaaaaack!! \ud83d\ude0d ( Drift \u53d1 )",
"description" : "",
"thumbnailUrl" : "https://v.cdn.vine.co/r/videos/98C3799A811316254965085667328_SW_WEBM_14567938452154dc600dbde.webm.jpg?versionId=wPuaQvDxnpwF7KjSGao21hoddooc3eCl",
"uploadDate" : "2016-03-01T00:58:35",
"contentUrl" : "https://v.cdn.vine.co/r/videos_h264high/98C3799A811316254965085667328_SW_WEBM_14567938452154dc600dbde.mp4?versionId=w7ugLPYtj5LWeVUsXaH1bt2VuK8QE0qv",
"embedUrl" : "https://vine.co/v/igO3EbIXDlI/embed/simple",
"interactionCount" : "82366"
}
}
</script>

此后该怎么办?

$html = 'https://vine.co/v/igO3EbIXDlI';
$dom = new DOMDocument;
$dom->loadHTML($html);

最佳答案

当我转到该页面时,我没有看到您引用的脚本标记。所以我找到了一个页面,其中有一个,这就是我要做的:

<?php
$html = file_get_contents('https://tv-sewingcenter.com');
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
$jsons = array();
$scripts = $dom->getElementsByTagName('script');
if( ! empty( $scripts ) )
{
foreach( $scripts as $script )
{
if( $script->hasAttribute('type') && $script->getAttribute('type') == 'application/ld+json' )
{
$jsons[] = json_decode($script->nodeValue, true);
}
}

if( ! empty( $jsons ) )
{
foreach( $jsons as $json )
{
echo '<pre>';
print_r( $json );
echo '</pre>';
}
}
}

关于javascript - 如何从 &lt;scriptscript type ="application/ld+json"> 获取内容 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45784803/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com