This article expands on a previous tutorial demonstrating a basic video application using Silex, Twig, and the Vimeo API. This time, we'll add video liking, watchlist functionality, and video uploads.
Key Enhancements:
Prerequisites:
Familiarize yourself with the previous tutorial (link or download provided in original). Ensure your development environment (Homestead Improved recommended) is set up and running.
Interacting with Videos (Liking & Watchlisting):
interact
scope:$scopes = array('public', 'private', 'interact'); $state = substr(str_shuffle(md5(time())), 0, 10); $_SESSION['state'] = $state; $url = $vimeo->buildAuthorizationEndpoint(REDIRECT_URI, $scopes, $state); $page_data = array('url' => $url);
templates/videos.php
, add like and watchlist buttons below the video description:<div> <button class="like" data-uri="{{ video.uri }}">Like</button> <button class="watch-later" data-uri="{{ video.uri }}">Watch Later</button> </div>
$('.like').click(function(){ let self = $(this); let uri = self.data('uri'); $.post('/tester/vimeo-slim/video/like', {'uri': uri}, function(response){ if(response.status == '204') self.prop('disabled', true); }); }); $('.watch-later').click(function(){ let self = $(this); let uri = self.data('uri'); $.post('/tester/vimeo-slim/video/watchlater', {'uri': uri}, function(response){ if(response.status == '204') self.prop('disabled', true); }); });
$app->post('/video/like', function () use ($app, $vimeo) { if($app->request->post('uri')){ $video_id = str_replace('/videos/', '', $app->request->post('uri')); $vimeo->setToken($_SESSION['user.access_token']); $response = $vimeo->request('/me/likes/' . $video_id, [], 'PUT'); $app->contentType('application/json'); echo json_encode(['status' => $response['status']]); } }); $app->post('/video/watchlater', function () use ($app, $vimeo) { //Similar to /video/like, but uses '/me/watchlater/' endpoint });
Uploading Videos:
upload
scope to your access tokens.templates/upload.php
:$scopes = array('public', 'private', 'interact'); $state = substr(str_shuffle(md5(time())), 0, 10); $_SESSION['state'] = $state; $url = $vimeo->buildAuthorizationEndpoint(REDIRECT_URI, $scopes, $state); $page_data = array('url' => $url);
<div> <button class="like" data-uri="{{ video.uri }}">Like</button> <button class="watch-later" data-uri="{{ video.uri }}">Watch Later</button> </div>
The above is the detailed content of Liking, Watchlisting and Uploading through Vimeo's API. For more information, please follow other related articles on the PHP Chinese website!