YouTube is the most popular video-sharing website that allows users to upload, view, and share videos online. If your web application has video upload functionality and you want to reduce the space of your server, upload video to YouTube will be a great idea. By uploading videos on YouTube, you will get the following facilities.
YouTube Data API provides an easy way to upload video to YouTube from the website using Google API Client Library. In this tutorial, we will show how you can upload video to YouTube from web application using PHP. Our example code will use the Google API PHP Client Library to upload video on YouTube from the website using PHP.
In the example YouTube video uploader script, the following functionality will be implemented.
Before getting started to build a PHP script to upload video to YouTube using PHP OAuth library, take a look at the file structure.
upload_video_to_youtube_with_php/ ├── config.php ├── dbConfig.php ├── index.php ├── upload.php ├── youtube_video_sync.php ├── status.php ├── logout.php ├── google-api-php-client/ └── videos/
You need to register your application on Google Developers Console for using Google API Client Library. Also, make sure the YouTube Data API v3 library is enabled to access YouTube data.
A dialog box will appear with OAuth client details, note the Client ID and Client secret for later use in the script. This Client ID and Client secret allow you to access the Google YouTube Data API.
Note that: The Client ID and Client secret need to be specified at the time of Google API call. Also, the Authorized redirect URIs must be matched with the Redirect URL specified in the script.
Do you want a detailed guide on Google project creation? See this tutorial – How to Create Google API Console Project
The Google API Client Library helps to authenticate with Google account and access the YouTube Data API using PHP. All the required Google API Client Library files are placed in the google-api-php-client directory.
To store the video information a table is required in the database. The following SQL creates a videos
table with some basic fields in the MySQL database.
CREATE TABLE `videos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci DEFAULT NULL,
`tags` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`privacy` enum('public','private') COLLATE utf8_unicode_ci NOT NULL,
`file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`youtube_video_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
In the config.php
file, constant variables of the Google OAuth API Client and database settings are defined.
Database Constants:
Google OAuth Client Constants:
Based on the OAuth API client configurations, the Google_Client class is loaded and the Google_Service_YouTube object is defined.
<?php
// Database configuration
define('DB_HOST', 'MySQL_Database_Host');
define('DB_USERNAME', 'MySQL_Database_Username');
define('DB_PASSWORD', 'MySQL_Database_Password');
define('DB_NAME', 'MySQL_Database_Name');
// Google API configuration
define('OAUTH_CLIENT_ID', 'Google_Project_Client_ID');
define('OAUTH_CLIENT_SECRET', 'Google_Project_Client_Secret');
define('REDIRECT_URL', 'https://www.example.com/youtube_video_sync.php');
// Start session
if(!session_id()) session_start();
// Include google client libraries
require_once 'google-api-php-client/autoload.php';
require_once 'google-api-php-client/Client.php';
require_once 'google-api-php-client/Service/YouTube.php';
// Initialize Google Client class
$client = new Google_Client();
$client->setClientId(OAUTH_CLIENT_ID);
$client->setClientSecret(OAUTH_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setRedirectUri(REDIRECT_URL);
// Define an object that will be used to make all API requests
$youtube = new Google_Service_YouTube($client);
?>
Note that: The Client ID and Client Secret can be found on the API Manager page of the Google API Console project.
The dbConfig.php
file is used to connect and select the database using PHP and MySQL.
<?php
// Include configuration file
require_once 'config.php';
// Create database connection
$db = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
Create an HTML form to select file and input video info (title, description, tags, privacy, etc.).
enctype="multipart/form-data"
attribute must be defined in the <form> tag to allow file upload.upload.php
) to process the upload functionality.<?php
// Include configuration file
include_once 'config.php';
$status = $statusMsg = '';
if(!empty($_SESSION['status_response'])){
$status_response = $_SESSION['status_response'];
$status = $status_response['status'];
$statusMsg = $status_response['status_msg'];
unset($_SESSION['status_response']);
}
?>
<!-- Status message -->
<?php if(!empty($statusMsg)){ ?>
<div class="alert alert-<?php echo $status; ?>"><?php echo $statusMsg; ?></div>
<?php } ?>
<!-- Upload form -->
<div class="col-md-12">
<form method="post" action="upload.php" class="form" enctype="multipart/form-data">
<div class="form-group">
<label>Video File:</label>
<input type="file" name="file" class="form-control" required>
</div>
<div class="form-group">
<label>Title:</label>
<input type="text" name="title" class="form-control" required>
</div>
<div class="form-group">
<label>Description:</label>
<textarea name="description" class="form-control"></textarea>
</div>
<div class="form-group">
<label>Tags:</label>
<input type="text" name="tags" class="form-control">
</div>
<div class="form-group">
<label>Privacy:</label>
<select name="privacy" class="form-control">
<option value="public">Public</option>
<option value="private">Private</option>
</select>
</div>
<div class="form-group">
<input type="submit" class="form-control btn-primary" name="submit" value="Upload">
</div>
</form>
</div>
The upload.php
file handles the file upload in PHP and the data insertion process in the MySQL database.
createAuthUrl()
method of Google_Client class.<?php
// Include database configuration file
require_once 'dbConfig.php';
$statusMsg = $valErr = '';
$status = 'danger';
// If the form is submitted
if(isset($_POST['submit'])){
// Allowed mime types of the file to upload
$allowedTypeArr = array("video/mp4", "video/avi", "video/mpeg", "video/mpg", "video/mov", "video/wmv", "video/rm", "video/quicktime");
// Store post data in session
$_SESSION['postData'] = $_POST;
// Get input's value
$title = $_POST['title'];
$description = $_POST['description'];
$tags = $_POST['tags'];
$privacy = !empty($_POST['privacy'])?$_POST['privacy']:'public';
// Validate form input fields
if(empty($_FILES["file"]["name"])){
$valErr .= 'Please select a video file to upload.<br/>';
}elseif(!in_array($_FILES['file']['type'], $allowedTypeArr)){
$valErr .= 'Sorry, only MP4, AVI, MPEG, MPG, MOV, and WMV files are allowed to upload.<br/>';
}
if(empty($title)){
$valErr .= 'Please enter the title.<br/>';
}
// Check whether user inputs are empty
if(empty($valErr)){
$targetDir = "videos/";
$fileName = time().'_'.basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir.$fileName;
// Upload file to local server
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
// Insert data into the database
$sqlQ = "INSERT INTO videos (title,description,tags,privacy,file_name,created) VALUES (?,?,?,?,?,NOW())";
$stmt = $db->prepare($sqlQ);
$stmt->bind_param("sssss", $db_title, $db_description, $db_tags, $db_privacy, $db_file_name);
$db_title = $title;
$db_description = $description;
$db_tags = $tags;
$db_privacy = $privacy;
$db_file_name = $fileName;
$insert = $stmt->execute();
if($insert){
$file_id = $stmt->insert_id;
// Remove post data from session
unset($_SESSION['postData']);
// Store DB reference ID of file in SESSION
$_SESSION['last_uploaded_file_id'] = $file_id;
// Get Google OAuth URL
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;
$googleOauthURL = $client->createAuthUrl();
// Redirect user for Google authentication
header("Location: $googleOauthURL");
exit();
}else{
$statusMsg = 'Something went wrong, please try again after some time.';
}
}else{
$statusMsg = 'File upload failed, please try again after some time.';
}
}else{
$statusMsg = '<p>Please fill all the mandatory fields:</p>'.trim($valErr, '<br/>');
}
}else{
$statusMsg = 'Form submission failed!';
}
$_SESSION['status_response'] = array('status' => $status, 'status_msg' => $statusMsg);
header("Location: index.php");
exit();
?>
This script is set as Redirect URI in Google API configuration. This means after authentication with the Google account, the user will be redirected to this script that handles the YouTube video upload process with the Google Client library using PHP.
getAccessToken()
method of Google_Client class.videos.insert
method.<?php
// Include database configuration file
require_once 'dbConfig.php';
$statusMsg = '';
$status = 'danger';
$redirectURL = 'index.php';
// Check if an auth token exists for the required scopes
$tokenSessionKey = 'token-' . $client->prepareScopes();
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
header('Location: ' . REDIRECT_URL);
}
if (isset($_SESSION[$tokenSessionKey])) {
$client->setAccessToken($_SESSION[$tokenSessionKey]);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
// Get file reference ID from SESSION
$file_id = $_SESSION['last_uploaded_file_id'];
if(!empty($file_id)){
// Fetch video file details from the database
$sqlQ = "SELECT * FROM videos WHERE id = ?";
$stmt = $db->prepare($sqlQ);
$stmt->bind_param("i", $db_file_id);
$db_file_id = $file_id;
$stmt->execute();
$result = $stmt->get_result();
$videoData = $result->fetch_assoc();
if(!empty($videoData)){
$file_name = $videoData['file_name'];
$videoPath = 'videos/'.$file_name;
if(!empty($videoData['youtube_video_id'])){
// Get video info from local database
$video_title = $videoData['title'];
$video_desc = $videoData['description'];
$video_tags = $videoData['tags'];
$youtube_video_id = $videoData['youtube_video_id'];
}else{
try {
// Create a snippet with title, description, tags and category ID
// Create an asset resource and set its snippet metadata and type.
// This example sets the video's title, description, keyword tags, and
// video category.
$snippet = new Google_Service_YouTube_VideoSnippet();
$snippet->setTitle($videoData['title']);
$snippet->setDescription($videoData['description']);
$snippet->setTags(explode(",", $videoData['tags']));
// Numeric video category. See
// https://developers.google.com/youtube/v3/docs/videoCategories/list
$snippet->setCategoryId("22");
// Set the video's status to "public". Valid statuses are "public",
// "private" and "unlisted".
$status = new Google_Service_YouTube_VideoStatus();
$status->privacyStatus = $videoData['privacy'];
// Associate the snippet and status objects with a new video resource.
$video = new Google_Service_YouTube_Video();
$video->setSnippet($snippet);
$video->setStatus($status);
// Specify the size of each chunk of data, in bytes. Set a higher value for
// reliable connection as fewer chunks lead to faster uploads. Set a lower
// value for better recovery on less reliable connections.
$chunkSizeBytes = 1 * 1024 * 1024;
// Setting the defer flag to true tells the client to return a request which can be called
// with ->execute(); instead of making the API call immediately.
$client->setDefer(true);
// Create a request for the API's videos.insert method to create and upload the video.
$insertRequest = $youtube->videos->insert("status,snippet", $video);
// Create a MediaFileUpload object for resumable uploads.
$media = new Google_Http_MediaFileUpload(
$client,
$insertRequest,
'video/*',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($videoPath));
// Read the media file and upload it chunk by chunk.
$status = false;
$handle = fopen($videoPath, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
// If you want to make other calls after the file upload, set setDefer back to false
$client->setDefer(false);
if(!empty($status['id'])){
// Uploaded youtube video info
$video_title = $status['snippet']['title'];
$video_desc = $status['snippet']['description'];
$video_tags = implode(",",$status['snippet']['tags']);
$youtube_video_id = $status['id'];
// Update youtube video reference id in the database
$sqlQ = "UPDATE videos SET youtube_video_id=? WHERE id=?";
$stmt = $db->prepare($sqlQ);
$stmt->bind_param("si", $db_youtube_video_id, $db_file_id);
$db_youtube_video_id = $youtube_video_id;
$db_file_id = $file_id;
$update = $stmt->execute();
if($update){
// Delete video file from local server
@unlink($videoPath);
}
unset($_SESSION['last_uploaded_file_id']);
$status = 'success';
$statusMsg = 'Video has been uploaded to YouTube successfully!';
}
} catch (Google_Service_Exception $e) {
$statusMsg = 'A service error occurred: <code>'.$e->getMessage().'</code>';
} catch (Google_Exception $e) {
$statusMsg = 'An client error occurred: <code>'.$e->getMessage().'</code>';
$statusMsg .= '<br/>Please reset session <a href="logout.php">Logout</a>';
}
}
if(!empty($youtube_video_id)){
$redirectURL = 'status.php?fid='.base64_encode($file_id);
}
}else{
$statusMsg = 'File data not found!';
}
}else{
$statusMsg = 'File reference not found!';
}
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
}else{
$statusMsg = 'Failed to fetch access token!';
}
$_SESSION['status_response'] = array('status' => $status, 'status_msg' => $statusMsg);
header("Location: $redirectURL");
exit();
Note that: This file URL must be set as Redirect URL in Authorized redirect URIs section of the Google API console project.
Once the YouTube video upload process is completed, the user is redirected to this status page. In this status.php
file, the uploaded video details are displayed on the webpage.
<?php
// Include configuration file
include_once 'dbConfig.php';
if(!empty($_GET['fid'])){
$file_id = base64_decode($_GET['fid']);
// Fetch video details from the database
$sqlQ = "SELECT * FROM videos WHERE id = ?";
$stmt = $db->prepare($sqlQ);
$stmt->bind_param("i", $db_file_id);
$db_file_id = $file_id;
$stmt->execute();
$result = $stmt->get_result();
$videoData = $result->fetch_assoc();
}
if(empty($videoData)){
header("Location: index.php");
exit();
}
$status = $statusMsg = '';
if(!empty($_SESSION['status_response'])){
$status_response = $_SESSION['status_response'];
$status = $status_response['status'];
$statusMsg = $status_response['status_msg'];
unset($_SESSION['status_response']);
}
?>
<h2>Video Details <a href="logout.php"><span class="badge bg-primary">logout</span></a></h2>
<!-- Status message -->
<?php if(!empty($statusMsg)){ ?>
<div class="alert alert-<?php echo $status; ?>"><?php echo $statusMsg; ?></div>
<?php } ?>
<!-- YouTube video info -->
<?php
if(!empty($videoData)){
$youtubeURL = 'https://youtu.be/'.$videoData['youtube_video_id'];
$privacy_class = ($videoData['privacy'] == 'private')?'danger':'success';
?>
<div class="card">
<embed width="100%" src="https://www.youtube.com/embed/<?php echo $videoData['youtube_video_id']; ?>"></embed>
<div class="card-body">
<h5 class="card-title"><?php echo $videoData['title']; ?></h5>
<p class="card-text"><?php echo $videoData['description']; ?></p>
<p><b>Tags:</b> <?php echo $videoData['tags']; ?></p>
<p><b>Privacy:</b> <span class="badge bg-<?php echo $privacy_class; ?>"><?php echo $videoData['privacy']; ?></span></p>
<p><b>YouTube URL:</b> <a href="<?php echo $youtubeURL; ?>" target="_blank"><?php echo $youtubeURL; ?></a></p>
</div>
<a href="index.php" class="btn btn-primary">New Upload</a>
</div>
<?php } ?>
On first time authentication, a SESSION is generated and the user doesn’t need to authenticate again for upload. If the user wishes to log out from the Google OAuth, the logout.php
file is loaded.
<?php
// Include configuration file
require_once 'config.php';
// Revoke token & destroy session
$client->revokeToken();
session_destroy();
// Redirect to the homepage
header("Location: index.php");
exit;
?>
Get Videos from YouTube Channel using Data API v3 and PHP
YouTube Data API provides the easiest way to upload video to YouTube channel with PHP. You can upload videos from the website to YouTube channel using PHP. In this example script, the Google client library is used to upload video to YouTube with YouTube Data API using PHP.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Where to spicify the youtube channnel?
Hi,
There is option to post to a specific Youtube Channel always without authentication each upload process using refresh token. Is it possible with modifying this code?
Hello,
can I purchase customization to enable it to work with WordPress?
which file you import in database ???
To store the video information a table needs to be created in the database. In the video tutorial, we have imported the
videos.sql
file. This SQL file can be found in our source code.Thank for Great Code and effort
I have a question if you can guide me its help full how to submit video with url like “www.xyz.com/abc.mp4” and this video will upload to my youtube channel,,, or if script can pick video from my local drive without file selector…
Sir awesome. Very helpful.
great tutorials. how can i use without authentication upload video and other user upload my channel.
hello sir,
great tutorials. how can i use without authentication upload video and other user upload my channel.
it works fine. Great work.It really helpful in my project
from where i collect google api , and where i put i plz reply
You will get all the required Google Client Library in our source code package.
It works fine. Thank you for the post!
There is option to post to a specific Youtube Channel always without authentication each upload process using refresh token. Is it possible with modifying this code?
Hi,
How do I use the API to upload with needing the user to be authorized.
Basically, I want to prompt the user like you are in this script, then upload to MY youtube channel.
its showing authorisation required whats the problem
The authentication is required before upload video to YouTube.
hello sir,
great tutorials. how can i use without authentication upload video and other user upload my channel.
Thanks alot, Boss.
Super Helpful.
Hey thank for such a good tutorials
But can you tell me how to take input from user and directly upload it to youtube
It works for me thanks.
i want to upload video direct from it to youtube using cakephp. how to convert this code to cake php
Hii i need to upload video to youtube from url 🙂
There is option to post to a specific Youtube Channel always without authentication each upload process using refresh token. Is it possible with modifying this code?
There is option to post to a specific Youtube Channel always without authentication each upload process using refresh token. Is it possible with modifying this code?
Dear Sir/Mam, Thanks for giving me such a script to upload video on youtube from websites php script. This script working fine but it required each & everytime google authentication to upload video on youtube my channel. but my requirement is multiple user upload video on my single youtube channel. but this script upload video the authentication user. so please let me know is there any way multiple user can upload video from my websites to only my youtube channel instead of user youtube channel. OR is this possible i fetch record of video from database and upload all the video on my youtube channel at a time. Please reply me as soon as possible. Thanks
Hi,
Thanks for the amzing script, i want to know how can i attach/upload video thumbnail when uploading the video.
Regards
Hello,
Is it possible to always publish on my account , users publish from my website on my account youtube without the need of authorization of his account ?
Thank you
It works fine. Thank you for the post.
There is option to post to a specific Youtube Channel always without authentication each upload process using refresh token. Is it possible with modifying this code?