Apple Movie Trailer Wordpress Widget

erich February 13th, 2007

I found another great widget, which displays a random Apple Movie trailer, complete with movie poster.

Sadly, I ran into exactly the same problem that I did with the APOD widget - use of url file access functions is strictly verboten on my web host. So, here is the modded version of the widget to use cURL:

PHP:
  1. <?php
  2. /*
  3. Plugin Name: Apple Trailer Sidebar Widget
  4. Description: Adds a sidebar widget that shows the latest movie trailers from apple.com
  5. Author: Patrick Queisler
  6. Version: 1.1
  7. Author URI: http://www.sosuechtig.de
  8. Version 1.1 - Erich Beyrent [http://www.beyrent.net]: added cURL functions to fetch the data from Apple
  9. */
  10.  
  11. function widget_appletrailer_init() {
  12.  
  13.     if ( !function_exists('register_sidebar_widget') )
  14.         return;
  15.        
  16.     function widget_appletrailer($args) {
  17.        
  18.         extract($args);
  19.         $options = get_option('widget_appletrailer');
  20.         $title = $options['title'];
  21.        
  22.         echo $before_widget . $before_title . $title . $after_title;
  23.         function appledotcom_parser(){
  24.             $myfile = '';
  25.             $lines = @file("http://wdirect.apple.com/trailers/home/externalA.js");
  26.             if($lines)
  27.             {
  28.                 foreach ($lines as $line_num=> $line)
  29.                 {
  30.                     $myfile .= stripslashes($line);
  31.                 }
  32.             }
  33.             else
  34.             {
  35.                 $ch = curl_init();
  36.                 curl_setopt($ch, CURLOPT_URL, "http://wdirect.apple.com/trailers/home/externalA.js");
  37.                 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  38.                 $myfile = curl_exec($ch);
  39.                 curl_close ($ch);
  40.             }   
  41.            
  42.             $out = eregi_replace("href=\"/trailers/","target=\"_blank\" href=\"http://www.apple.com/trailers/",$myfile);
  43.             return $out;
  44.         }
  45.         function get_trailer(){
  46.             echo "<center><script language=\"JavaScript\" type=\"text/javascript\">";
  47.             echo appledotcom_parser();
  48.             echo "loadRandomD();
  49.             </script></center>";
  50.         }
  51.         get_trailer();
  52.         echo $after_widget;
  53.     }
  54.  
  55.  
  56.     function widget_appletrailer_control() {
  57.  
  58.         $options = get_option('widget_appletrailer');
  59.         if ( !is_array($options) )
  60.             $options = array('title'=>'');
  61.         if ( $_POST['appletrailer-submit'] ) {
  62.  
  63.             $options['title'] = strip_tags(stripslashes($_POST['appletrailer-title']));
  64.             update_option('widget_appletrailer', $options);
  65.         }
  66.  
  67.         $title = htmlspecialchars($options['title'], ENT_QUOTES);
  68.        
  69.         echo '<p style="text-align:right;"><label for="appletrailer-title">Title: <input style="width: 200px;" id="appletrailer-title" name="appletrailer-title" type="text" value="'.$title.'" /></label></p>';
  70.         echo '<input type="hidden" id="appletrailer-submit" name="appletrailer-submit" value="1" />';
  71.     }
  72.    
  73.     register_sidebar_widget('Apple Trailer', 'widget_appletrailer');
  74.     register_widget_control('Apple Trailer', 'widget_appletrailer_control', 300, 100);
  75. }
  76.  
  77. add_action('plugins_loaded', 'widget_appletrailer_init');
  78.  
  79. ?>

tags: , , , , , , , , ,

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.