igbas90/youtube-data-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

igbas90/youtube-data-api

Composer 安装命令:

composer require igbas90/youtube-data-api

包简介

Youtube Data API v3

README 文档

README

Support APIs

Authorization

!!! This library not supported OAuth2, only GOOGLE CONSOLE API KEY

Installation

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

composer require igbas90/youtube-data-api

Usage

Instance

Using a single shell. Services are created when they are accessed

use Igbas90\YoutubeDataApi\YoutubeDataApi;
$dataApiClient = new YoutubeDataApi();
$commentsThreadClient = $dataApiClient->commentsThreadList;

Create only service

use Igbas90\YoutubeDataApi\Services\CommentsThreadList;
$commentsThreadClient = new CommentsThreadList();

Set params

You can set parameters separately for each service

/** @var $dataApiClient Igbas90\YoutubeDataApi\YoutubeDataApi */
$dataApiClient->commentsThreadList->setApiKey('you google console api key');

$dataApiClient->subscriptionsList->setVideoId('9L9UQANH5oI');

Bulk settings

/** @var $dataApiClient Igbas90\YoutubeDataApi\YoutubeDataApi */
$dataApiClient->commentsThreadList->setParams([
    'apiKey' => "you google console api key",
    'params' => [
        'part' => ['part1', 'part2', ...],
        'videoId' => 'string or array',
        ...
    ]
]);

Parameters such as apiKey, proxy, responseFormatter can be set to services when creating in the shell for this, set these parameters in the shell. After creating the services, you can change these parameters only through the methods of the service itself

use Igbas90\YoutubeDataApi\YoutubeDataApi;

$dataApiClient = new YoutubeDataApi([
    'apiKey' => 'your google console api key',
    'proxy' => 'http://username:password@ip:port',
    'responseFormatter' => new ResponseFormatter()
]);

Usage proxy

To use a proxy server set it to the service

use Igbas90\YoutubeDataApi\YoutubeDataApi;
$dataApiClient = new YoutubeDataApi();

//set proxy
$dataApiClient->commentsThreadList->setProxy('http://username:password@ip:port');

//reset proxy
$dataApiClient->commentsThreadList->resetProxy();

Get result

/** @var $client Igbas90\YoutubeDataApi\Services\CommentsThreadList */
$response = $client->request();

Pagination

For pagination you can manually set pageToken

use Igbas90\YoutubeDataApi\YoutubeDataApi;

$client = (new YoutubeDataApi())->commentsThreadList->setParams([...]);

$response = $client->setPageToken('pageToken')->request();

The package has built-in support for paging data. An ITERATOR is used for this.

!!!Attention, the client pageToken will be used as the start for the iterator

/** @var $client \Igbas90\YoutubeDataApi\Services\CommentsThreadList*/ 
$comments = [];
foreach($client->getIterator() as $response) {
    $body = json_decode($response->getBody(), true);
    $comment = array_merge($comments, $body['items']);
}

By default, the iterator works with the original service, which is not very convenient for using multiple iterators. To exclude the influence of the iterator on the service, you can set the iterator to work with the service clone. To do this, call the getIterator() method, with the parameter true.

/** @var $client \Igbas90\YoutubeDataApi\Services\CommentsThreadList*/ 
$iterator1 = $client->getIterator(true);
$iterator2 = $client->setVideoId('videoId')->getIterator(true);
$iterator3 = $client->setParams([
    'videoId' => '9L9UQANH5oI',
    'apiKey' => 'google console api key'
])->getIterator(true);

Response format

By default, Psr\Http\Message\ResponseInterface is returned If you need to return a different format, then you can specify an object that implements the Igbas90\YoutubeDataApi\Classes\ResponseFormatter interface, which will be used to convert the returned result.

use Igbas90\YoutubeDataApi\Classes\ResponseFormatter;
use Psr\Http\Message\ResponseInterface;

/*
 * Create custom response converter
 */
class BodyJsonDecodeFormatter implements ResponseFormatter
{
    public function format(ResponseInterface $response)
    {
        $content = $response->getBody();
        return json_decode($content, true);
    }
}

/** @var $client Igbas90\YoutubeDataApi\Services\CommentsThreadList */
$client->setResponseFormatter(new BodyJsonDecodeFormatter());

//@var $response array
$response = $client->request();

TEST

forward running the tests you need to set the environment variables in define.php, for this just copy define-example.php into define.php

cp define-example.php define.php

replace define.php values ​​with your values.

define("API_KEY", "google console key");
define("PROXY", "http://username:password@ip:port");
define("CHANNEL_ID", "UCf-b4GSsV5HJysCi6A0Bm6g");
define("PLAYLIST_ID", "UU_x5XG1OV2P6uZZ5FSM9Ttw");
define("VIDEO_ID", "oxQ7wfiS4GY");

Documentations

统计信息

  • 总下载量: 33
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固