Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get Thumbnail of Private Vimeo Videos WordPress

  • SOLVED

I was able to get thumbnail for normal videos. But getting error for private videos.

I need a proper function to get thumbnails in videos that is set to mark as private on vimeo.

The videos are embeddable on my website just getting errors on thumbnail

Example Video: https://vimeo.com/220234391/19891dd1af

The code I for free video:
http://www.telegraphicsinc.com/2013/03/using-a-wordpress-function-to-get-vimeo-thumbnails/
https://ilikekillnerds.com/2012/03/a-php-function-to-get-vimeo-video-thumbnails-easily/
// Get And Cache Vimeo Thumbnails
function get_vimeo_thumb($vURL, $size = 'thumbnail_small') {
$pieces = explode("/", $vURL);
$id = end($pieces);

if(get_transient('vimeo_' . $size . '_' . $id)) {
$thumb_image = get_transient('vimeo_' . $size . '_' . $id);
} else {
$json = json_decode(file_get_contents( "http://vimeo.com/api/v2/video/" . $id . ".json" ));
$thumb_image = $json[0]->$size;
set_transient('vimeo_' . $size . '_' . $id, $thumb_image, 2629743);
}
return $thumb_image;
}

But vimeo said I need add auth, key, secret in head see here https://vimeo.com/forums/api/topic:283660
https://github.com/vimeo/vimeo.php

Anyone how to improve this function add authentication information so I can private video thumbs.

Answers (3)

2017-06-08

Luis Abarca answers:

Hi, you should use Vimeo API to get private videos.

Take a look https://gist.github.com/luisabarca/b6739bf5d5bf3e6c8039851a693a70b0


// file: path-to-get-token.php or inside a plugin
<?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'];



Jihan Ahmed comments:

Can you help me in implementing this I am getting fatal error http://dev-easywp.pantheonsite.io/videos/
I will increase it to $20.


Jihan Ahmed comments:

What's your email or skype so I can send you login info. Even if you are confident I can set it to live domain.

Do we need the compete it library of vimeo.php or part of it.


Luis Abarca comments:

Sure, you have to download vimeo for PHP from github on a folder inside your theme files


Luis Abarca comments:

[email protected] / luis_abarcag


Jihan Ahmed comments:

added on skype. I have added the vimeo.php already you can see on files.

2017-06-08

Reigel Gallarde answers:

hello, can you give me a sample url of a private video? I want to test it.


Reigel Gallarde comments:

also, you said that the videos are embeddable on your website, can you give me the code of the embed...


Jihan Ahmed comments:

http://peterliljensten.dk/videos/intro-peter-liljensten-online-academy/


Jihan Ahmed comments:

https://vimeo.com/220234391/19891dd1af
Directly embeddable in WordPress Though I used oembed function

Code 1:<div class="video-excerpt">
<?php
$video_id = get_post_meta( get_the_ID(), '_video_id', true );
if ( ! empty( $video_id ) ) {
$video_thumb = get_video_thumbnail();
$video_desc = get_post_meta( get_the_ID(), '_video_description', true );

echo '<a rel="wp-video-lightbox" href="'.$video_id.'?width=auto&amp;height=auto" title="'.$video_desc.'"><img src="'.$video_thumb.'" class="video_lightbox_anchor_image" alt="Peter Liljensten"><img class="cs-icon" src="'.get_stylesheet_directory_uri().'/img/s-v-icon.gif" width="490" height="138" alt="Video Icon"></a>';
}
?>
</div>

Code 2:
<?php
$video_id = get_post_meta( get_the_ID(), '_video_id', true );
if ( ! empty( $video_id ) ) {
$video_embed_code = wp_oembed_get(''.$video_id.'');
echo '<div class="iframe-flexible-container">';
echo $video_embed_code;
echo '</div>';
}
?>


Reigel Gallarde comments:

Well, I'm not really sure how you would use this, but here's what I come up with...

I have two functions here: https://gist.github.com/reigelgallarde/ab32da490fae0427177179e6ea81b61d
(for readability)

get_vimeo_thumb function will return an array something like this with var_dump:

array(4) {
["1280"]=> string(47) "https://i.vimeocdn.com/video/638188038_1280.jpg"
["960"]=> string(46) "https://i.vimeocdn.com/video/638188038_960.jpg"
["640"]=> string(46) "https://i.vimeocdn.com/video/638188038_640.jpg"
["base"]=> string(38) "https://i.vimeocdn.com/video/638188038"
}


you can use it something like this

$id = '220234391';
$img = get_vimeo_thumb($id);
echo '$img = get_vimeo_thumb(' . $id . ');';
echo '<br>';
var_dump($img);

foreach ( $img as $key => $img_url ){
echo '<br>';
echo '<br>';
echo '<br>$img[' . $key . '] : ';
echo $img_url;
echo '<br><img src="' . $img_url . '" >';
}

sample output
http://demo.reigelgallarde.me/vimeo/test.php?id=220234391


Jihan Ahmed comments:

It's working like charm. Thanks already.
Few Questions: 1. How can I target specific thumb I see they are all in array.
2. I see you are getting code from player bypassing authentication. So, If embed this link in custom field within wp https://vimeo.com/220234391/19891dd1af I see no way to get only ID 220234391. Hence I will stick to your solution and use Iframes for embedding.

I will ask the client to put https://vimeo.com/220234391 only. And implode last part

Now I ask you: How do I target specific img size?
Can you add the explode and cache image from this function to your function so we will get from this url https://vimeo.com/220234391/

That's all. Thanks again.


Jihan Ahmed comments:

I need to get on thumb at a time. I think base thumb 640 will work for me. So, just give me a clean code for that. I assume we can bypass foreach loop. Do we need get_string_between function?

Thanks !


Jihan Ahmed comments:

Sorry we have got a problem.... the function does not work with most of videos. Can give these ID's a try plz
https://vimeo.com/220334725/7b3d75721a
https://vimeo.com/220292680/ad2a1430da
https://vimeo.com/220265605/d7e5462473
https://vimeo.com/220234391/19891dd1af
https://vimeo.com/220213161/10e0b2662d
https://vimeo.com/220189050/813d829749


Reigel Gallarde comments:

I'm editing the demo page... so it's not working right now.


Jihan Ahmed comments:

No. as you see this videos are marked as private links and embed anywhere. This is privacy setting. I have the auth keys if you need.


Jihan Ahmed comments:

Hi, Were you able to produce any result plz?

2017-06-08

Cesar Contreras answers:

You need the API Key to get the information of the private videos.


Cesar Contreras comments:

It would be fair to pay the same as you offer on this site:

https://www.truelancer.com/freelance-project/get-thumbnail-of-private-vimeo-videos-in-wordpress-47853


Jihan Ahmed comments:

I will.


Jihan Ahmed comments:

If anyone can give me working code.


Cesar Contreras comments:

Of course. Sorry if you bothered with that but I saw your other post and it is unfair to pay less here as it is the same problem.

I have not used the Vimeo API, so I can not give you the solution ): I just know that for the private videos of uses the API and its Key.

Luck with your solution.