Created
December 8, 2012 13:40
-
-
Save shanestillwell/4240305 to your computer and use it in GitHub Desktop.
Upload files to YouTube with a Zend Framework script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/** | |
* Put this in your ./scripts folder | |
* Make it executeable (chmod +x) | |
* | |
* Usage: | |
* Edit the interesting bits below put in your own keys and such | |
* ./upload2youtube.php | |
*/ | |
// Define path to application directory | |
defined('APPLICATION_PATH') | |
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); | |
// Define application environment, (I am only running this in development) | |
define('APPLICATION_ENV', 'development'); | |
// Ensure library/ is on include_path | |
set_include_path(implode(PATH_SEPARATOR, array( | |
realpath(APPLICATION_PATH . '/../library'), | |
get_include_path(), | |
))); | |
/** Zend_Application */ | |
require_once 'Zend/Application.php'; | |
// Creating application | |
$application = new Zend_Application( | |
APPLICATION_ENV, | |
APPLICATION_PATH . '/configs/application.ini' | |
); | |
// Bootstrapping resources | |
$bootstrap = $application->bootstrap()->getBootstrap(); | |
$bootstrap->bootstrap(); | |
$description = $title = 'TITLE'; | |
$url = 'http://link.to.your.mov'; | |
/** | |
* Autos & Vehicles | |
* Comedy | |
* Education | |
* Entertainment | |
* Film & Animation | |
* Gaming | |
* Howto & Style | |
* Music | |
* News & Politics | |
* Nonprofits & Activism | |
* People & Blogs | |
* Pets & Animals | |
* Science & Technology | |
* Sports | |
* Travel & Events | |
* | |
*/ | |
$category = 'Entertainment'; | |
$tags = ''; | |
$username = 'USERNAME'; | |
$password = 'PASSWORD'; | |
$developer_key = | |
'DEVELOPER_KEY'; | |
$application_id = 'APP_ID'; | |
$info = parse_url($url); | |
$file = basename($info['path']); | |
//Get File from S3 | |
$ch = curl_init($url); | |
$fp = fopen($file, 'wb'); | |
curl_setopt($ch, CURLOPT_FILE, $fp); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_exec($ch); | |
curl_close($ch); | |
fclose($fp); | |
// Get mimetype | |
if (function_exists('finfo')) { | |
$finfo = new finfo(FILEINFO_MIME_TYPE); | |
$mimetype = $finfo->file($file); | |
} elseif (function_exists('mime_content_type')) { | |
$mimetype = mime_content_type($file); | |
} else { | |
$mimetype = 'video/quicktime'; | |
} | |
print "Down downloading file\n . $mimetype"; | |
// Upload to YouTube | |
$authenticationURL= 'https://www.google.com/accounts/ClientLogin'; | |
$httpClient = | |
Zend_Gdata_ClientLogin::getHttpClient( | |
$username = $username, | |
$password = $password, | |
$service = 'youtube', | |
$client = null, | |
$source = $application_id, // a short string identifying your application | |
$loginToken = null, | |
$loginCaptcha = null, | |
$authenticationURL); | |
$yt = new Zend_Gdata_YouTube($httpClient, | |
$application_id, | |
NULL, | |
$developer_key); | |
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry(); | |
$filesource = $yt->newMediaFileSource($file); | |
$filesource->setContentType($mimetype); | |
$filesource->setSlug($file); | |
$myVideoEntry->setMediaSource($filesource); | |
$myVideoEntry->setVideoTitle($title); | |
$myVideoEntry->setVideoDescription($description); | |
$myVideoEntry->setVideoCategory($category); | |
$myVideoEntry->setVideoTags($tags); | |
$myVideoEntry->setVideoPrivate(); | |
// Upload URI for the currently authenticated user | |
$uploadUrl = | |
'http://uploads.gdata.youtube.com/feeds/users/default/uploads'; | |
// Try to upload the video, catching a Zend_Gdata_App_HttpException | |
// if availableor just a regular Zend_Gdata_App_Exception | |
try { | |
$newEntry = $yt->insertEntry($myVideoEntry, | |
$uploadUrl, | |
'Zend_Gdata_YouTube_VideoEntry'); | |
} catch (Zend_Gdata_App_HttpException $httpException) { | |
echo $httpException->getRawResponseBody(); | |
} catch (Zend_Gdata_App_Exception $e) { | |
echo $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment