Example – Upload video to YouTube using PHP on Website


YouTube is one of the most popular for video upload and video-sharing. If your web application has video upload functionality, then upload video to YouTube would be the best idea. Once the upload is completed, display video at web application by the returned YouTube video ID. This will reduce the server space and provide the simple way for playing video.

In this tutorial we will discuss how can you upload video to YouTube from your web application using PHP. We have developed PHP scripts for upload video to YouTube using PHP OAuth library. You can view the live demo of the script from the Demo link and also can download the full Upload video on YouTube using PHP API project from the Download link.

Please follow the steps for configuring Upload video on YouTube using PHP project.

This YouTube video upload has two main folders named src/, includes/ and some PHP files. The folders and files structure are given below:

  • src/
    • Google API, Google Client, Google Oauth and other libraries
  • includes/
    • DB.php
  • config.php
  • index.php
  • youtube_upload.php
  • logout.php

Google Project Creation:

Go to the Google Developers Console – https://console.developers.google.com/
Select an existing project, or create a new project by clicking Create Project:

  • In the Project name field, type in a name for your new project.
  • In the Project ID field, the console has created project ID. Optionally you can type in a project ID for your project. But project ID must be unique world-wide.
  • Click on the Create button and the project to be created within some seconds. Once the project is created successfully, the project name would be appearing at the top of the left sidebar.

In the left sidebar, select APIs under the APIs & auth section. A list of Google APIs appears.

Find the Google+ API service and set its status to Enable.
Find the YouTube Data API service and set its status to Enable.

In the sidebar, select Credentials under the APIs & auth section.

In the OAuth section of the page, select Create New Client ID.

  • Create Client ID dialog box would be appearing for choosing application type.
  • In the Application type section of the dialog, select Web application and click on the Configure consent screen button.
  • Choose Email address, enter Product name and save the form.
  • In the Authorized JavaScript origins field, enter your app origin. If you want to allow your app to run on different protocols, domains, or subdomains, then you can enter multiple origins.
  • In the Authorized redirect URI field, enter the redirect URL.
  • Click on Create Client ID.

Now you can see the Client ID for web application section. Note the Client ID and Client secret that will need to use to access the APIs.

Database table creation:

To store the details of video, you need to create a database and a table. At first create a database like tutspointer. After that copy the below SQL query and run the SQL query on the database.

Project Configuration:

In this project you just have to modify only the following two files.
Database configuration (includes/DB.php):
DB.php file have DB class with some functions for handle the database works. Into the __construct() function we have configured the database and connect with the database. You need only to change the $dbHost, $dbUsername, $dbPassword and $dbName variables value with your MySQL server details.

Google API configuration (config.php): Open the config.php file and modify the $oauthClientID, $oauthClientSecret, $baseUri and $redirectUri variables value.

Note that: You can find the $clientId and $clientSecret at the Google project page.(Go to the https://console.developers.google.com/=> click on your project => click on the “APIs & auth” link from the left menu panel => click on the Credentials link from under the APIs & auth menu => copy the Client ID and Client Secret)

If you have any query about this tutorial and scripts, feel free to comment here.

Leave A Reply

Your email address will not be published.