我已经获得了PHP代码来获取我的YouTube视频中的观看次数:
$xdoc = new DomDocument;
$xdoc->Load('http://gdata.youtube.com/feeds/api/users/myUserName');
$ytstat = $xdoc->getElementsByTagName('statistics')->item(0);
$total_views = $ytstat->getAttribute(totalUploadViews);
现在如何获取上传的视频总数?谢谢你的帮助。
答案 0 :(得分:1)
您当然更愿意使用YouTube API v2的JSON格式:
<?php
$username = 'username';
$user = json_decode(file_get_contents(
"https://gdata.youtube.com/feeds/api/users/$username?v=2&alt=json"));
$uploads = json_decode(file_get_contents(
"https://gdata.youtube.com/feeds/api/users/$username/uploads?v=2&alt=jsonc&max-results=0"));
printf("Total uploads: %d\nTotal views: %d\n",
$uploads->data->totalItems,
$user->entry->{'yt$statistics'}->totalUploadViews);
更好的是,您可能希望使用新的YouTube API v3,它可以在一个请求中报告这两种信息。要使用新API,您必须在Google Cloud Console上获取API密钥,并进一步启用YouTube Data API v3。以下是YouTube API v3代码:
<?php
$username = 'username';
$api_key = 'your-api-key';
$channel = json_decode(file_get_contents(
"https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=$username&key=$api_key"));
printf("Total uploads: %d\nTotal views: %d\n",
$channel->items->statistics->videoCount,
$channel->items->statistics->viewCount);
更多信息: