php - 如何在 PHP 中从 YouTube 数据 API 访问统计信息

标签 php json api youtube statistics

我正在运行一个 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 数据 API 的新手,我的知识参差不齐。有人可以解释我做错了什么吗?

最佳答案

您需要修改 API 请求以添加 statisticspart范围。

此外,您正在使用“搜索”选项 - 并且根据其 documentation ,它不显示 statistics值(value)(这是你需要的)。

因此,您必须使用“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 :
  • 编号:您要检索的 YouTube 视频的 ID statistics .
  • 关键字:您的 API key 。
  • 部分: part 参数指定 API 响应将包括的一个或多个搜索资源属性的逗号分隔列表。对于这种情况,我添加了 snippet statistics .

  • 该视频的统计数据将显示在statistics API 响应的部分。

    这是使用 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"
        }
      }]
    }
    

    关于php - 如何在 PHP 中从 YouTube 数据 API 访问统计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51485912/

    相关文章:

    php - Fat-Free-Framework - 防止表名插入映射器对象的虚拟字段

    php - 如何将一个值插入到不同的行

    json - 使用 App Engine 在 golang 中将 json 解析为来自 google api 请求的结构

    arrays - 在 T-SQL 中更新/添加属性到 json 数组

    C win32选项卡控件

    php - 使用层次结构管理用户权限

    php - 如何向 zfc-twig 1.2 注册 ZF2 Helpers?

    python - 从 Python 接收 C++ 中的字符串

    javascript - JS脚本中的执行顺序

    postgresql - 无法获取 Go http 返回错误; 'No data recieved'