使用 YouTube api v3 playlistItems 获取数组中的 videoId 列表并构建另一个视频查询以获取统计信息 viewCount
Getting the list of videoId in array using YouTube api v3 playlistItems and build another videos query to get statistics viewCount
我需要从播放列表中嵌入 youtube 视频并显示观看次数,播放列表项的 YouTube API v3 没有观看次数,所以我需要使用 API 进行另一个查询v3 用于提供统计信息 viewCount 但需要在查询中添加以逗号分隔的 YouTube 视频 ID 列表的视频
https://www.googleapis.com/youtube/v3/videos?part=statistics&id='.$listofvideoIDfromPlaylistItems.'&key={YOUR_API_KEY}
这就是我为 playlistItems
public function loadPlaylist($maxVideos){
if($this->_key && $this->_playlist) {
$this->_url = filter_var(
$this->_apiUrl.'playlistItems?part=id,snippet,contentDetails,status&maxResults='.$maxVideos.'&playlistId='.$this->_playlist.'&key='.$this->_key,
FILTER_SANITIZE_URL
);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, $this->_url);
curl_setopt($curlObj, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlObj, CURLOPT_HTTPGET, true);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json','Accept: application/json'));
curl_setopt($curlObj,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curlObj);
curl_close($curlObj);
$json = json_decode($response, true);
print("<pre>".print_r($json['items'],true)."</pre>");
}
return FALSE;
}
print_r
给了我
Array
(
[0] => Array
(
[kind] => youtube#playlistItem
[etag] => "xxx"
[id] => xxx
[snippet] => Array
(
[publishedAt] => xxx
[channelId] => xxx
[title] => xxx
[description] => xxx
[thumbnails] => Array
(
[default] => Array
(
[url] => xxx
[width] => 120
[height] => 90
)
[medium] => Array
(
[url] => xxx
[width] => 320
[height] => 180
)
[high] => Array
(
[url] => xxx
[width] => 480
[height] => 360
)
[standard] => Array
(
[url] => xxx
[width] => 640
[height] => 480
)
[maxres] => Array
(
[url] => xxx
[width] => 1280
[height] => 720
)
)
[channelTitle] => xxx
[playlistId] => xxx
[position] => 0
[resourceId] => Array
(
[kind] => youtube#video
[videoId] => videoID
)
)
[contentDetails] => Array
(
[videoId] => videoID
)
[status] => Array
(
[privacyStatus] => public
)
)
[1] => Array
(
[kind] => youtube#playlistItem
[etag] => "xxx"
[id] => xxx
[snippet] => Array
(
[publishedAt] => xxx
[channelId] => xxx
[title] => xxx
[description] => xxx
[thumbnails] => Array
(
[default] => Array
(
[url] => xxx
[width] => 120
[height] => 90
)
[medium] => Array
(
[url] => xxx
[width] => 320
[height] => 180
)
[high] => Array
(
[url] => xxx
[width] => 480
[height] => 360
)
[standard] => Array
(
[url] => xxx
[width] => 640
[height] => 480
)
[maxres] => Array
(
[url] => xxx
[width] => 1280
[height] => 720
)
)
[channelTitle] => xxx
[playlistId] => xxx
[position] => 0
[resourceId] => Array
(
[kind] => youtube#video
[videoId] => videoID
)
)
[contentDetails] => Array
(
[videoId] => videoID
)
[status] => Array
(
[privacyStatus] => public
)
)
)
如何创建以逗号分隔的视频 ID?
经过大量阅读:)
这就是我的结局
public static function getVideos($params) {
$key = $params->get('apiKey');
$list = $params->get('playlistID');
$maxVideos = $params->get('maxVideos');
if($key && $list){
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json','Accept: application/json'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$apiPlaylist = get_data("https://www.googleapis.com/youtube/v3/playlistItems?part=id,snippet,contentDetails,status&maxResults=".$maxVideos."&playlistId=".$list."&key=".$key."");
$video = json_decode($apiPlaylist);
$video2 = json_decode($apiPlaylist);
//To display video statistics viewCount
foreach ($video2->items as $statsitem) {
$videoids = $statsitem->contentDetails->videoId. ",";
$apiVideostats = get_data("https://www.googleapis.com/youtube/v3/videos?part=id,statistics&id=".$videoids."&key=".$key."");
$stats = json_decode($apiVideostats);
var_dump($stats->items);
}
$videolist = array();
//To display embed code, title and description
foreach ($video->items as $videoitem) {
$videolist[] = $videoitem;
}
return $videolist;
}
return false;
}
$stats->items var_dump returns
array (size=1)
0 =>
object(stdClass)[686]
public 'kind' => string 'youtube#video' (length=13)
public 'etag' => string '"xxx"' (length=57)
public 'id' => string 'xxx' (length=11)
public 'statistics' =>
object(stdClass)[687]
public 'viewCount' => string '465' (length=3)
public 'likeCount' => string '1' (length=1)
public 'dislikeCount' => string '0' (length=1)
public 'favoriteCount' => string '0' (length=1)
public 'commentCount' => string '0' (length=1)
array (size=1)
0 =>
object(stdClass)[690]
public 'kind' => string 'youtube#video' (length=13)
public 'etag' => string '"xxx"' (length=57)
public 'id' => string 'xxx' (length=11)
public 'statistics' =>
object(stdClass)[691]
public 'viewCount' => string '439' (length=3)
public 'likeCount' => string '2' (length=1)
public 'dislikeCount' => string '0' (length=1)
public 'favoriteCount' => string '0' (length=1)
public 'commentCount' => string '1' (length=1)
如何组合 2 个 foreach 循环或者我这样做是否正确?
最后,设法让它工作,不确定这是否是正确的方法,但我可以显示嵌入视频和视频观看次数。必须执行 2 个独立的功能。
public static function getVideos($params) {
$key = $params->get('apiKey');
$list = $params->get('playlistID');
$maxVideos = $params->get('maxVideos');
if($key && $list){
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json','Accept: application/json'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$apiPlaylist = get_data("https://www.googleapis.com/youtube/v3/playlistItems?part=id,snippet,contentDetails,status&maxResults=".$maxVideos."&playlistId=".$list."&key=".$key."");
$video = json_decode($apiPlaylist);
$videolist = array();
//To display embed code, title and description
foreach ($video->items as $videoitem) {
$videolist[] = $videoitem;
}
return $videolist;
}
return false;
}
public static function getStats($params) {
$key = $params->get('apiKey');
$list = $params->get('playlistID');
$maxVideos = $params->get('maxVideos');
if($key && $list){
function get_data2($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json','Accept: application/json'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$apiPlaylist = get_data2("https://www.googleapis.com/youtube/v3/playlistItems?part=id,snippet,contentDetails,status&maxResults=".$maxVideos."&playlistId=".$list."&key=".$key."");
$video2 = json_decode($apiPlaylist);
$videoids = '';
//To get all videoId from playlist
foreach ($video2->items as $statsitem) {
$videoids .= $statsitem->contentDetails->videoId. ",";
}
$apiVideostats = get_data2("https://www.googleapis.com/youtube/v3/videos?part=id,statistics&id=".$videoids."&key=".$key."");
$stats = json_decode($apiVideostats);
$statslist = array();
//To display video statistics viewCount
foreach ($stats->items as $statitem) {
$statslist[] = $statitem;
}
return $statslist;
}
return false;
}
在查看页面中,我只需要检查videoid 是否等于统计id。
我需要从播放列表中嵌入 youtube 视频并显示观看次数,播放列表项的 YouTube API v3 没有观看次数,所以我需要使用 API 进行另一个查询v3 用于提供统计信息 viewCount 但需要在查询中添加以逗号分隔的 YouTube 视频 ID 列表的视频
https://www.googleapis.com/youtube/v3/videos?part=statistics&id='.$listofvideoIDfromPlaylistItems.'&key={YOUR_API_KEY}
这就是我为 playlistItems
public function loadPlaylist($maxVideos){
if($this->_key && $this->_playlist) {
$this->_url = filter_var(
$this->_apiUrl.'playlistItems?part=id,snippet,contentDetails,status&maxResults='.$maxVideos.'&playlistId='.$this->_playlist.'&key='.$this->_key,
FILTER_SANITIZE_URL
);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, $this->_url);
curl_setopt($curlObj, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlObj, CURLOPT_HTTPGET, true);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json','Accept: application/json'));
curl_setopt($curlObj,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curlObj);
curl_close($curlObj);
$json = json_decode($response, true);
print("<pre>".print_r($json['items'],true)."</pre>");
}
return FALSE;
}
print_r
给了我
Array
(
[0] => Array
(
[kind] => youtube#playlistItem
[etag] => "xxx"
[id] => xxx
[snippet] => Array
(
[publishedAt] => xxx
[channelId] => xxx
[title] => xxx
[description] => xxx
[thumbnails] => Array
(
[default] => Array
(
[url] => xxx
[width] => 120
[height] => 90
)
[medium] => Array
(
[url] => xxx
[width] => 320
[height] => 180
)
[high] => Array
(
[url] => xxx
[width] => 480
[height] => 360
)
[standard] => Array
(
[url] => xxx
[width] => 640
[height] => 480
)
[maxres] => Array
(
[url] => xxx
[width] => 1280
[height] => 720
)
)
[channelTitle] => xxx
[playlistId] => xxx
[position] => 0
[resourceId] => Array
(
[kind] => youtube#video
[videoId] => videoID
)
)
[contentDetails] => Array
(
[videoId] => videoID
)
[status] => Array
(
[privacyStatus] => public
)
)
[1] => Array
(
[kind] => youtube#playlistItem
[etag] => "xxx"
[id] => xxx
[snippet] => Array
(
[publishedAt] => xxx
[channelId] => xxx
[title] => xxx
[description] => xxx
[thumbnails] => Array
(
[default] => Array
(
[url] => xxx
[width] => 120
[height] => 90
)
[medium] => Array
(
[url] => xxx
[width] => 320
[height] => 180
)
[high] => Array
(
[url] => xxx
[width] => 480
[height] => 360
)
[standard] => Array
(
[url] => xxx
[width] => 640
[height] => 480
)
[maxres] => Array
(
[url] => xxx
[width] => 1280
[height] => 720
)
)
[channelTitle] => xxx
[playlistId] => xxx
[position] => 0
[resourceId] => Array
(
[kind] => youtube#video
[videoId] => videoID
)
)
[contentDetails] => Array
(
[videoId] => videoID
)
[status] => Array
(
[privacyStatus] => public
)
)
)
如何创建以逗号分隔的视频 ID?
经过大量阅读:)
这就是我的结局
public static function getVideos($params) {
$key = $params->get('apiKey');
$list = $params->get('playlistID');
$maxVideos = $params->get('maxVideos');
if($key && $list){
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json','Accept: application/json'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$apiPlaylist = get_data("https://www.googleapis.com/youtube/v3/playlistItems?part=id,snippet,contentDetails,status&maxResults=".$maxVideos."&playlistId=".$list."&key=".$key."");
$video = json_decode($apiPlaylist);
$video2 = json_decode($apiPlaylist);
//To display video statistics viewCount
foreach ($video2->items as $statsitem) {
$videoids = $statsitem->contentDetails->videoId. ",";
$apiVideostats = get_data("https://www.googleapis.com/youtube/v3/videos?part=id,statistics&id=".$videoids."&key=".$key."");
$stats = json_decode($apiVideostats);
var_dump($stats->items);
}
$videolist = array();
//To display embed code, title and description
foreach ($video->items as $videoitem) {
$videolist[] = $videoitem;
}
return $videolist;
}
return false;
}
$stats->items var_dump returns
array (size=1)
0 =>
object(stdClass)[686]
public 'kind' => string 'youtube#video' (length=13)
public 'etag' => string '"xxx"' (length=57)
public 'id' => string 'xxx' (length=11)
public 'statistics' =>
object(stdClass)[687]
public 'viewCount' => string '465' (length=3)
public 'likeCount' => string '1' (length=1)
public 'dislikeCount' => string '0' (length=1)
public 'favoriteCount' => string '0' (length=1)
public 'commentCount' => string '0' (length=1)
array (size=1)
0 =>
object(stdClass)[690]
public 'kind' => string 'youtube#video' (length=13)
public 'etag' => string '"xxx"' (length=57)
public 'id' => string 'xxx' (length=11)
public 'statistics' =>
object(stdClass)[691]
public 'viewCount' => string '439' (length=3)
public 'likeCount' => string '2' (length=1)
public 'dislikeCount' => string '0' (length=1)
public 'favoriteCount' => string '0' (length=1)
public 'commentCount' => string '1' (length=1)
如何组合 2 个 foreach 循环或者我这样做是否正确?
最后,设法让它工作,不确定这是否是正确的方法,但我可以显示嵌入视频和视频观看次数。必须执行 2 个独立的功能。
public static function getVideos($params) {
$key = $params->get('apiKey');
$list = $params->get('playlistID');
$maxVideos = $params->get('maxVideos');
if($key && $list){
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json','Accept: application/json'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$apiPlaylist = get_data("https://www.googleapis.com/youtube/v3/playlistItems?part=id,snippet,contentDetails,status&maxResults=".$maxVideos."&playlistId=".$list."&key=".$key."");
$video = json_decode($apiPlaylist);
$videolist = array();
//To display embed code, title and description
foreach ($video->items as $videoitem) {
$videolist[] = $videoitem;
}
return $videolist;
}
return false;
}
public static function getStats($params) {
$key = $params->get('apiKey');
$list = $params->get('playlistID');
$maxVideos = $params->get('maxVideos');
if($key && $list){
function get_data2($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json','Accept: application/json'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$apiPlaylist = get_data2("https://www.googleapis.com/youtube/v3/playlistItems?part=id,snippet,contentDetails,status&maxResults=".$maxVideos."&playlistId=".$list."&key=".$key."");
$video2 = json_decode($apiPlaylist);
$videoids = '';
//To get all videoId from playlist
foreach ($video2->items as $statsitem) {
$videoids .= $statsitem->contentDetails->videoId. ",";
}
$apiVideostats = get_data2("https://www.googleapis.com/youtube/v3/videos?part=id,statistics&id=".$videoids."&key=".$key."");
$stats = json_decode($apiVideostats);
$statslist = array();
//To display video statistics viewCount
foreach ($stats->items as $statitem) {
$statslist[] = $statitem;
}
return $statslist;
}
return false;
}
在查看页面中,我只需要检查videoid 是否等于统计id。