Forked from luisabarca/get-vimeo-private-video-thumb.php
Created
April 17, 2020 20:44
-
-
Save jonabaptistella/5b9b2357a7c62dc09b6ed632f4c8e67a to your computer and use it in GitHub Desktop.
Get a thumbnail from Vimeo private videos
This file contains 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