【发布时间】:2018-12-31 07:49:19
【问题描述】:
我正在运行一个使用 channelId 从 YouTube 频道检索视频的 php 脚本。
$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 的新手,我的知识参差不齐。有人可以解释我做错了什么吗?
【问题讨论】:
标签: php json api youtube statistics