
Vimeo API – OAuth and Upload Example
Today I would like to continue talking about video. Last time we talked about Youtube, but today I decided to select Vimeo. Maybe you are owner of your own video website, maybe you are thinking about it, but anyway I think that our information will be useful for you. As you know, video usually means that you need to have a lot of space at hard disk of your hosting account. And it is true in case if you store video files at your own server. But, you can avoid all these difficulties (video storing and conversion) – you can try to work with 3-rd party video hostings. Our second example – Vimeo. In our new tutorial I will tell you how you can create Vimeo cross-uploader for your website.
To achieve our idea we will use
Vimeo Upload API. In the beginning, we should register at Vimeo here. After, please open this page. Now, we shoud create our own application (upload application). Please click ‘Create a new app’ button at the right. Here we should fill all the fields. As ‘App URL’ – you should use your result application URL, as ‘App Callback URL’ – you can use the same url (as App URL). As result – we should obtain Vimeo’s Consumer Key and Secret keys. We are going to use them in our project. Don’t forget to send request to Vimeo in order to get access to upload feature. Once you get it – you can continue.
Live Demo
download in package
Now – download the source files and lets start coding !
Step 1. PHP
Now, please create an empty index.php file and put next code:
index.php
<?php
// prepare our Consumer Key and Secret
$consumer_key = 'CONSUMER_KEY';
$consumer_secret = 'CONSUMER_SECRET';
require_once('vimeo.php');
session_start();
$sUploadResult = '';
switch ($_REQUEST['action']) {
case 'clear': // Clear session
session_destroy();
session_start();
break;
case 'upload': // Upload video
$vimeo = new phpVimeo($consumer_key, $consumer_secret, $_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']);
$video_id = $vimeo->upload($_FILES['file']['tmp_name']);
if ($video_id) {
$sUploadResult = 'Your video has been uploaded and available <a href="http://vimeo.com/'.$video_id.'">here</a> !';
$vimeo->call('vimeo.videos.setPrivacy', array('privacy' => 'nobody', 'video_id' => $video_id));
$vimeo->call('vimeo.videos.setTitle', array('title' => $_POST['title'], 'video_id' => $video_id));
$vimeo->call('vimeo.videos.setDescription', array('description' => $_POST['description'], 'video_id' => $video_id));
} else {
$sUploadResult = 'Video Fails to Upload, try again later.';
}
break;
default:
// Create the object and enable caching
$vimeo = new phpVimeo($consumer_key, $consumer_secret);
$vimeo->enableCache(phpVimeo::CACHE_FILE, './cache', 300);
break;
}
// Setup initial variables
$state = $_SESSION['vimeo_state'];
$request_token = $_SESSION['oauth_request_token'];
$access_token = $_SESSION['oauth_access_token'];
// Coming back
if ($_REQUEST['oauth_token'] != NULL && $_SESSION['vimeo_state'] === 'start') {
$_SESSION['vimeo_state'] = $state = 'returned';
}
// If we have an access token, set it
if ($_SESSION['oauth_access_token'] != null) {
$vimeo->setToken($_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']);
}
$bUploadCase = false;
switch ($_SESSION['vimeo_state']) {
default:
// Get a new request token
$token = $vimeo->getRequestToken();
// Store it in the session
$_SESSION['oauth_request_token'] = $token['oauth_token'];
$_SESSION['oauth_request_token_secret'] = $token['oauth_token_secret'];
$_SESSION['vimeo_state'] = 'start';
// Build authorize link
$authorize_link = $vimeo->getAuthorizeUrl($token['oauth_token'], 'write');
break;
case 'returned':
// Store it
if ($_SESSION['oauth_access_token'] === NULL && $_SESSION['oauth_access_token_secret'] === NULL) {
// Exchange for an access token
$vimeo->setToken($_SESSION['oauth_request_token'], $_SESSION['oauth_request_token_secret']);
$token = $vimeo->getAccessToken($_REQUEST['oauth_verifier']);
// Store
$_SESSION['oauth_access_token'] = $token['oauth_token'];
$_SESSION['oauth_access_token_secret'] = $token['oauth_token_secret'];
$_SESSION['vimeo_state'] = 'done';
// Set the token
$vimeo->setToken($_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']);
}
// display upload videofile form
$bUploadCase = true;
break;
}
?>
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>Vimeo API - OAuth and Upload Example | Script Tutorials</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<h2>Vimeo API - OAuth and Upload Example</h2>
<a href="http://www.script-tutorials.com/vimeo-api-oauth-and-upload-example/" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
</header>
<img src="vim.png" class="vim" alt="vimeo" />
<?php if ($_SESSION['vimeo_state'] == 'start'): ?>
<center>
<h1>Step 1. OAuth</h1>
<h2>Click the link to go to Vimeo to authorize your account.</h2>
<p><a href="<?= $authorize_link ?>"><?php echo $authorize_link ?></a></p>
</center>
<?php endif ?>
<?php if ($bUploadCase && $sUploadResult == ''): ?>
<center>
<h1>Step 2. Video info</h1>
<h2>Now we should send video file, title and description to Vimeo</h2>
</center>
<form enctype="multipart/form-data" action="index.php" method="post">
<input type="hidden" name="action" value="upload" />
<label for="file">Please choose a file:</label><input name="file" type="file" />
<label for="title">Title:</label><input name="title" type="text" />
<label for="description">Description:</label><input name="description" type="text" />
<input type="submit" value="Upload" />
</form>
<?php endif ?>
<?php if ($sUploadResult): ?>
<center>
<h1>Step 4. Final</h1>
<h2><?php echo $sUploadResult ?></h2>
</center>
<?php endif ?>
<br /><center><h2>(<a href="?action=clear">Click here to start over</a>)</h2></center>
</body>
</html>
In the beginning – we should attach ‘vimeo.php’ library, you can download this library here. This is a very convenient library to work with Vimeo. All the logic functionality are more easy than in case of Youtube. In case of Vimeo we should: (a) send request to vimeo website in order to obtain oauth_access_token and oauth_access_token_secret, (b) then we should send to vimeo file itself (via POST), and also title and description of our file (as text). In the result – our file should be uploaded. All this code is commented very well, so I hope that you don’t have difficulties with understanding.
Step 2. CSS
Now we can stylize our page elements:
css/main.css
.vim {
display: block;
margin: 40px auto;
}
form {
background-color: #ddd;
display: block;
margin: 20px auto;
padding: 15px;
width: 400px;
}
label {
display: block;
margin-bottom: 5px;
}
input, select {
border-style: groove;
font-size: 16px;
height: 25px;
margin-bottom: 10px;
width: 400px;
/*css3 border radius*/
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
/* CSS3 Box sizing property */
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
-o-box-sizing: border-box;
box-sizing: border-box;
}
input[type=submit], input[type=file]{
cursor: pointer;
font-weight: bold;
height: 35px;
padding: 5px;
}
Live Demo
download in archive
Conclusion
Today we have made our next really useful tutorial. Sure that this information will be useful for your own projects. Good luck in your work!

It’s amazing but can we make something like “Live feed of uploaded videos” ???
Hello Adnan,
Yes, of course, in order to make our cross-uploader we used Advanced api (https://developer.vimeo.com/apis/advanced).
But the easiest way to get information about videos, users, groups, channels, albums, and activity is Simple API (https://developer.vimeo.com/apis/simple). You don’t need to register or authenticate your app to use the Simple API, but it’s limited to public data and is read-only.
Please check all the possible functions of Simple API to understand that you can.
This fails on the upload function for me every time from both the PC and from mobile… any ideas what it could be?
I’ve tried a 2 second video clip to cancel out any timeout issues.
Thanks
Hello Luke, The first thing you need to do is request upload access for your application. You can do so from your My Apps page (https://developer.vimeo.com/apps).
Awesome and extremely helpful, you sir deserve a pint!
Excuse me,
I run the demo, it can’t upload.
I don’t know whether it need to notice?
Thanks
Hi Elsa,
Maybe they deactivated our demo account?
have you tried to work with your own api keys?
Hi Andrew,
Thanks for your help.
I think my problem is the php.ini setting about the upload file size.
It can work.
Thanks
Hi Elsa again,
In case of php.ini you can increase your upload limits with:
upload_max_filesize = 20M
post_max_size = 20M
Or, in case of .htaccess:
php_value upload_max_filesize 20M
php_value post_max_size 20M
(20M means – 20 megabytes)
I trying, but something wrong.
1. Downloaded files
2. Upload to my site
3. Go by link in dem-page to authorization page in vimeo and make “Allow”
4. Vimeo say me: To start using “Test App”, go back to the application and enter the following code: widget-akhto
5. I going to my page and did not see place where need to enter this code. Nad did not see input place for select file for upload…
?
Hi Nikita,
As we mentioned in the beginning, you have to:
1. register at Vimeo …
2. create our own application (upload application)…
3. send request to Vimeo in order to get access to upload feature…
It is very good tutorial and working very fine but can I use this code in to a Symfony 2.1 application , if yes pls give me some code help .
Thanks in Advance !
Hello Sir,
Thank you for this tutorial. But can you be more specific or give us more detailed instructions? I am not a developer and I’d really want to have this feature implemented on my website.
Thank you very much
Hello Mauro,
What exactly do you want to know? Just ask