PHP Media API Class (YouTube, Flickr, Vimeo, Bitly)


/ Published in: PHP
Save to your folder(s)

Originally written by Jason Lengstorf, rewritten by Drew Douglass, packaged into the EnnuiCMS (link above), can be used as standalone class.

Be sure to change or define your constants for PAGE_OBJ_WIDTH and PAGE_OBJ_HEIGHT when using the YouTube method.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. class Media
  4. {
  5. static function latestVimeo($username)
  6. {
  7. // Load the RSS feed
  8. $xml = simplexml_load_file('http://vimeo.com/api/v2/'.$username.'/videos.xml');
  9.  
  10. // Get the first video's ID
  11. $id = $xml->video[0]->id;
  12.  
  13. // Output valid XHTML
  14. return "
  15. <object width=\"600\" height=\"338\"
  16. type=\"application/x-shockwave-flash\"
  17. data=\"http://vimeo.com/moogaloop.swf?clip_id=$id\">
  18. <param name=\"allowfullscreen\" value=\"true\" />
  19. <param name=\"allowscriptaccess\" value=\"always\" />
  20. <param name=\"movie\"
  21. value=\"http://vimeo.com/moogaloop.swf?clip_id=$id\" />
  22. </object>\n";
  23. }
  24.  
  25. /**
  26. * Rewrite of original latestYouTube method to allow multiple videos and more flexibility.
  27. *
  28. * @author Jason Lengstorf (original author, kudos to the might bearded one)
  29. * @author Drew Douglass (rewrite)
  30. * @param array $settings - Array with keys and values for options.
  31. * @param $settings["username"] - YouTube username - required! This is the only required array key.
  32. * @param $settings["class"] -The div class to wrap each video in, default is "youtube".
  33. * @param $settings["rel"] - Toggle on or off related videos when video ends, defaults to 0.
  34. * @param $settings["count"] - Amount of videos to include, defaults to 1.
  35. * @param $settings["color1"] - Color one setting, defaults to 0x000000
  36. * @param $settings["color2"] - Color two setting, defaults to 0x0000FF
  37. * @param $settings["border"] - The border int setting, defaults to 0.
  38. * @param $settings["showsearch"] - Show or hide search, defaults to 0.
  39. * @param $settings["showinfo"] - Show or hide info, defaults to 0.
  40. * @param $settings["fs"] - Fullscreen toggle, defaults to 0.
  41. * @param $settings["autoplay"] - Toggles auto play on or off, defaults to 0.
  42. * @param $settings["start"] -Number of seconds into the video to start at, defaults to 0.
  43. * @param $settings["hd"] -Enable HD if available, defaults to 0.
  44. * @return str (containing markup) | bool (on failure)
  45. *
  46. * NOTE: The width and height are set in the config/config.inc.php file!
  47. *
  48. * "Basic" Usage Example:
  49. <?php
  50. $video_opts = array("username" => "BritneySpearsVEVO");
  51. echo Media::latestYouTube($video_opts);
  52. ?>
  53. *
  54. *
  55. *
  56. *
  57. * "Advanced" Usage Example:
  58. <?php
  59. $video_opts = array(
  60. "username" => "BritneySpearsVEVO",
  61. "count" => 5,
  62. "rel" => 1,
  63. "fs" => 1,
  64. "class" => "my-videos"
  65. );
  66. echo Media::latestYouTube($video_opts);
  67. ?>
  68. *
  69. *
  70. *
  71. */
  72. public static function latestYouTube($settings = array())
  73. {
  74. //Check to ensure username was passed.
  75. if ( array_key_exists("username", $settings) )
  76. {
  77. //Set defaults as variables
  78. //We're setting them individually here as to easily overwrite them below in one loop.
  79. $class = "youtube";
  80. $rel = 0;
  81. $count = 1;
  82. $color1 = "0x000000";
  83. $color2 = "0x0000FF";
  84. $border = 0;
  85. $showsearch = 0;
  86. $showinfo = 0;
  87. $fs = 0;
  88. $autoplay = 0;
  89. $start = 0;
  90. $hd = 0;
  91.  
  92. //Loop through and set any keys passed as variables, overwriting defaults only as needed.
  93. foreach ($settings as $key => $value)
  94. {
  95. $$key = $value;
  96. }
  97.  
  98. //Ensure count is greater than 0.
  99. $count = ((int)$count) <= 0 ? 1 : $count;
  100.  
  101. // Load the RSS feed
  102. $xml = simplexml_load_file("http://www.youtube.com/rss/user/$username/videos.rss");
  103.  
  104. //Variable holding final markup
  105. $videos = "";
  106. //Increment counter
  107. $i = 0;
  108.  
  109. //Pull out each link and snip off the arguments
  110. //Create the markup with user options
  111. foreach($xml->channel->item as $key => $value)
  112. {
  113. //Check to see if we reached our count limit
  114. if($i >= $count){break;}
  115.  
  116. //Grab the video argument string and discard the rest.
  117. $id = str_replace("http://www.youtube.com/watch?v=", "", $value->link);
  118. $url_args = "?rel=$rel&count=$count&color1=$color1&color2=$color2&border=$border&showsearch=$showsearch"
  119. ."&showinfo=$showinfo&fs=$fs&autoplay=$autoplay&start=$start&hd=$hd";
  120.  
  121. //Build markup
  122. $videos .= "<div class=".$class.">"
  123. . "<object width=".PAGE_OBJ_WIDTH." height=".PAGE_OBJ_HEIGHT."
  124. type=\"application/x-shockwave-flash\"
  125. data=\"http://www.youtube.com/v/$id$url_args\">
  126. <param name=\"allowfullscreen\" value=".($fs = 1 ? "true" : "false")." />
  127. <param name=\"allowscriptaccess\" value=\"always\" />
  128. <param name=\"movie\"
  129. value=\"http://www.youtube.com/v/$id$url_args\" />
  130. </object>\n"
  131. ."</div><!--End".$class."-->";
  132.  
  133. //Increment the counter
  134. $i++;
  135. }
  136. return $videos;
  137.  
  138. }
  139. //No username passed, we're done here.
  140. return false;
  141. }
  142.  
  143. static function loadFlickr($username, $class=NULL)
  144. {
  145. $feed = "http://api.flickr.com/services/feeds/photos_public.gne?id="
  146. . $username . "&lang=en-us&format=rss_200";
  147. $link = "http://flickr.com/$username";
  148. $rss = simplexml_load_file($feed);
  149. $photodisp = "\n\t<ul class=\"$class\">\n";
  150. foreach ($rss->channel->item as $item) {
  151. $title = $item->title;
  152. $media = $item->children('http://search.yahoo.com/mrss/');
  153. $image = $media->thumbnail->attributes();
  154. $url = str_replace('_s', '', $image['url']);
  155.  
  156. $photodisp .= <<<________________EOD
  157.  
  158. <li>
  159. <img src="$url"
  160. title="$title"
  161. alt="$title"
  162. style="border:0;" />
  163. </li>
  164. ________________EOD;
  165. }
  166.  
  167. return $photodisp . "\n\t</ul>\n<a href=\"$link\">View on Flickr</a>";
  168. }
  169.  
  170. /**
  171. * Returns a shortened bit.ly link given a "long" anchor.
  172. * Attempts to use cURL before file_get_contents for performance reasons, see results here:
  173. * http://stackoverflow.com/questions/555523/file-get-contents-vs-curl-what-has-better-performance
  174. *
  175. * @author - Drew Douglass
  176. * @param str $username - The bitly username to use.
  177. * @param str $key - The api key attached to the username.
  178. * @param str $link - The link to shorten, no HTML (i.e. http://google.com)
  179. * @param [optional] int $timeout - The time in seconds until timeout, if performance is a major concern stick with 2 or less.
  180. * @return str The shortened link
  181. *
  182. * Example usage: <?php echo Media::bitlyShorten("yourBitlyUsername","YOURAPIKEY","http://google.com"); ?>
  183. */
  184. public static function bitlyShorten($username, $key, $link, $timeout = 2)
  185. {
  186. // Generate the URL to send to the bit.ly API
  187. $url = "http://api.bit.ly/shorten?version=2.0.1&login=$username"
  188. . "&apiKey=$key&longUrl=$link";
  189.  
  190. //Attempt to use cURL first as it is very fast.
  191. if ( in_array("curl", get_loaded_extensions()) )
  192. {
  193. $ch = curl_init($url);
  194. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  195. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  196. $short_link = json_decode(curl_exec($ch),true);
  197.  
  198. if ($short_link["errorCode"] === 0) {
  199. return $short_link["results"][$link]["shortUrl"];
  200. }
  201. //Else an error was present when using the API
  202. return false;
  203. }
  204.  
  205. //cURL not available, try file_get_contents (slight performance hit)
  206. elseif ( function_exists("file_get_contents") )
  207. {
  208. $short_link = file_get_contents($url);
  209. $short_link = json_decode($short_link,true);
  210. if ($short_link["errorCode"] === 0) {
  211. return $short_link["results"][$link]["shortUrl"];
  212. }
  213. //Else an error was present when using the API
  214. return false;
  215. }
  216.  
  217. else
  218. {
  219. return false;
  220. }
  221. }
  222. }
  223.  
  224. ?>

URL: http://github.com/jasonatennui/Ennui-CMS

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.