Last active
April 20, 2020 02:15
-
-
Save luisabarca/b6739bf5d5bf3e6c8039851a693a70b0 to your computer and use it in GitHub Desktop.
Get a thumbnail from Vimeo private videos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// You need Vimeo API https://github.com/vimeo/vimeo.php | |
require 'path-to-your-lib/vimeo/vimeo.php'; | |
// Get this from your account | |
$vimeo_client_id = 'xxxxx'; | |
$vimeo_client_secret = 'xxxxxxxxxxx'; | |
// This has to be generated on your site, plugin or theme | |
$vimeo_token = 'xxxxxx'; | |
$callback_url = 'http://yourdomain/path-to-get-token.php'; | |
// If you don't have a token yet | |
if ( ! empty( $vimeo_client_id ) && ! empty( $vimeo_client_secret ) ) { | |
// Create vimeo API object with data | |
$vimeo = new Vimeo( $vimeo_client_id, $vimeo_client_secret ); | |
$_SESSION['state'] = $state = base64_encode(openssl_random_pseudo_bytes(30)); | |
// Create URL for special link call and our callback url | |
$vimeo_oauth_url = $vimeo->buildAuthorizationEndpoint($callback_url, 'private', $state); | |
echo '<a href="' . $vimeo_oauth_url . '">Authenticate on Vimeo</a>'; | |
} | |
/* | |
* Handle Vimeo response for token | |
* | |
*/ | |
if ( isset($_GET['code']) && ! empty($_GET['code']) ) { | |
$code = $_GET['code']; | |
$state = $_GET['state']; | |
$vimeo_rquest = $vimeo->accessToken($code, $callback_url); | |
if ($vimeo_rquest['status'] == 200) { | |
/* | |
* Vimeo API token | |
* Save it on database or transient | |
*/ | |
$vimeo_token = $vimeo_rquest['body']['access_token']; | |
// Will save it on this session for now | |
$_SESSION['access_token'] = $vimeo_token; | |
} else { | |
echo "Unsuccessful authentication"; | |
} | |
} | |
$video_id = '12345'; | |
$vimeo = new Vimeo( $vimeo_client_id, $vimeo_client_secret ); | |
$vimeo->setToken( $vimeo_token ); | |
$result = $vimeo->request('/videos/' . $video_id); | |
// Video width and height | |
$video_w = $result['body']['width']; | |
$video_h = $result['body']['height']; | |
// Data for thumbnail with index 1 | |
$thumb_data = $result['body']['pictures'][1]; | |
// This is your thumbnail URL | |
$thumb_url = $thumb_data['link']; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment