pepebe
9/4/2014 - 11:22 AM

MODX Snippets for Vimeo Intergration

MODX Snippets for Vimeo Intergration

<?php
/*
http://forums.modx.com/thread/13967/solved-snippets-for-integrating-vimeo?page=3#dis-post-76833

OVERVIEW
In order to make use of the Vimeo Advanced API from our own website, we need four OAuth Tokens:
(1) a CONSUMER KEY; (2) a CONSUMER SECRET; (3) an OAUTH ACCESS TOKEN; and (4) an OAUTH ACCESS TOKEN SECRET.

We're going to:
(A) Install the Vimeo API on our modx site.
(B) Register our site with Vimeo and get our Consumer Keys.
(C) Authorize our site and get our OAuth Tokens.
(D) Start using the Advanced API for our Vimeo Gallery!


PART A - INSTALL THE VIMEO API LIBRARY ON YOUR SITE

1. Download the "Advanced API PHP Library" from this page: http://www.vimeo.com/api/docs/downloads
2. From that zip, upload "[tt]vimeo.php[/tt]" and "[tt]index.php[/tt]" to a folder called "[tt]vimeo[/tt]" on the root of your website (i.e., so it's in the same folder as your [tt]"assets"[/tt] and [tt]"manager"[/tt] folders.)
3. Create a folder within "[tt]vimeo[/tt]" called "cache" with permissions 777.
4. You need the full absolute server path to [tt]vimeo.php[/tt]. For example, on DreamHost, my absolute path would be [tt]/home/danzg/mycoolsite.com/vimeo/vimeo.php[/tt]. 

PART B - REGISTER YOUR SITE AS AN API APPLICATION ON VIMEO

5. Login to your vimeo account, and register your website as a new "app" at http://www.vimeo.com/api/applications/new
6. Use the URL of your website as the "Application URL".
7. IMPORTANT: Enter the URL to the [tt]index.php[/tt] you just uploaded as the "Application Callback URL".
   (In my example, this would be "[tt]http://mycoolsite.com/vimeo/index.php[/tt]")
   If it's your first time registering an app, you MAY have to wait for a human to authorize the request.
8. Get your Consumer Key and Consumer Secret and SAVE THESE TWO CODES. YOU WILL NEED THEM.

PART C - AUTHORIZE YOUR SITE

Now you need to do a 1-time authorization of your site.
9. Edit line 6 of [tt]vimeo/index.php[/tt] (on your site) and put in the values you just obtained.

*/
$vimeo = new phpVimeo('CONSUMER_KEY', 'CONSUMER_SECRET');
/*
   should become something like:
*/
$vimeo = new phpVimeo('3bab4a234d4c0e345df678ac669548ca', 'a9a44ed654e123a');
/*

10. Around line 60 of [tt]vimeo/index.php[/tt], there is a comment "[tt]// Set the token[/tt]". The next line is:

*/
$vimeo->setToken($_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']);

/*
   insert these lines right after it:
*/
echo "
";
echo "Your oauth access token: ";
echo $_SESSION['oauth_access_token'];
echo "
";           
echo "Your oath access token secret: ";
echo $_SESSION['oauth_access_token_secret'];
echo "
";
/*
11. Go to your browser and open up your [tt]vimeo/index.php[/tt] page, e.g., I would go to [tt]http://mycoolsite.com/vimeo/index.php[/tt]
   At the bottom it will say:
   "Click the link to go to Vimeo to authorize your account."
   Go ahead and click that long nasty link (which should contain an OAuth token).

12. This will take you to Vimeo, where you may need to sign in.
   You should see:
   "Do you want to authorize this app? ... Yes, authorize ...  "

   Go ahead and authorize, and, if you entered your callback URL correctly, it should take you back to your index.php, where, at the top, you will see
   [tt]Your oauth access token: xxxxxxxxxxxxx
   Your oath access token secret: xxxxxxxxxxxxxxxxx
   [/tt]
   SAVE THESE TWO CODES. YOU WILL NEED THEM.

Congratulations!

You have now authorized your website for full access to your vimeo account, and you can now use all the cool methods in the API, which you can peruse here:
http://vimeo.com/api/docs/methods

13. DO NOT LOAD THE [tt]vimeo/index.php[/tt] PAGE AGAIN, or I believe you will end up re-setting your tokens. Might want to just delete it. You should only have to do the above steps once.

14. DO NOT DELETE THE [tt]vimeo/vimeo.php[/tt] FILE -- that is needed by any PHP code / snippets that call the Vimeo API.


Now, any time you want to call the Advanced API, your PHP will start with these 3 lines:
*/

require_once('/absolute/path/to/vimeo.php');
$vimeo = new phpVimeo('YourConsumerKey', 'YourConsumerSecret');
$vimeo->setToken('YourOAuthAccessToken','YourOAuthAccessTokenSecret');
<?php
/* 
  VimeoThumbURL will return the thumbnail for a given Vimeo Video ID
  [[VimeoThumbURL?albumID=`VimeoAlbumID`]] 
*/
$vdata = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$VimeoVideoID.php"));
return $vdata[0]['thumbnail_medium'];
<?php
/*
  VimeoMostRecent will return the id for the most recent video from a given album.
  (You can change ’id’ to ’url’ if you want to retrieve the link to the video)
  [[VimeoMostRecent?albumID=`VimeoAlbumID`]] 
*/
$albumData = unserialize(file_get_contents("http://vimeo.com/api/v2/album/$albumID/videos.php"));
return $albumData[0]['id'];
<?php
/* 
  VimeoAlbum will pull in an album from Vimeo, complete with links, thumbnails, descriptions, etc.
  
  Source: http://forums.modx.com/thread/13967/solved-snippets-for-integrating-vimeo#dis-post-76815
  Author: http://forums.modx.com/u/danzg
  
  CAVEAT: This will only return the first 20 videos in a given album, 
  and will not return any videos whose privacy is hidden!
  [[VimeoAlbum?albumID=`VimeoAlbumID`]] 
*/
require_once('/absolute/path/to/vimeo.php');
$vimeo = new phpVimeo('YourConsumerKey', 'YourConsumerSecret');
$vimeo->setToken('YourOAuthAccessToken','YourOAuthAccessTokenSecret');
 
$output = '<div class="albumGallery">';
$result = $vimeo->call('vimeo.albums.getVideos', array('album_id' => $albumID,full_response => '1'));
$videos = $result->videos->video;
foreach ($videos as $video) {
   $output .= ' <a href="'.$video->urls->url[0]->_content.'" title="'.$video->title.'" rel="zoombox[group]">';
   $output .= '  <div class="albumGalleryItem">';
   $output .= '   <div class="ImgContainer"><img src="'.$video->thumbnails->thumbnail[1]->_content.'"></div>';
   $output .= '   <div class="albumGalleryCaption">'.$video->title.'</div>';
   $output .= '   <div class="albumGalleryDescription">'.nl2br($video->description).'</div>';
   $output .= '  </div>';
   $output .= ' </a>';
}
 
$output .= '</div>';
 
return $output;