PHP (Laravel) Package for Google / YouTube API of Video Live Streaming with Google Auth
PHP (Laravel) Package for Google / YouTube API of Video Live Streaming with Google Auth
composer require sahusoftcom/youtube-livestream-api
Add Service provider to config/app.php provider's array:
SahusoftCom\YoutubeApi\LiveStreamApiServiceProvider::class
Execute the following command to get the configurations:
php artisan vendor:publish --tag='youtube-config'
https://console.developers.google.com
API key
from credentials tab.product name
(your site name).you site url
.add a url which you want the auth code to return
(redirect_url)<?php
namespace Your\App\NameSpace;
use SahusoftCom\YoutubeApi\AuthService; // Add Code to call the api class
$authServiceObject = new AuthService();
# Replace the identifier with a unqiue identifier for account or channel
$authUrl = $authServiceObject->getLoginUrl('email','identifier');
<?php
namespace Your\App\NameSpace;
use SahusoftCom\YoutubeApi\AuthService; // Add Code to call the api class
$authServiceObject = new AuthService();
$authToken = $authServiceObject->getToken($authcode);
<?php
namespace Your\App\NameSpace;
use SahusoftCom\YoutubeApi\YoutubeLiveEventService; // Add Code to call the api class
$data = array(
"title" => "",
"description" => "",
"thumbnail_path" => "", // Optional
"event_start_date_time" => "",
"event_end_date_time" => "", // Optional
"time_zone" => "",
'privacy_status' => "", // default: "public" OR "private"
"language_name" => "", // default: "English"
"tag_array" => "" // Optional and should not be more than 500 characters
);
$ytEventObj = new YoutubeLiveEventService();
/**
* The broadcast function returns array of details from YouTube.
* Store this information & will be required to supply to youtube
* for live streaming using encoder of your choice.
*/
$response = $ytEventObj->broadcast($authToken, $data);
if ( !empty($response) ) {
$youtubeEventId = $response['broadcast_response']['id'];
$serverUrl = $response['stream_response']['cdn']->ingestionInfo->ingestionAddress;
$serverKey = $response['stream_response']['cdn']->ingestionInfo->streamName;
}
<?php
namespace Your\App\NameSpace;
use SahusoftCom\YoutubeApi\YoutubeLiveEventService; // Add Code to call the api class
$ytEventObj = new YoutubeLiveEventService();
/**
* 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($authToken, $data, $youtubeEventId);
// $youtubeEventId = $response['broadcast_response']['id'];
// $serverUrl = $response['stream_response']['cdn']->ingestionInfo->ingestionAddress;
// $serverKey = $response['stream_response']['cdn']->ingestionInfo->streamName
<?php
namespace Your\App\NameSpace;
use SahusoftCom\YoutubeApi\YoutubeLiveEventService; // Add Code to call the api class
$ytEventObj = new YoutubeLiveEventService();
/**
* Deleting the event requires authentication token for the channel in which the event is created and the youtube_event_id
*/
$ytEventObj->deleteEvent($authToken, $youtubeEventId);
<?php
namespace Your\App\NameSpace;
use SahusoftCom\YoutubeApi\YoutubeLiveEventService; // Add Code to call the api class
$ytEventObj = new YoutubeLiveEventService();
/**
* $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($authToken, $youtube_event_id,$broadcastStatus);
<?php
namespace Your\App\NameSpace;
use SahusoftCom\YoutubeApi\YoutubeLiveEventService; // Add Code to call the api class
$ytEventObj = new YoutubeLiveEventService();
/**
* $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($authToken,$youtube_event_id, $broadcastStatus); // $broadcastStatus = ["complete"]