我正在运行一个PHP脚本,该脚本使用channelId从YouTube channel 检索视频。

$API_key    = 'MY_KEY';
$channelID  = 'MY_CHANNEL_ID';
$maxResults = 2;

$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));

然后,我将其插入网页中:
<iframe width="280" height="150" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
                        <h2>'. $item->snippet->title .'</h2>
                        <h2>'. $item->snippet->publishedAt .'</h2>
                        <h2>'. $item->statistics->duration .'</h2>

我似乎无法访问json文件的统计信息部分。当我打印json时,它看起来像这样:
stdClass Object
(
    [kind] => youtube#searchListResponse
    [etag] => "XI7nbFXulYBIpL0ayR_gDh3eu1k/n6hG3LC-2onr-
    5PwKJtRxnC1Kjc"
    [nextPageToken] => CAEQAA
    [regionCode] => US
    [pageInfo] => stdClass Object
    (
        [totalResults] => 1094
        [resultsPerPage] => 1
    )

    [items] => Array
    (
        [0] => stdClass Object
            (
                [kind] => youtube#searchResult
                [etag] =>
      "XI7nbFXulYBIpL0ayR_gDh3eu1k/lKQ8CNf00_OxqchddzGEYtbidqk"
                [id] => stdClass Object
                    (
                        [kind] => youtube#video
                        [videoId] => YxjEmz6KWdU
                    )

                [snippet] => stdClass Object
                    (
                        [publishedAt] => 2018-04-06T21:10:48.000Z
                        [channelId] => UCV4xOVpbcV8SdueDCOxLXtQ
                        [title] => SE ACABO :'V
                        [description] => https://www.twitch.tv/fernanfloo.
                        [thumbnails] => stdClass Object
                            (
                                [default] => stdClass Object
                                    (
                                        [url] => https://i.ytimg.com/vi/YxjEmz6KWdU/default.jpg
                                        [width] => 120
                                        [height] => 90
                                    )

                                [medium] => stdClass Object
                                    (
                                        [url] => https://i.ytimg.com/vi/YxjEmz6KWdU/mqdefault.jpg
                                        [width] => 320
                                        [height] => 180
                                    )

                                [high] => stdClass Object
                                    (
                                        [url] => https://i.ytimg.com/vi/YxjEmz6KWdU/hqdefault.jpg
                                        [width] => 480
                                        [height] => 360
                                    )

                            )

                        [channelTitle] => Fernanfloo
                        [liveBroadcastContent] => none
                    )

            )

    )

)

因此,显然json文件并不包含所有信息。我相信这是网址的问题。它只是创建带有一些信息的json文件。我尝试将url和不同的路径切换为持续时间。
$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));

我是YouTube Data API的新手,但我的知识参差不齐。有人可以解释我做错了吗?

最佳答案

您需要修改API请求,以便在statistics参数中添加part

另外,您使用的是“搜索”选项-根据其documentation,它不会显示statistics值(这是您所需要的)。

因此,您必须使用“videos”选项。

这是您可以使用的修改后的API请求:

https://www.googleapis.com/youtube/v3/videos?id=YxjEmz6KWdU&maxResults=2&part=snippet%2Cstatistics&key=<YOUR_API_KEY>

以下信息来自YouTube Data API - official documentation:
  • id:您要检索statistics的YouTube视频的ID。
  • 密钥:您的API密钥。
  • 部分:参数part指定一个API响应将包括的一个或多个搜索资源属性的逗号分隔列表。对于这种情况,我添加了snippet statistics

  • 该视频的统计信息将显示在API响应的statistics部分中。

    这是使用try-it functionality并指定videoId YxjEmz6KWdU得出的结果:
    {
      "kind": "youtube#videoListResponse",
      "etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/4N2LykLhXBiFo79x71IYUazfgdk\"",
      "pageInfo": {
        "totalResults": 1,
        "resultsPerPage": 1
      },
      "items": [{
        "kind": "youtube#video",
        "etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/Pvuq35nnqnUa3-pETVOKpdemDVU\"",
        "id": "YxjEmz6KWdU",
        "snippet": {
          "publishedAt": "2018-04-06T21:10:48.000Z",
          "channelId": "UCV4xOVpbcV8SdueDCOxLXtQ",
          "title": "SE ACABO :'V",
          "description": "→ https://www.twitch.tv/fernanfloo",
          "thumbnails": {
            "default": {
              "url": "https://i.ytimg.com/vi/YxjEmz6KWdU/default.jpg",
              "width": 120,
              "height": 90
            },
            "medium": {
              "url": "https://i.ytimg.com/vi/YxjEmz6KWdU/mqdefault.jpg",
              "width": 320,
              "height": 180
            },
            "high": {
              "url": "https://i.ytimg.com/vi/YxjEmz6KWdU/hqdefault.jpg",
              "width": 480,
              "height": 360
            },
            "standard": {
              "url": "https://i.ytimg.com/vi/YxjEmz6KWdU/sddefault.jpg",
              "width": 640,
              "height": 480
            },
            "maxres": {
              "url": "https://i.ytimg.com/vi/YxjEmz6KWdU/maxresdefault.jpg",
              "width": 1280,
              "height": 720
            }
          },
          "channelTitle": "Fernanfloo",
          "tags": [
            "fernanfloo",
            "fernan",
            "fernan el crack",
            "twitch",
            "fernanfloo twitch",
            "en vivo",
            "en directo",
            "regreso",
            "el regreso de fernanfloo",
            "vlog",
            "blog",
            "2018",
            "pc",
            "fornite",
            "fernanfloo con barba",
            "fernanfllo viejo",
            "lol"
          ],
          "categoryId": "20",
          "liveBroadcastContent": "none",
          "localized": {
            "title": "SE ACABO :'V",
            "description": "→ https://www.twitch.tv/fernanfloo"
          }
        },
        "statistics": {
          "viewCount": "21926458",
          "likeCount": "1371385",
          "dislikeCount": "135852",
          "favoriteCount": "0",
          "commentCount": "303938"
        }
      }]
    }
    

    07-24 09:52
    查看更多