PHP tutorials,a way to getting some thing new in web

Followers

Powered by Blogger.

Tuesday 2 September 2014

How to upload video on Youtube using PHP

No comments :
Today, I am going to show you code for very advance API.We use youtube in daily basis but this time php will be used to post video on youtube.I have got new code for it and here I thought to  share with you all.

The code is so simple.you should download zend package from zend site.and got developer key from google.also your gmail username and password will work in this code.








Here is code:

<?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_YouTube');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
 try 
 {
$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient =  Zend_Gdata_ClientLogin::getHttpClient(
 $username = 'XXXX', //your gmail password and username
 $password = 'XXXXX',
 $service = 'youtube',
 $client = null,
 $source = 'Yoh Money',            
 $loginToken = null,
 $loginCaptcha = null,
 $authenticationURL);
 } 
 catch (Zend_Gdata_App_Exception $e) 
 {
 $arry['data']['flag'] = false;
 $arry['data']['msg'] = 'Username or Password Invalid.';
 print_r(json_encode($arry));
 die();
 }

$developerKey='XXXXXX';//make an application and get its developer key
$applicationId = 'not require';
$clientId = 'not require';
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
$fileName = "Offo.mp4";   
$fileType = "video/mp4"; 

$newEntry = new Zend_Gdata_YouTube_VideoEntry();
$filesource = $yt->newMediaFileSource($fileName);
$filesource->setContentType('video/mp4');
$filesource->setSlug($fileName); 
$newEntry->setMediaSource($filesource);
$newEntry->setVideoTitle("Offo"); 
$newEntry->setVideoDescription("Offo is awesome song of 2states"); 
$newEntry->setVideoCategory("Comedy"); 
$newEntry->setVideoTags("2 States,Offo");  
try {
$newEntry = $yt->insertEntry($newEntry, 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads', 'Zend_Gdata_YouTube_VideoEntry');
$state = $newEntry->getVideoState();
if ($state) 
{
$videourl = $newEntry->getVideoWatchPageUrl();
$arry['data']['flag'] = true;
$arry['data']['url'] = $videourl;
$arry['data']['msg'] = "Video Uploaded Successfully.";


else 
{
$arry['data']['flag'] = false;
$arry['data']['msg'] = "Not able to retrieve the video status information yet. " ."Please try again later.\n";
}


catch (Zend_Gdata_App_Exception $e) {
 $arry['data']['flag'] = false;
 $arry['data']['msg'] = $e->getMessage();
}
echo "<pre>";
print_r($arry);

?>

No comments :

Post a Comment