Created
September 9, 2019 11:24
-
-
Save morgyface/1fb29cb25712d2e04b18497bed365cba to your computer and use it in GitHub Desktop.
PHP | Vimeo oEmbed API | Thumbnail function
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 | |
| function vimeo_thumbnail( $video_embed, $referer_url, $width ) { | |
| if( strpos($video_embed, 'vimeo') !== false ) { | |
| preg_match('/src="(.+?)"/', $video_embed, $matches); // Finds the src element of the iframe | |
| $video_url = strtok($matches[1], '?'); // Removes all parameters from URL | |
| $curl = curl_init(); | |
| curl_setopt_array($curl, array( | |
| CURLOPT_URL => 'https://vimeo.com/api/oembed.json?url=' . $video_url . '&width=' . $width, | |
| CURLOPT_RETURNTRANSFER => true, | |
| CURLOPT_ENCODING => '', | |
| CURLOPT_MAXREDIRS => 10, | |
| CURLOPT_TIMEOUT => 30, | |
| CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
| CURLOPT_CUSTOMREQUEST => 'GET', | |
| CURLOPT_POSTFIELDS => '', | |
| CURLOPT_HTTPHEADER => array( | |
| 'Referer: ' . $referer_url | |
| ), | |
| )); | |
| $response = curl_exec($curl); | |
| $err = curl_error($curl); | |
| curl_close($curl); | |
| if ($err) { | |
| return '<p>cURL Error #:' . $err . '</p>'; | |
| // return false; | |
| } else { | |
| $response = json_decode($response, TRUE); | |
| $thumbnail_url = $response['thumbnail_url']; | |
| $thumbnail_id = substr($thumbnail_url, strrpos($thumbnail_url, '/') + 1); // Find the starting point of the image ID which is after the last forward slash | |
| $thumbnail_id = substr($thumbnail_id, 0, strrpos($thumbnail_id, '_')); // Now find the endpoint of the ID which is the underscore | |
| $thumbnail_width = $response['thumbnail_width']; | |
| $thumbnail_height = $response['thumbnail_height']; | |
| return '<img id="' . $thumbnail_id . '" src="' . $thumbnail_url . '" width="' . $thumbnail_width . '" height="' . $thumbnail_height . '">'; | |
| } | |
| } else { | |
| return false; | |
| } | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Vimeo thumbnails in PHP using cURL and JSON decode
Typical use:
Well this was a bit of a challenge. As far as I can tell this is the easiest way to grab the thumbnail attached to a vimeo video. The other options involve registering an app and generating an access token via developer.vimeo.com. If you go the app/developer route you can use
https://api.vimeo.com/videos/{video_id}/picturesto return a representation of all the thumbnails associated with the video, which would be less verbose and more focused.This function uses the oEmbed API which is more general and returns less specific video details.
video_embed
I've designed this to work with an Advanced Custom Fields (ACF) oEmbed field which returns the entire iFrame. So the function begins by extracting the video URL. If you're passing in the video URL you could remove lines 4 and 5 and change the variable names accordingly.
referer_url
This is the URL the request is coming from, if you're on a WordPress single post you can obtain the current URL using:
Note: When I ran this function locally, the referrer URL didn't cut the mustard. It worked fine on the dev server.
width
We also pass in a width, this means the API will return the image closest to this size. Without the width parameter it returns the default thumb which is typically pretty small.