1,首先要有个谷歌账号,有个谷歌api key。
https://console.cloud.google.com/apis/dashboard
2,通过谷歌账号将youtube api v3启用,创建API key。
3,可以去前往API使用文档页面看看了。全是代码看吧
https://developers.google.com/youtube/v3/docs/videos#snippet(获取单个视频信息返回值说明)
?https://developers.google.com/youtube/v3/code_samples/code_snippets(用例和代码片段)
https://developers.google.com/youtube/v3/getting-started(YouTube 数据 API 概述)
https://developers.google.com/youtube/iframe_api_reference#Requirements(iframe api)
4,上代码:
$key = "你的 api key";
$id = "FN1HJ4g0RNQ";
$url = 'https://youtube.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id='.$id.'&key='.$key;
该part 参数是检索或返回资源的任何 API 请求的必需参数。
$ch = curl_init($url);
?curl_setopt($ch, CURLOPT_URL, $url);
?curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
?curl_setopt($ch, CURLOPT_HTTPGET, true);
?$result = curl_exec($ch);
?$result = json_decode($result, true);
?curl_close($ch);
var_dump($result);
{
"kind": "youtube#videoListResponse",
"etag": "M8ooSEz5k-UopvAjKaScpwWCebM",
"items": [
{
"kind": "youtube#video",
"etag": "BPjwz-Q-ByoA9q-RXGMZMAGb4lc",
"id": "FN1HJ4g0RNQ",
"snippet": {
"publishedAt": "2022-04-22T09:31:05Z",
"channelId": "UC8dNjbSArx86GrulKgqfV8g",
"title": "详细信息",
"description": "详细信息说明",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/FN1HJ4g0RNQ/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/FN1HJ4g0RNQ/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/FN1HJ4g0RNQ/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "东北小伙儿",
"categoryId": "22",
"liveBroadcastContent": "none",
"localized": {
"title": "详细信息",
"description": "详细信息说明"
}
},
"contentDetails": {
"duration": "PT4S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": false,
"contentRating": {},
"projection": "rectangular"
},
"statistics": {
"viewCount": "21",
"likeCount": "1",
"favoriteCount": "0",
"commentCount": "0"
}
}
],
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
}
}
?齐活,根据自己需求改造吧。
|