为 PHP 设置 youtube API v3
Setup youtube API v3 for PHP
我想为 php 使用 YouTube API 版本 3。我从 GitHub 下载 php 的样本。
在 google 控制台中的 YouTube API 上,替换示例文件中的 DEVLOPER_KEY
。然后我在每个文件中看到有两个文件包括 Google/Client.php
和 Google/Service/YouTube.php
.
这两个文件不在我下载的示例文件夹中,所以我在 google 上搜索并下载 google 客户端库 php。然后我 运行 样本文件,它显示错误。
Fatal error: Class Google_Service
not found in D:\wamp\www\api\php\Google\Service\YouTube.php
on line 32
有什么问题我不能正确下载所有东西我错过了任何东西吗?请指导我。谢谢。
您将在 YouTube 上寻找简单的视频搜索 API,因此下面给出了最佳方法代码。
<?php
error_reporting(0);
$search = "Search Query"; // Search Query
$api = "YouTube API Key"; // YouTube Developer API Key
$link = "https://www.googleapis.com/youtube/v3/search?safeSearch=moderate&order=relevance&part=snippet&q=".urlencode($search). "&maxResults=10&key=". $api;
$video = file_get_contents($link);
$video = json_decode($video, true);
foreach ($video['items'] as $data){
$title = $data['snippet']['title'];
$description = $data['snippet']['title'];
$vid = $data['id']['videoId'];
$image = "https://i.ytimg.com/vi/$vid/default.jpg";
// Output Title/Description/Image URL If Video ID exist
if($vid){
echo "Title: $title<br />Description: $description<br />Video ID: $vid<br />Image URL: $image<hr>";
}
}
?>
我想为 php 使用 YouTube API 版本 3。我从 GitHub 下载 php 的样本。
在 google 控制台中的 YouTube API 上,替换示例文件中的 DEVLOPER_KEY
。然后我在每个文件中看到有两个文件包括 Google/Client.php
和 Google/Service/YouTube.php
.
这两个文件不在我下载的示例文件夹中,所以我在 google 上搜索并下载 google 客户端库 php。然后我 运行 样本文件,它显示错误。
Fatal error: Class
Google_Service
not found inD:\wamp\www\api\php\Google\Service\YouTube.php
on line 32
有什么问题我不能正确下载所有东西我错过了任何东西吗?请指导我。谢谢。
您将在 YouTube 上寻找简单的视频搜索 API,因此下面给出了最佳方法代码。
<?php
error_reporting(0);
$search = "Search Query"; // Search Query
$api = "YouTube API Key"; // YouTube Developer API Key
$link = "https://www.googleapis.com/youtube/v3/search?safeSearch=moderate&order=relevance&part=snippet&q=".urlencode($search). "&maxResults=10&key=". $api;
$video = file_get_contents($link);
$video = json_decode($video, true);
foreach ($video['items'] as $data){
$title = $data['snippet']['title'];
$description = $data['snippet']['title'];
$vid = $data['id']['videoId'];
$image = "https://i.ytimg.com/vi/$vid/default.jpg";
// Output Title/Description/Image URL If Video ID exist
if($vid){
echo "Title: $title<br />Description: $description<br />Video ID: $vid<br />Image URL: $image<hr>";
}
}
?>