我正在尝试显示来自 YouTube 的多个视频,并且我正在使用 YouTube API 提取信息。
除了我想对 viewCount 视频的观看进行排序之外,我一切正常。
我可以简单地将其转换为数据库系统,但随后我必须运行一个 cron 作业(这不是问题,只是试图避免额外需要运行比需要更多的代码。)
$video = array(
"id1",
"id2",
"id3",
);
foreach ($video as $key) {
$api = "http://gdata.youtube.com/feeds/api/videos/$key?v=2&alt=json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$yt = json_decode(curl_exec($ch));
curl_close($ch);
$title = $yt->{'entry'}->{'title'}->{'$t'};
$viewCount = $yt->{'entry'}->{'yt$statistics'}->{'viewCount'};
$length = $yt->{'entry'}->{'media$group'}->{'yt$duration'}->{'seconds'};
$numDislikes = $yt->{'entry'}->{'yt$rating'}->{'numDislikes'};
$numLikes = $yt->{'entry'}->{'yt$rating'}->{'numLikes'};
$average = $yt->{'entry'}->{'gd$rating'}->{'average'};
$minutes = sprintf("%0.2f", $length / 60);
$seconds = str_replace(".", ":", $minutes);
//What I've tried, doesn't work. With and without if statement and max (Shouldn't need both, but I need the if statement to display one format of the number is higher) but too avail it hasn't successfully worked.
$views = 0;
foreach ($viewCount as $maxViews) {
$views = max($views,$maxViews);
if ($maxViews > $views) {
echo $views;
} else {
echo $viewCount;
}
}
}
这应该很简单,显然完全可以查看它,认为这是因为数组值不是数字,数字是从 $viewCount 变量的每个 foreach 循环中提取的。
希望有人可以帮助我并正确解决此问题。
注意:我自己编写了代码,但由于它是 YouTube API,您可以自己使用代码。JSON 解析有时会很痛苦。特别是使用 YouTube 的 API。
问候