无法通过 PHP 从 YouTube 数据 API v3 获取 ytRating

Cannot get ytRating from YouTube Data API v3 with PHP

使用 PHP 我可以从 API 获得一些信息,但我无法获得完整的 API.

这是我目前拥有的:

// url with $vID being my video ID and $API_key being the access key
$api_url = 'https://www.googleapis.com/youtube/v3/videos?part=status&id=' . $vID . '&key=' . $API_key;

// get contents
$videoInfo = file_get_contents( $api_url );

这将使我获得 API 的第一个区块,如下所示:

VidInfo: { "kind": "youtube#videoListResponse", 
            "etag": "xxxxxxxxxxxxxx", 
            "items": [ 
                { "kind": "youtube#video", 
                "etag": "yyyyyyyyyyy", 
                "id": "zzzzzz", 
                "status": { 
                    "uploadStatus": "processed", 
                    "privacyStatus": "public", 
                    "license": "youtube", 
                    "embeddable": true, 
                    "publicStatsViewable": true, 
                    "madeForKids": false 
                    } 
                } 
            ], 
            "pageInfo": { "totalResults": 1, "resultsPerPage": 1 } 
        }

但是,完整的 API 响应应如下所示:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"mPrpS7Nrk6Ggi_P7VJ8-KsEOiIw/el3Y0P65UwM366CJD3POX-W4y0c\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {

   "kind": "youtube#video",
   "etag": "\"mPrpS7Nrk6Ggi_P7VJ8-KsEOiIw/Rn64PVwC0Uhr4xp41vDOqygjJ9c\"",
   "id": "VIDEO_ID",
   "contentDetails": {
    "duration": "PT10M6S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": false,
    "contentRating": {
     "ytRating": "ytAgeRestricted"
    }
   }
  }
 ]
}

我特别感兴趣的是 ytRating 以查看视频是否受到限制,但我无法在响应中找到它得到.

有人可以帮我吗?

作为@CBroe , your call to the Videos.list API endpoint is missing a required contentDetails part for to be able to obtain the value of the property of your interest contentDetails.contentRating.ytRating

将您的 URL 更改为:

$api_url = 'https://www.googleapis.com/youtube/v3/videos?part=status,contentDetails&id=' . $vID . '&key=' . $API_key.

另请注意,最好使用 fields 请求参数,以便仅从 API 请求实际使用的信息。如果只对 ytRating 感兴趣,那么请将您的 URL 如图所示:

$api_url = 'https://www.googleapis.com/youtube/v3/videos?part=contentDetails&fields=items/contentDetails/contentRating/ytRating&id=' . $vID . '&key=' . $API_key.