Search code examples
phpapicurlvimeo

Vimeo API help (cURL or Library)?


Background: Vimeo PRO account (owner of videos) Both public and private videos Only embeddable on my domains

Goal: I simple want to pass in a VIDEO ID, and get a JSON data dump response back.. (so I can use this in an embeddable iframe/player.. BUT ALSO be able to display the description from the video, as well as the video download links... this all seems to be available in the JSON returned data)

I have tried a simple cURL approach..

//project vars 
$client_id = 'xxx';
$client_secret = 'xxx';
$access_token = 'xxx';

$video_endpoint = 'https://api.vimeo.com/videos/';
$video_url = '171811266';
//$video_url = '171811266/5822169b48';


$json_url = $video_endpoint . '.json?url=' . rawurlencode($video_url);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $json_url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
        "authorization: Bearer ".$access_token
      ),
    ));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
}else {
    echo $response;
}

And while I -thought- I was on the right track (even though its an OLD approach)..

I get this error:

{"error":"The requested video could not be found"}

Not sure what I could be doing wrong? Video link: https://vimeo.com/171811266

  • I believe I set this video to be PUBLIC for testing..etc

Since I couldnt get that approach to work.. I moved on to attempt to use an 'official library':

https://github.com/vimeo/vimeo.php

However.. IMHO, it does not have any 'working examples' out of the box..

Here is what I tried so far:

*I am not clear why I need to 'authenticate' for videos "I" own.. on a Vimeo account "I" own.. and can only be displayed on domains "I" own?

  • I not clear why I need to send a user to the Vimeo site to 'accept/approve' ANYTHING? and then redirect them back... to where? The page where the video is housed?

    //project vars 
    
    
    $client_id = 'xxxx';
    $client_secret = 'xxx';
    $access_token = 'xxx';
    
    $redirect_uri = 'http://www.domain.com/file.php'; //what URL should this be?  The .php page that has the embedded player on it? 
    
    // scope is an array of permissions your token needs to access. You can read more at https://developer.vimeo.com/api/authentication#scopes
    $scopes = Array('public', 'private');
    $state = 'Ldhg0478y'; //randomly made up state variable/value
    
    require("Vimeo/autoload.php");
    
    
    // instantiate a new instance of Vimeo class/object
    $lib = new \Vimeo\Vimeo($client_id, $client_secret);
    
    // build a link to Vimeo so your users can authorize your app. //whatever that means and is for?
    $url = $lib->buildAuthorizationEndpoint($redirect_uri, $scopes, $state);
    // redirect_uri must be provided, and must match your configured uri
    $token = $lib->accessToken(code, redirect_uri);
    // usable access token
    var_dump($token['body']['access_token']);
    // accepted scopes
    var_dump($token['body']['scope']);
    // use the token
    $lib->setToken($token['body']['access_token']);
    
    
    $response = $lib->request('/me/videos', array('per_page' => 2), 'GET');
    var_dump($response['body']);
    

My response from the above is:

Notice: Use of undefined constant code - assumed 'code' in /usr/www/users/domain_name.org/file_name.php on line 27

Notice: Use of undefined constant redirect_uri - assumed 'redirect_uri' in /usr/www/users/domain_name.org/file_name.php on line 27

Notice: Undefined index: access_token in /usr/www/users/domain_name.org/file_name.php on line 30
NULL
Notice: Undefined index: scope in /usr/www/users/domain_name.org/file_name.php on line 33
NULL
Notice: Undefined index: access_token in /usr/www/users/domain_name.org/file_name.php on line 36

array(1) { ["error"]=> string(52) "You must provide a valid authenticated access token." } 

I think I'd feel more comfortable and have more control (I think?) if I could get the cURL version working? Instead of trying to use this Vimeo library? (Theres just too many files and random lines to make sense for me at this point to understand what I need)

I dont need to upload anything.. I just need to/want to pass a video ID and get back a list of JSON data for that video.. (dont need USER info... dont need to UPLOAD.. dont need to DELETE)

Can anyone guide me or give me some direction on what I am doing wrong? and how to correct it?

Thanks

========================================================================= Update: "What worked for me!"

Using the official library.. the readme 'example' just didnt work for me (as I posted)..

However.. this did work for me: (again, using the official library)

<?
//include offifial library
require("Vimeo/autoload.php");
$client_id = 'xxx';
$client_secret = 'xxx';
$access_token = 'xxx';
$video_id = 'xxx'; 
$lib = new Vimeo\Vimeo($client_id, $client_secret, $access_token);
$video_response = $lib->request('/videos/'.$video_id);

//dont really need this, but included in case there is some data you need to display
$token_response = $lib->clientCredentials();

//example of parsing out specific data from the array returned
//name/title
echo $video_response['body']['name'] . '<br><br>';


?>

Solution

  • Where are you getting the .json?url= part? Your request url should be just

    $json_url = $video_endpoint . rawurlencode($video_url);
    

    Your code works with that change.

    Reference: https://developer.vimeo.com/api/endpoints/videos#/{video_id}