简体   繁体   中英

How can I upload a video from my file system to vimeo directly from the browser using the form-based approach in PHP

I have looked at the vimeo documentation on how to use their API. I was able to invoke their api successfully but am not able to upload a video. I have upload access and I need to upload a video to vimeo directly using the form approach. my problem is, how do I do the POST request to '/me/videos' and get the response parameters to use in the deployed form. below is my code:

<?php
include '../vendor/vimeo/vimeo-api/autoload.php';
include '../vendor/vimeo/vimeo-api/src/Vimeo/Vimeo.php';

//require_once 'VimeoClass.php';

use Vimeo\Vimeo

$clientid=clientid
$clientsecret=clientsecret
$access_token=access_token
//initialize the vimeo library
$lib = new \Vimeo\Vimeo($clientid, $clientsecret);

//set access token
$lib->setToken($access_token);



  

    $method='POST';

$url="/me/videos";

$headers= array('Authorization' =>'bearer'.$access_token ,'Content-Type'=>'application/json','Accept'=>'application/vnd.vimeo.*+json;version=3.4');

if(isset($_POST['btnvideo']) && $_SERVER['REQUEST_METHOD']=='POST'){
    try{
    //get video size
    $file_size=filesize(realpath($_FILES['vfile']['tmp_name']));
    //declare params to send to the request
    $params = array('upload' =>['approach'=>'post','size'=>$file_size,'redirect_url'=>'VideoRedirect.php']);
    $respond=$lib->request($url, $params, $method);

Thanks.

<form method="POST" id ="vimeo-form" action="upload.upload_link" enctype="multipart/form-data">
    <label for="file">File:</label>
    <input class="form-control" type="file" name="file_data" id="file" data-link=""><br>
    <input class="form-control" type="hidden" name="duration"><br>
    <input  type="submit" name="submit" value="Submit">
</form>

<?php
    require 'vendor/autoload.php';
    use Vimeo\Vimeo;
    $client_id = 'generated in created app in your vimeo account';
    $client_secret = 'generated in created app in your vimeo account';
    $client_token = 'generated in created app in your vimeo account';
    
    $client = new Vimeo($client_id, $client_secret, $client_token);
?>

<script type="text/javascript">
$.ajax({
        type: 'POST',
        url: 'https://api.vimeo.com/me/videos',
        upload: {
                approach: "post",
            },
        headers: {
             'Authorization': 'bearer ' + 'acces token',
             'Content-Type': 'application/json',
             'Accept': 'application/vnd.vimeo.*+json;version=3.4'
           },
        
        success: function(res){
            console.log(res);
        },
        error: function(err){
            console.log(err);
        }
    });
<script>

Try to use this I used AJAX request. It is a form-based approach based on the Vimeo API documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM