• Reading time:8 mins read
Vimeo API - OAuth and Upload Example

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

[sociallocker]

download in package

[/sociallocker]


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 | Dev School</title>
        <link href="css/main.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <header>
            <h2>Vimeo API - OAuth and Upload Example</h2>
            <a href="https://dev-school.net/vimeo-api-oauth-and-upload-example/" class="stuts">Back to original tutorial on <span>Dev School</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

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!