PHPackages                             sr1871/yii2-youtube-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. sr1871/yii2-youtube-api

ActiveYii2-extension[API Development](/categories/api)

sr1871/yii2-youtube-api
=======================

Component for use youtube api with Oauth in yii2

072821PHP

Since Aug 31Pushed 7y ago2 watchersCompare

[ Source](https://github.com/sr1871/yii2-youtube-api)[ Packagist](https://packagist.org/packages/sr1871/yii2-youtube-api)[ RSS](/packages/sr1871-yii2-youtube-api/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (1)

Youtube Api for yii2
====================

[](#youtube-api-for-yii2)

Youtube api is a yii2 extension to use youtube api as a yii2 component.

this extension access youtube api via oauth, saving the access token and refresh it when expire, so you have to give credentials just once

This extension also contains methods for partners, so you don't have to put your onBehalfContentOwner every single call, and control the youtube partner calls per minute to avoid overflow of limit call per minute

With this extension you can

- upload and update a videos
- upload a thumbnail for a video
- list of videos
- create and update playlists
- get ,add and remove elements for a playlist
- searches
- Put the player

Also can do partener actions like

- get channels
- monetize/unmonetize videos

Installation
------------

[](#installation)

The preferred way to install this extension is through composer.

`$ composer require sr1871/yii2-youtube-api`

Usage
-----

[](#usage)

Add it to your components

### add to your components

[](#add-to-your-components)

```
'components' => [
    ...
    'youtube' => [
        'class' => \sr1871\youtubeApi\components\YoutubeApi::className(),
        'clientId' => '{your Oauth Client Id, you can get it from google console}',
        'clientSecret' => '{your Oauth Client Secret, you can get it from google console}',
        'setAccessTokenFunction' => function($client){ file_put_contents('pathFile.txt'json_encode($client->getAccessToken());}, //anonymous function where save the accesToken
        'getAccessTokenFunction' => function(){ return file_get_contents('pathFile.txt');}, // an anonymous function where get the accessToken
        'scopes' => ['{scopes that you going to use}', '{as array}'],
    ],
    ...
]

```

`setAccessTokenFunction` and `getAccessTokenFunction` are so important, in one you gonna save your accessToken and another let the component take it.

In the example above, the access token was save it in txt, and in te function to take it return the content of that file.

It's important that you `setAccessTokenFunction` has one parameter (`$client` for example) and always save only `json_encode($client->getAccessToken)`.

### generate your access token

[](#generate-your-access-token)

An advantage of this component is that you only have to generate your access token once.

Create an action in any controller

```
public function actionValidation() {
    if(Yii::$app->request->get('code')){
        Yii::$app->youtube->validationPost(Yii::$app->urlManager->createAbsoluteUrl('/site/validation'));
    } else {
        Yii::$app->session->setFlash('success', 'The access token was generated');
        return $this->redirect('index');
    }
}

```

You can call your action as you want, when has a GET parameter called 'code', you must call `Yii::$app->youtube->validationPost('{url_to_this_action}')` , this method create and save the access token and redirect to the url passed as parameter.

To get this action you must do it through `Yii::$app->youtube->validationGet(Yii::$app->urlManager->createAbsoluteUrl({url_to_action}))`, you can use it for example in a `` tag

for example

`echo Html::a('Validar', Yii::$app->youtube->validationGet(Yii::$app->urlManager->createAbsoluteUrl('/site/validation')) )`

The access token'll be saved and you can use the component

### Example

[](#example)

`Yii::$app->youtube->setParts(['snippet', 'recordingDetails', 'id'])->listVideos(['id' => 'someId'])`

You can pass in `setParts()`, the parts that you want, if you don't want the default parts. For more information of every method and how do it, read the PhpDOC of component's methods

### Partner

[](#partner)

if you want to use the extension as a partner you must indicate your `onBehalfContentOwner` in your config

```
'youtube' => [
    'class' => \sr1871\youtubeApi\components\YoutubeApi::className(),
    'clientId' => '{your Oauth Client Id, you can get it from google console}',
    'clientSecret' => '{your Oauth Client Secret, you can get it from google console}',
    'setAccessTokenFunction' => function($client){ file_put_contents('pathFile.txt'json_encode($client->getAccessToken());}, //anonymous function where save the accesToken
    'getAccessTokenFunction' => function(){ return file_get_contents('pathFile.txt');}, // an anonymous function where get the accessToken
    'scopes' => ['{scopes that you going to use}', '{as array}'],
    'onBehalfContentOwner' => {your_content_owner},
    'youtubePartnerCallsPerSecond' => 2 //you can indicate how many calls per second can you do, default is 2
],

```

If you don't know your `onBehalfContentOwner` you can get it with `getOnBehalfOfContentOwner()` method

### Player

[](#player)

This extension include a player in widget format

```
\sr1871\youtubeApi\widgets\YoutubeIFrame::widget([
    'id' => 'SomeIdForDivAndJSObject'
    'iFrameOptions' => [
        'videoId' => 'someId'
        ...
    ],
    'iFrameEvents' => [
        'onReady' => 'function(event) {
            console.log('ready')
        }'
    ],
    'options' => [] //html div options
])

```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/924e61127639daa9bb02669e3a101a433d050ab107a025a50ff1cca4429eb32e?d=identicon)[sr1871](/maintainers/sr1871)

---

Top Contributors

[![sr1871](https://avatars.githubusercontent.com/u/24658476?v=4)](https://github.com/sr1871 "sr1871 (1 commits)")

### Embed Badge

![Health badge](/badges/sr1871-yii2-youtube-api/health.svg)

```
[![Health](https://phpackages.com/badges/sr1871-yii2-youtube-api/health.svg)](https://phpackages.com/packages/sr1871-yii2-youtube-api)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M187](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

263103.1M454](/packages/google-gax)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
