Today I would like to talk about video. Maybe you’ve got own video website, maybe you’re thinking about it, but anyway I think that our new information will be useful for you. As you know, video usually means that you need to have a lot of space at your hosting. And it is true in case if you store video files at your own server. But, in order to avoid all these difficulties (video storing and conversion), you can try to work with 3-rd party video hostings. As example youtube (or vimeo). In our new tutorial I will tell you how you can create youtube cross-uploader for your website.
In order to achieve our idea we will use
YouTube API v2.0 – Browser-based Uploading. In the beginning, we should prepare our own access token key. OAuth will help us with it. Then, we will display a form, where user can enters the video details (like title, category, description and keywords). When we have sent this information, Youtube will return us temporary upload token and url. Once we get it – we can start upload of selected video file.
[sociallocker]
[/sociallocker]
Now – download the source files and lets start coding !
Step 1. PHP
Now, please create empty index.php file and put next code:
index.php
003 |
if (version_compare(phpversion(), '5.3.0' , '>=' ) == 1) |
004 |
error_reporting (E_ALL & ~E_NOTICE & ~E_DEPRECATED); |
006 |
error_reporting (E_ALL & ~E_NOTICE); |
008 |
$sDevKey = 'Your Youtube Developer Key' ; |
010 |
$sAuthUrl = urlencode( $sWebPageUrl . '?action=auth' ); |
013 |
$sToken = $sUploadUrl = $sUploadToken = $sNextUrl = $sVideoId = '' ; |
016 |
$sAction = $_REQUEST [ 'action' ]; |
019 |
$sToken = $_GET [ 'token' ]; |
023 |
$sTitle = strip_tags ( $_POST [ 'title' ]); |
024 |
$sDesc = strip_tags ( $_POST [ 'description' ]); |
025 |
$sCategory = strip_tags ( $_POST [ 'category' ]); |
026 |
$sKeywords = strip_tags ( $_POST [ 'keywords' ]); |
027 |
$sToken = strip_tags ( $_POST [ 'token' ]); |
030 |
<?xml version= "1.0" ?> |
035 |
<media:title type= "plain" >{ $sTitle }</media:title> |
036 |
<media:description type= "plain" >{ $sDesc }</media:description> |
038 |
<media:keywords>{ $sKeywords }</media:keywords> |
043 |
$aHeaders = array ( "POST /action/GetUploadToken HTTP/1.1" , |
044 |
"Host: gdata.youtube.com" , |
045 |
"Authorization: AuthSub token=" . $sToken , |
046 |
"X-GData-Key: key=" . $sDevKey , |
047 |
"Content-Length: " . strlen ( $sData ), |
048 |
"Content-Type: application/atom+xml; charset=UTF-8" |
050 |
$sUserAgent = $_SERVER [ 'HTTP_USER_AGENT' ]; |
052 |
$oCurl = curl_init(); |
053 |
curl_setopt( $oCurl , CURLOPT_URL, $sUplTokenUrl ); |
054 |
curl_setopt( $oCurl , CURLOPT_HTTPHEADER, $aHeaders ); |
055 |
curl_setopt( $oCurl , CURLOPT_HEADER, true); |
056 |
curl_setopt( $oCurl , CURLOPT_TIMEOUT, 30); |
057 |
curl_setopt( $oCurl , CURLOPT_USERAGENT, $sUserAgent ); |
058 |
curl_setopt( $oCurl , CURLOPT_RETURNTRANSFER, true); |
059 |
curl_setopt( $oCurl , CURLOPT_POSTFIELDS, $sData ); |
060 |
curl_setopt( $oCurl , CURLOPT_POST, true); |
061 |
$sXmlRes = curl_exec( $oCurl ); |
064 |
$sXmlSrc = substr ( $sXmlRes , strpos ( $sXmlRes , '<?xml' )); |
065 |
if ( $sXmlSrc != '' ) { |
066 |
$oXml = simplexml_load_string( $sXmlSrc ); |
067 |
$sUploadUrl = $oXml ->url; |
068 |
$sUploadToken = $oXml ->token; |
069 |
$sNextUrl = urlencode( $sWebPageUrl . '?action=finish' ); |
073 |
$iUploadStatus = (int) $_GET [ 'status' ]; |
074 |
$sVideoId = $_GET [ 'id' ]; |
081 |
<meta charset= "utf-8" /> |
082 |
<title>YouTube API - OAuth and Upload Example | Script Tutorials</title> |
083 |
<link href= "css/main.css" rel= "stylesheet" type= "text/css" /> |
087 |
<h2>YouTube API - OAuth and Upload Example</h2> |
090 |
<img src= "ytb.png" class = "ytb" alt= "youtube" /> |
091 |
<?php if (! $sToken && $sAction != 'finish' ): ?> |
093 |
<h1>Step 1. OAuth</h1> |
094 |
<h2>Please click <a href= "<?= $sOAuthUrl ?>" >this link</a> in order to authorize your account at youtube</h2> |
097 |
<?php if ( $sToken && ! $sUploadToken ): ?> |
099 |
<h1>Step 2. Video info</h1> |
100 |
<h2>Now we should send video info to youtube in order to obtain proper upload token and url</h2> |
102 |
<form method= "post" action= "index.php" > |
103 |
<input type= "hidden" name= "token" value= "<?php echo($sToken) ?>" /> |
104 |
<input type= "hidden" name= "action" value= "prepare" /> |
105 |
<label for = "title" >Title:</label> |
106 |
<input type= "text" name= "title" value= "Sample title of your video" /> |
107 |
<label for = "description" >Description:</label> |
108 |
<input type= "text" name= "description" value= "Sample description of your video" /> |
109 |
<label for = "category" >Category:</label> |
110 |
<select name= "category" > |
111 |
<option value= "Autos" >Autos & Vehicles</option> |
112 |
<option value= "Comedy" >Comedy</option> |
113 |
<option value= "Education" >Education</option> |
114 |
<option value= "Entertainment" >Entertainment</option> |
115 |
<option value= "Film" >Film & Animation</option> |
116 |
<option value= "Games" >Gaming</option> |
117 |
<option value= "Howto" >Howto & Style</option> |
118 |
<option value= "Music" >Music</option> |
119 |
<option value= "News" >News & Politics</option> |
120 |
<option value= "Nonprofit" >Nonprofits & Activism</option> |
121 |
<option value= "People" >People & Blogs</option> |
122 |
<option value= "Pets" >Pets & Animals</option> |
123 |
<option value= "Tech" >Science & Technology</option> |
124 |
<option value= "Sports" >Sports</option> |
125 |
<option value= "Travel" >Travel & Events</option> |
127 |
<label for = "keywords" >Keywords:</label> |
128 |
<input type= "text" name= "keywords" value= "Keywords" /> |
129 |
<input type= "submit" name= "submit" value= "Continue" /> |
132 |
<?php if ( $sUploadUrl && $sUploadToken && $sNextUrl ): ?> |
134 |
<h1>Step 3. Upload file</h1> |
135 |
<h2>Now we should select video file and start upload</h2> |
137 |
<form method= "post" enctype= "multipart/form-data" action= "<?php echo $sUploadUrl . '?nexturl=' . $sNextUrl ?>" > |
138 |
<input type= "hidden" name= "token" value= "<?php echo($sUploadToken) ?>" /> |
139 |
<label for = "file" >Select file to upload:</label> |
140 |
<input type= "file" name= "file" size= "41" /> |
141 |
<input type= "submit" name= "submit" value= "Start upload" /> |
144 |
<?php if ( $iUploadStatus == 200 && $sVideoId ): ?> |
146 |
<h1>Step 4. Final</h1> |
149 |
<?php elseif ( $iUploadStatus ): ?> |
151 |
<h1>Step 4. Final</h1> |
152 |
<h2>Upload is failed. Error #<?php echo ( $iUploadStatus ) ?></h2> |
155 |
<?php if ( $sToken || $sVideoId ): ?> |
157 |
<center><h2>(<a href= "<?php echo $sWebPageUrl ?>" >Click here to start over</a>)</h2></center> |
Please pay attention, in the most beginning, you should create your own product in order to create your youtube developer key. So, please go to this page and make your own key, and after – put your key here: $sDevKey = ‘Your Youtube Developer Key’;
I hope that my code is easy enough. All the process is separated into three logical steps: get access token, send video info and receive upload token, and upload video file. That’s all.
Step 2. CSS
Now we need to stylize our page elements:
css/main.css
07 |
background-color : #ddd ; |
24 |
-moz-border-radius: 5px ; |
25 |
-ms-border-radius: 5px ; |
26 |
-o-border-radius: 5px ; |
27 |
-webkit-border-radius: 5px ; |
30 |
-moz-box-sizing: border-box; |
31 |
-webkit-box-sizing: border-box; |
32 |
-o-box-sizing: border-box; |
33 |
box-sizing: border-box; |
35 |
input[type=submit], input[type=file]{ |
Conclusion
And again, we have made our next really useful tutorial. Sure that this information will be useful for your own projects. Good luck in your work!