YouTube

YouTube

Laravel 框架的 YouTube 数据 API 集成包

这个 PHP 封装包为 Laravel 框架提供了 YouTube 数据 API v3 的集成。它支持视频信息获取、内容搜索和播放列表管理等功能,适用于 PHP 7.0+ 和 Laravel 5.1+ 版本。该包使用简单,仅需 API 密钥即可运行,无需 OAuth 认证。此外,它还提供分页功能和验证规则,便于开发者在 Laravel 项目中快速实现 YouTube 相关功能。

YouTubeAPILaravelPHP开发Github开源项目

YouTube

YouTube Tests Donate

Laravel PHP Facade/Wrapper for the YouTube Data API v3 ( Non-OAuth )

Requirements

Looking for YouTube Package for either of these: PHP 5, Laravel 5.0, Laravel 4? Visit the php5-branch

Installation

Run in console below command to download package to your project:

composer require alaouy/youtube

Configuration

In /config/app.php add YoutubeServiceProvider (Laravel < 5.5):

Alaouy\Youtube\YoutubeServiceProvider::class,

Do not forget to add also YouTube facade there (Laravel < 5.5):

'Youtube' => Alaouy\Youtube\Facades\Youtube::class,

Publish config settings:

$ php artisan vendor:publish --provider="Alaouy\Youtube\YoutubeServiceProvider"

Set your YouTube API key in the file:

/config/youtube.php

Or in the .env file

YOUTUBE_API_KEY = KEY

Or you can set the key programmatically at run time :

Youtube::setApiKey('KEY');

Usage

// use Alaouy\Youtube\Facades\Youtube; // Return an STD PHP object $video = Youtube::getVideoInfo('rie-hPVJ7Sw'); // Get multiple videos info from an array $videoList = Youtube::getVideoInfo(['rie-hPVJ7Sw','iKHTawgyKWQ']); // Get localized video info $video = Youtube::getLocalizedVideoInfo('vjF9GgrY9c0', 'pl'); // Get comment threads by videoId $commentThreads = Youtube::getCommentThreadsByVideoId('zwiUB_Lh3iA'); // Get popular videos in a country, return an array of PHP objects $videoList = Youtube::getPopularVideos('us'); // Search playlists, channels and videos. return an array of PHP objects $results = Youtube::search('Android'); // Only search videos, return an array of PHP objects $videoList = Youtube::searchVideos('Android'); // Search only videos in a given channel, return an array of PHP objects $videoList = Youtube::searchChannelVideos('keyword', 'UCk1SpWNzOs4MYmr0uICEntg', 40); // List videos in a given channel, return an array of PHP objects $videoList = Youtube::listChannelVideos('UCk1SpWNzOs4MYmr0uICEntg', 40); $results = Youtube::searchAdvanced([ /* params */ ]); // Get channel data by channel handle (like https://www.youtube.com/@google), return an STD PHP object $channel = Youtube::getChannelByHandle('google'); // Get channel data by channel name, return an STD PHP object $channel = Youtube::getChannelByName('xdadevelopers'); // Get channel data by channel ID, return an STD PHP object $channel = Youtube::getChannelById('UCk1SpWNzOs4MYmr0uICEntg'); // Get playlist by ID, return an STD PHP object $playlist = Youtube::getPlaylistById('PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs'); // Get playlists by multiple ID's, return an array of STD PHP objects $playlists = Youtube::getPlaylistById(['PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs', 'PL590L5WQmH8cUsRyHkk1cPGxW0j5kmhm0']); // Get playlist by channel ID, return an array of PHP objects $playlists = Youtube::getPlaylistsByChannelId('UCk1SpWNzOs4MYmr0uICEntg'); // Get items in a playlist by playlist ID, return an array of PHP objects $playlistItems = Youtube::getPlaylistItemsByPlaylistId('PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs'); // Get channel activities by channel ID, return an array of PHP objects $activities = Youtube::getActivitiesByChannelId('UCk1SpWNzOs4MYmr0uICEntg'); // Retrieve video ID from original YouTube URL $videoId = Youtube::parseVidFromURL('https://www.youtube.com/watch?v=moSFlvxnbgk'); // result: moSFlvxnbgk

