【问题标题】:Setup youtube API v3 for PHP为 PHP 设置 youtube API v3
【发布时间】:2016-04-10 07:31:36
【问题描述】:

我想为 php 使用 YouTube API 版本 3。我从 GitHub 下载 php 的示例。

在谷歌控制台中的 YouTube API 上,替换示例文件中的 DEVLOPER_KEY。然后我看到在每个文件中都有两个文件包括Google/Client.phpGoogle/Service/YouTube.php

这两个文件不在我下载的示例文件夹中,所以我在 google 上搜索并下载了 php 的 google 客户端库。然后我运行示例文件并显示错误。

致命错误:在第 32 行的 D:\wamp\www\api\php\Google\Service\YouTube.php 中找不到类 Google_Service

什么问题我不能正确下载所有东西我错过了任何东西?请指导我。谢谢。

【问题讨论】:

  • 只需在您的项目中包含autoload.php 就可以了。
  • 您需要include()require() client.php 和 youtube.php 文件。
  • @brian 我已经包含了 client.php 和 youtube.php
  • @jan 我包括autoload.php 它给出错误Fatal error: Uncaught exception 'Exception' with message 'This library must be installed via composer or by downloading the full package. See the instructions at https://github.com/google/google-api-php-client#installation.' in D:\wamp\www\api\php\Google\autoload.php on line 14

标签: php google-api-php-client youtube-data-api


【解决方案1】:

您将在 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>";
        }
}
?>

【讨论】:

    猜你喜欢
    • 2014-08-25
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2013-12-02
    • 2013-11-09
    • 1970-01-01
    相关资源
    最近更新 更多