使用 API v3 从 PHP 中的 videoid 获取 youtube 标题
Get youtube title from videoid in PHP using API v3
这是获取视频信息的 url,您必须将 VIDEO-ID 和 API-KEY:
https://www.googleapis.com/youtube/v3/videos?part=snippet&id=VIDEO-ID-HERE&key=YOUR-API-KEY-HERE
如何从中获取标题并将其保存为 PHP 中的变量?
google developers 中有两个 PHP 示例。
此外,您可以在本页底部进行测试。输入 part
(片段)和 id
(select 一个 youtube id)字段。它将通过 GET 请求和 JSON 响应向您展示。
使用 PHP 客户端库,您可以通过以下方式获得与特定视频相关的信息:
<?php
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
$client = new Google_Client();
$client->setDeveloperKey('{YOUR-API-KEY}');
$youtube = new Google_Service_YouTube($client);
$videoResponse = $youtube->videos->listVideos('snippet', array(
'id' => '{YOUR-VIDEO-ID}'
));
$title = $videoResponse['items'][0]['snippet']['title'];
?>
<!doctype html>
<html>
<head>
<title>Video information</title>
</head>
<body>
Title: <?= $title ?>
</body>
</html>
另一个解决方案 API 请求
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.get(
"https://www.googleapis.com/youtube/v3/videos",{
part : 'snippet',
id : 'VIODE_ID',
key: 'API_KEY'},
function(data) {
$.each( data.items, function( i, item ) {
alert(item.snippet.title);
});
}
);
});
</script>
这是获取视频信息的 url,您必须将 VIDEO-ID 和 API-KEY:
https://www.googleapis.com/youtube/v3/videos?part=snippet&id=VIDEO-ID-HERE&key=YOUR-API-KEY-HERE
如何从中获取标题并将其保存为 PHP 中的变量?
google developers 中有两个 PHP 示例。
此外,您可以在本页底部进行测试。输入 part
(片段)和 id
(select 一个 youtube id)字段。它将通过 GET 请求和 JSON 响应向您展示。
使用 PHP 客户端库,您可以通过以下方式获得与特定视频相关的信息:
<?php
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
$client = new Google_Client();
$client->setDeveloperKey('{YOUR-API-KEY}');
$youtube = new Google_Service_YouTube($client);
$videoResponse = $youtube->videos->listVideos('snippet', array(
'id' => '{YOUR-VIDEO-ID}'
));
$title = $videoResponse['items'][0]['snippet']['title'];
?>
<!doctype html>
<html>
<head>
<title>Video information</title>
</head>
<body>
Title: <?= $title ?>
</body>
</html>
另一个解决方案 API 请求
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.get(
"https://www.googleapis.com/youtube/v3/videos",{
part : 'snippet',
id : 'VIODE_ID',
key: 'API_KEY'},
function(data) {
$.each( data.items, function( i, item ) {
alert(item.snippet.title);
});
}
);
});
</script>