PHP code example of 0duddu / youtube-laravel-api

1. Go to this page and download the library: Download 0duddu/youtube-laravel-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

0duddu / youtube-laravel-api example snippets


ZeroDUDDU\YoutubeLaravelApi\YoutubeLaravelApiServiceProvider::class



namespace Your\App\NameSpace;

use  ZeroDUDDU\YoutubeLaravelApi\AuthenticateService;	



$authObject  = new AuthenticateService;

# Replace the identifier with a unqiue identifier for account or channel
$authUrl = $authObject->getLoginUrl('email','identifier'); 


$code = $request->get('code');
$identifier = $request->get('state');


$authObject  = new AuthenticateService;
$authResponse = $authObject->authChannelWithCode($code, true);

$authObject  = new AuthenticateService;
$authResponse = $authObject->authChannelWithCode($code, false);


namespace Your\App\NameSpace;

use  ZeroDUDDU\YoutubeLaravelApi\LiveStreamService;	

# data format creating live event
$data = array(
	"thumbnail_path" => "",				// Optional
	"event_start_date_time" => "",
	"event_end_date_time" => "",			// Optional
	"time_zone" => "",
	'privacy_status' => "",				// default: "private"
	"language_name" => "",				// default: "English"
	"tag_array" => ""				// Optional and should not be more than 500 characters
);

$ytEventObj = new LiveStreamService($authToken);
/**
 * The broadcast function returns array of details from YouTube.
 * Store this information & will be 

$ytEventObj = new LiveStreamService($authToken);
/**
* The updateBroadcast response give details of the youtube_event_id,server_url and server_key. 
* The server_url & server_key gets updated in the process. (save the updated server_key and server_url).
*/
$response = $ytEventObj->updateBroadcast($data, $youtubeEventId);

// $youtubeEventId = $response['broadcast_response']['id'];
// $serverUrl = $response['stream_response']['cdn']->ingestionInfo->ingestionAddress;
// $serverKey = $response['stream_response']['cdn']->ingestionInfo->streamName

$ytEventObj = new LiveStreamService($authToken);

# Deleting the event 

$ytEventObj = new LiveStreamService($authToken);
/**
 * $broadcastStatus - ["testing", "live"]
 * Starting the event takes place in 3 steps
 * 1. Start sending the stream to the server_url via server_key recieved as a response in creating the event via the encoder of your choice.
 * 2. Once stream sending has started, stream test should be done by passing $broadcastStatus="testing" & it will return response for stream status.
 * 3. If transitioEvent() returns successfull for testing broadcast status, then start live streaming your video by passing $broadcastStatus="live" 
 * & in response it will return us the stream status.
 */ 
$streamStatus = $ytEventObj->transitionEvent($youtubeEventId, $broadcastStatus);	

$ytEventObj = new LiveStreamService($authToken);
/**
 * $broadcastStatus - ["complete"]
 * Once live streaming gets started succesfully. We can stop the streaming the video by passing broadcastStatus="complete" and in response it will give us the stream status.
 */
$ytEventObj->transitionEvent($youtubeEventId, $broadcastStatus);	// $broadcastStatus = ["complete"]


namespace Your\App\NameSpace;

use  ZeroDUDDU\YoutubeLaravelApi\ChannelService;

/**
 * [channelsListById -gets the channnel details and ]
 *   $part    'id,snippet,contentDetails,status, statistics, contentOwnerDetails, brandingSettings'
 *  $params  [array channels id(comma separated ids ) or you can get ('forUsername' => 'GoogleDevelopers')]
 */

$part = 'id,snippet';
$params = array('id'=> 'channel_1_id,channel_2_id');
$channelServiceObject  = new ChannelService($authToken);
$channelDetails = $channelServiceObject->channelsListById($part, $params);

 
$channelServiceObject  = new ChannelService;
$channelDetails = $channelServiceObject->getChannelDetails($authToken);


/*
* $params array('channelId'=>'', 'totalResults'= 10)
* totalResults is different of maxResults from Google Api.
* totalResults = the amount of results you want
* maxResults = max of results PER PAGE. We don't need this parameter here since it will loop until it gets all the results you want.
*/
$channelServiceObject  = new ChannelService($authToken);
$channelDetails = $channelServiceObject->subscriptionByChannelId($params);
 
/*
* properties  array('snippet.resourceId.kind' => 'youtube#channel','snippet.resourceId.channelId' => 'UCqIOaYtQak4-FD2-yI7hFkw')
*/
$channelServiceObject  = new ChannelService;
$response = $channelServiceObject->addSubscriptions($properties, $token, $part='snippet', $params=[]);


$response = $channelServiceObject->removeSubscription( $token, $subscriptionId);


/*
 *      $properties array('id' => '',
 *					'brandingSettings.channel.description' => '',
 *					'brandingSettings.channel.keywords' => '',
 *					'brandingSettings.channel.defaultLanguage' => '',
 *					'brandingSettings.channel.defaultTab' => '',
 *					'brandingSettings.channel.moderateComments' => '',
 *					'brandingSettings.channel.showRelatedChannels' => '',
 *					'brandingSettings.channel.showBrowseView' => '',
 *					'brandingSettings.channel.featuredChannelsTitle' => '',
 *					'brandingSettings.channel.featuredChannelsUrls[]' => '',
 *					'brandingSettings.channel.unsubscribedTrailer' => '')
 */

$channelServiceObject  = new ChannelService($authToken);
$response = $channelServiceObject->updateChannelBrandingSettings($properties);


namespace Your\App\NameSpace;

use  ZeroDUDDU\YoutubeLaravelApi\VideoService;

$part ='snippet,contentDetails,id,statistics';
$params =['id'=>'xyzgh'];
$videoServiceObject  = new VideoService($authToken);
$response = $videoServiceObject->videosListById($part, $params);


$videoServiceObject  = new VideoService($authToken);
$response = $videoServiceObject->uploadVideo($videoPath, $title, $description, $categoryId, $privacyStatus, $tags, $data);

$videoServiceObject  = new VideoService($authToken);
$response = $videoServiceObject->deleteVideo($videoId);

# rating  'like' or 'dislike' or 'none'
	$videoServiceObject  = new VideoService($authToken);
$response = $videoServiceObject->videosRate( $videoId, $rating);

shell
php artisan vendor:publish --tag='youtube-config'