简体   繁体   中英

how can get content json from <scriptscript type=“application/ld+json”>

I can't find an API for Vine to get the page content's title, description and image. The JSON is in the body of the page itself in a script tag: . How do I get the content (the JSON) of this script tag using PHP so it can be parsed?

Vine page:

https://vine.co/v/igO3EbIXDlI

From page source

<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>

What to do after this?

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

When I go to that page, I don't see the script tag you are referencing. So I found a page that has one, and this is how I would do it:

<?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>';
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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