vimeo 镜像_Vimeo API – OAuth和上传示例

本文介绍如何使用Vimeo API与OAuth进行视频上传,包括注册应用、获取密钥、上传视频及设置隐私等步骤。

vimeo 镜像

Vimeo API - OAuth and Upload Example
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.

Vimeo API – OAuth和上载示例今天,我想继续谈论视频。 上次我们谈论Youtube,但今天我决定选择Vimeo。 也许您是自己的视频网站的所有者,也许您正在考虑,但无论如何,我认为我们的信息对您有用。 如您所知,视频通常意味着您需要在托管帐户的硬盘上留出大量空间。 如果您将视频文件存储在自己的服务器上,则情况确实如此。 但是,您可以避免所有这些困难(视频存储和转换)–您可以尝试使用第三方视频托管。 我们的第二个示例– Vimeo。 在我们的新教程中,我将告诉您如何为您的网站创建Vimeo交叉上传器。

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.

为了实现我们的想法,我们将使用Vimeo Upload API 。 在开始的时候,我们应该在Vimeo的注册这里 。 之后,请打开此页面 。 现在,我们应该创建自己的应用程序(上传应用程序)。 请点击右侧的“创建新应用”按钮。 在这里,我们应该填写所有字段。 作为“应用程序URL”,您应使用结果应用程序URL;作为“应用程序回调URL”,您可以使用相同的URL(作为应用程序URL)。 结果–我们应该获得Vimeo的使用者密钥和秘密密钥。 我们将在项目中使用它们。 不要忘记发送请求到Vimeo以获得对上传功能的访问。 一旦获得它–您就可以继续。

现场演示

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

Now – download the source files and lets start coding !

现在–下载源文件并开始编码!

步骤1. PHP (Step 1. PHP)

Now, please create an empty index.php file and put next code:

现在,请创建一个空的index.php文件并放入下一个代码:

index.php (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="https://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>

<?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="https://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.

首先,我们应该附加“ vimeo.php”库,您可以在此处下载该库。 这是使用Vimeo的非常方便的库。 与Youtube相比,所有逻辑功能都更加简单。 如果是Vimeo,我们应该:(a)发送请求到vimeo网站以获得oauth_access_token和oauth_access_token_secret,(b)然后我们应该发送文件到vimeo文件本身(通过POST),以及文件的标题和描述(作为文本) )。 结果–我们的文件应该上传了。 所有这些代码的注释都很好,因此希望您在理解上没有困难。

步骤2. CSS (Step 2. CSS)

Now we can stylize our page elements:

现在我们可以样式化页面元素:

css / main.css (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;
}

.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;
}

现场演示

结论 (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!

今天,我们做了下一个真正有用的教程。 确保此信息对您自己的项目有用。 祝您工作顺利!

翻译自: https://www.script-tutorials.com/vimeo-api-oauth-and-upload-example/

vimeo 镜像

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值