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

如何在PHP中从YouTube数据API访问统计信息,php,json,api,youtube,statistics,Php,Json,Api,Youtube,Statistics,我正在运行一个php脚本,它使用channelId从YouTube频道检索视频 $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='.$cha

我正在运行一个php脚本,它使用channelId从YouTube频道检索视频

$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文件并不包含所有信息。我认为这是url的问题。它只是创建一个包含一些信息的json文件。我尝试过将url和不同路径切换到duration

$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请求,以便在
部分
参数中添加
统计信息

此外,您正在使用“搜索”选项,并且根据its,它不会显示
统计数据
值(这是您所需要的)

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

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

https://www.googleapis.com/youtube/v3/videos?id=YxjEmz6KWdU&maxResults=2&part=snippet%2Cstatistics&key=<YOUR_API_KEY>
https://www.googleapis.com/youtube/v3/videos?id=YxjEmz6KWdU&maxResults=2&part=snippet%2Cstatistics&key=<YOUR_API_KEY>
{
  "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"
    }
  }]
}