Validation Rules

// use Alaouy\Youtube\Rules\ValidYoutubeVideo; // Validate a YouTube Video URL [ 'youtube_video_url' => ['bail', 'required', new ValidYoutubeVideo] ];

You can use the bail rule in conjunction with this in order to prevent unnecessary queries.

Basic Search Pagination

// Set default parameters $params = [ 'q' => 'Android', 'type' => 'video', 'part' => 'id, snippet', 'maxResults' => 50 ]; // Make intial call. with second argument to reveal page info such as page tokens $search = Youtube::searchAdvanced($params, true); // Check if we have a pageToken if (isset($search['info']['nextPageToken'])) { $params['pageToken'] = $search['info']['nextPageToken']; } // Make another call and repeat $search = Youtube::searchAdvanced($params, true); // Add results key with info parameter set print_r($search['results']); /* Alternative approach with new built-in paginateResults function */ // Same params as before $params = [ 'q' => 'Android', 'type' => 'video', 'part' => 'id, snippet', 'maxResults' => 50 ]; // An array to store page tokens so we can go back and forth $pageTokens = []; // Make inital search $search = Youtube::paginateResults($params, null); // Store token $pageTokens[] = $search['info']['nextPageToken']; // Go to next page in result $search = Youtube::paginateResults($params, $pageTokens[0]); // Store token $pageTokens[] = $search['info']['nextPageToken']; // Go to next page in result $search = Youtube::paginateResults($params, $pageTokens[1]); // Store token $pageTokens[] = $search['info']['nextPageToken']; // Go back a page $search = Youtube::paginateResults($params, $pageTokens[0]); // Add results key with info parameter set print_r($search['results']);

The pagination above is quite basic. Depending on what you are trying to achieve you may want to create a recursive function that traverses the results.

Manual Class Instantiation

// Directly call the YouTube constructor $youtube = new Youtube(config('YOUTUBE_API_KEY')); // By default, if the $_SERVER['HTTP_HOST'] header is set, // it will be used as the `Referer` header. To override // this setting, set 'use-http-host' to false during // object construction: $youtube = new Youtube(config('YOUTUBE_API_KEY'), ['use-http-host' => false]); // This setting can also be set after the object was created $youtube->useHttpHost(false);

Run Unit Test

If you have PHPUnit installed in your environment, run:

$ phpunit

If you don't have PHPUnit installed, you can run the following:

$ composer update $ ./vendor/bin/phpunit

Format of returned data

The returned JSON is decoded as PHP objects (not Array). Please read the "Reference" section of the Official API doc.

YouTube Data API v3

Donation

If you find this project to be of use to you please consider buying me a cup of tea :)

paypal

Credits

Built on code from Madcoda's php-youtube-api.

编辑推荐精选

博思AIPPT

博思AIPPT

AI一键生成PPT,就用博思AIPPT!

博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。

AI办公办公工具AI工具博思AIPPTAI生成PPT智能排版海量精品模板AI创作热门
潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

iTerms

iTerms

企业专属的AI法律顾问

iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。

SimilarWeb流量提升

SimilarWeb流量提升

稳定高效的流量提升解决方案,助力品牌曝光

稳定高效的流量提升解决方案,助力品牌曝光

Sora2视频免费生成

Sora2视频免费生成

最新版Sora2模型免费使用,一键生成无水印视频

最新版Sora2模型免费使用,一键生成无水印视频

Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
商汤小浣熊

商汤小浣熊

最强AI数据分析助手

小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。

imini AI

imini AI

像人一样思考的AI智能体

imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。

下拉加载更多