home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Services / YouTube.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  13.2 KB  |  443 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * PHP Client for YouTube API
  6.  *
  7.  * PHP versions 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  *
  16.  * @category   Services
  17.  * @package    Services_YouTube
  18.  * @author     Shin Ohno <ganchiku@gmail.com>
  19.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  20.  * @version    0.2.0 $Id: YouTube.php,v 1.7 2006/10/21 09:31:41 shin Exp $
  21.  * @see        http://www.youtube.com/dev
  22.  * @since      0.1
  23.  */
  24.  
  25. /**
  26.  * Services_YouTube exception class
  27.  */
  28. require_once 'Services/YouTube/Exception.php';
  29.  
  30. /**
  31.  * Services_YouTube
  32.  *
  33.  * @package Services_YouTube
  34.  * @version 0.2.0
  35.  * @author Shin Ohno <ganchiku@gmail.com>
  36.  */
  37. class Services_YouTube
  38. {
  39.     // {{{ class const
  40.     /**
  41.      * Version of this package
  42.      */
  43.     const VERSION = '0.2.0';
  44.  
  45.     /**
  46.      * URL of the YouTube Server
  47.      */
  48.     const URL = 'www.youtube.com';
  49.     /**
  50.      * URI of the XML RPC path
  51.      */
  52.     const XMLRPC_PATH = '/api2_xmlrpc';
  53.  
  54.     /**
  55.      * URI of the REST path
  56.      */
  57.     const REST_PATH = '/api2_rest';
  58.  
  59.     /**
  60.      * Max number of the movie list per page
  61.      */
  62.     const VIDEO_PER_PAGE_MAX = 100;
  63.     // }}}
  64.     // {{{ class vars
  65.     /**
  66.      * Developer ID
  67.      *
  68.      * @var string
  69.      * @access public
  70.      */
  71.     protected $developerId = null;
  72.  
  73.     /**
  74.      * driver
  75.      *
  76.      * @var string
  77.      * @access protected
  78.      */
  79.     protected $driver = 'rest';
  80.  
  81.     /**
  82.      * Use cache
  83.      * @var boolean
  84.      * @access protected
  85.      */
  86.     protected $useCache = false;
  87.     /**
  88.      * cache_lite options
  89.      *
  90.      * @var array
  91.      * @access protected
  92.      */
  93.     protected $cacheOptions = array();
  94.     /**
  95.      * format of the xml response
  96.      *
  97.      * @var string
  98.      * @access protected
  99.      */
  100.     protected $responseFormat = 'object';
  101.     // }}}
  102.     // {{{ constructor
  103.     /**
  104.      * Constructor
  105.      *
  106.      * @param string $developerId Developer ID
  107.      * @access public
  108.      * @return void
  109.      */
  110.     public function __construct($developerId, $options = array())
  111.     {
  112.         $this->developerId = $developerId;
  113.         $availableOptions = array('useCache', 'cacheOptions', 'responseFormat', 'driver');
  114.         foreach ($options as $key => $value) {
  115.             if (in_array($key, $availableOptions)) {
  116.                 $this->$key = $value;
  117.             }
  118.         }
  119.     }
  120.     // }}}
  121. // {{{ setter methods
  122.    /**
  123.      * Choose which driver to use(XML-RPC or REST)
  124.      *
  125.      * @param string $driver
  126.      * @access public
  127.      * @return void
  128.      * @throws Services_YouTube_Exception
  129.      */
  130.     public function setDriver($driver)
  131.     {
  132.         if ($driver == 'xmlrpc' or $driver == 'rest') {
  133.             $this->driver = $driver;
  134.         } else {
  135.             throw new Services_YouTube_Exception('Driver has to be "xmlrpc" or "rest"');
  136.         }
  137.     }
  138.  
  139.     /**
  140.      * Choose which Response Fomat to use(object or array)
  141.      *
  142.      * @param string $responseFormat
  143.      * @access public
  144.      * @return void
  145.      * @throws Services_YouTube_Exception
  146.      */
  147.     public function setResponseFormat($responseFormat)
  148.     {
  149.         if ($responseFormat == 'object' or $responseFormat == 'array') {
  150.             $this->responseFormat = $responseFormat;
  151.         } else {
  152.             throw new Services_YouTube_Exception('ResponseFormat has to be "object" or "array"');
  153.         }
  154.     }
  155.  
  156.     /**
  157.      * Choose if this uses Cache_Lite.
  158.      * If this uses Cache_Lite, then set the cacheOptions for Cache_Lite
  159.      *
  160.      * @param mixed $useCache
  161.      * @param array $cacheOptions
  162.      * @access public
  163.      * @return void
  164.      */
  165.     public function setUseCache($useCache = false, $cacheOptions = array())
  166.     {
  167.         $this->useCache = $useCache;
  168.         if ($useCache) {
  169.             $this->cacheOptions = $cacheOptions;
  170.         }
  171.     }
  172. // }}}
  173.     // {{{ users
  174.     /**
  175.      * Retrieves the public parts of a user profile.
  176.      *
  177.      * @param string $user The user to retrieve the profile for. This is the same as the name that shows up on the YouTube website.
  178.      * @access public
  179.      * @return array on success, error object on failure
  180.      * @throws Services_YouTube_Exception
  181.      */
  182.     public function getProfile($user)
  183.     {
  184.         $parameters = array('dev_id' => $this->developerId,
  185.             'user'   => $user);
  186.         return $this->sendRequest('youtube.users.', 'get_profile', $parameters);
  187.     }
  188.     /**
  189.      * Lists a user's favorite videos.
  190.      *
  191.      * @param string $user The user to retrieve the favorite videos for. This is the same as the name that shows up on the YouTube website
  192.      * @access public
  193.      * @return array on success, error object on failure
  194.      * @throws Services_YouTube_Exception
  195.      */
  196.     public function listFavoriteVideos($user)
  197.     {
  198.         $parameters = array('dev_id' => $this->developerId,
  199.             'user'   => $user);
  200.         return $this->sendRequest('youtube.users.', 'list_favorite_videos', $parameters);
  201.     }
  202.     /**
  203.      * Lists a user's friends.
  204.      *
  205.      * @param string $user The user to retrieve the favorite videos for. This is the same as the name that shows up on the YouTube website
  206.      * @access public
  207.      * @return array on success, error object on failure
  208.      * @throws Services_YouTube_Exception
  209.      */
  210.     public function listFriends($user)
  211.     {
  212.         $parameters = array('dev_id' => $this->developerId,
  213.             'user'   => $user);
  214.         return $this->sendRequest('youtube.users.', 'list_friends', $parameters);
  215.     }
  216.     // }}}
  217.     // {{{ videos
  218.     /**
  219.      * Displays the details for a video.
  220.      *
  221.      * @param string $videoId The ID of the video to get details for. This is the ID that's returned by the list
  222.      * @access public
  223.      * @return array on success, error object on failure
  224.      * @throws Services_YouTube_Exception
  225.      */
  226.     public function getDetails($videoId)
  227.     {
  228.         $parameters = array('dev_id'   => $this->developerId,
  229.             'video_id' => $videoId);
  230.         return $this->sendRequest('youtube.videos.', 'get_details', $parameters);
  231.     }
  232.     /**
  233.      * Lists all videos that have the specified tag.
  234.      *
  235.      * @param string $tag the tag to search for
  236.      * @param string $page the "page" of results you want to retrieve (e.g. 1, 2, 3) (default 1)
  237.      * @param string $perPage the number of results you want to retrieve per page (default 20, maximum 100)
  238.      *
  239.      * @access public
  240.      * @return array on success, error object on failure
  241.      * @throws Services_YouTube_Exception
  242.      */
  243.     public function listByTag($tag, $page = 1, $perPage = 20)
  244.     {
  245.         if ($perPage > self::VIDEO_PER_PAGE_MAX) {
  246.             throw new Services_YouTube_Exception('The Maximum of the perPage is ' . self::VIDEO_PER_PAGE_MAX);
  247.         }
  248.         $parameters = array('dev_id' => $this->developerId,
  249.             'tag'    => $tag,
  250.             'page' => $page,
  251.             'per_page' => $perPage);
  252.         return $this->sendRequest('youtube.videos.', 'list_by_tag', $parameters);
  253.     }
  254.  
  255.     /**
  256.      * Lists all videos that were uploaded by the specified user
  257.      *
  258.      * @param string $user user whose videos you want to list
  259.      * @access public
  260.      * @return array on success, error object on failure
  261.      * @throws Services_YouTube_Exception
  262.      */
  263.     public function listByUser($user)
  264.     {
  265.         $parameters = array('dev_id' => $this->developerId,
  266.             'user'   => $user);
  267.         return $this->sendRequest('youtube.videos.', 'list_by_user', $parameters);
  268.     }
  269.  
  270.     /**
  271.      * Lists the most recent 25 videos that have been featured on the front page of the YouTube site.
  272.      *
  273.      * @access public
  274.      * @return array on success, error object on failure
  275.      * @throws Services_YouTube_Exception
  276.      */
  277.     public function listFeatured()
  278.     {
  279.         $parameters = array('dev_id' => $this->developerId);
  280.         return $this->sendRequest('youtube.videos.', 'list_featured', $parameters);
  281.     }
  282.     // }}}
  283.     // {{{ protected
  284.     /**
  285.      *  Send Request either rest or xmlrpc approach, and return simplexml_element of the response.
  286.      *  When $this->usesCaches is true, use Cache_Lite the response xml.
  287.      *  If $this->responseFormat is "array", return array, instead simplexml_element.
  288.      *
  289.      * @param string $prefix
  290.      * @param string $method
  291.      * @param array $parameters
  292.      * @access protected
  293.      * @return SimpleXMLObject or Array on success, error object on failure
  294.      * @throws Services_YouTube_Exception
  295.      */
  296.     protected function sendRequest($prefix, $method, $parameters)
  297.     {
  298.         // Use Cache_Lite
  299.         if ($this->useCache) {
  300.             require_once 'Cache/Lite.php';
  301.             $cacheID = md5($prefix . $method . serialize($parameters));
  302.             $cache = new Cache_Lite($this->cacheOptions);
  303.  
  304.             if ($response = $cache->get($cacheID)) {
  305.                 return $this->parseResponse($response);
  306.             }
  307.         }
  308.  
  309.         if ($this->driver == 'rest') {
  310.             $response = $this->useRest($prefix . $method, $parameters);
  311.         } else if ($this->driver == 'xmlrpc') {
  312.             $response = $this->useXMLRPC($prefix, $method, $parameters);
  313.         } else {
  314.             throw new Services_YouTube_Exception('Driver has to be "xmlrpc" or "rest"');
  315.         }
  316.         $data = $this->parseResponse($response);
  317.  
  318.         // Use Cache_Lite
  319.         if ($this->useCache and isset($cache)) {
  320.             if (!$cache->save($response, $cacheID)) {
  321.                 throw new Services_YouTube_Exception('Can not write cache');
  322.             }
  323.         }
  324.         return $data;
  325.     }
  326.  
  327.     /**
  328.      * Use REST approach.
  329.      *
  330.      * @param string $method
  331.      * @param array $parameters
  332.      * @access protected
  333.      * @return string
  334.      * @throws Services_YouTube_Exception
  335.      */
  336.     protected function useRest($method, $parameters)
  337.     {
  338.         if (!function_exists('curl_init')) {
  339.             throw new Services_YouTube_Exception('cannot use curl exntensions');
  340.         }
  341.         if (!$ch = curl_init()) {
  342.             throw new Services_YouTube_Exception('Unable to setup curl');
  343.         }
  344.         $url = 'http://' . self::URL . self::REST_PATH . '?method=' . $method;
  345.         foreach ($parameters as $key => $val) {
  346.             $url .= '&' . $key . '=' . urlencode($val);
  347.         }
  348.         curl_setopt($ch, CURLOPT_URL, $url);
  349.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  350.         curl_setopt($ch, CURLOPT_POST, true);
  351.         curl_setopt($ch, CURLOPT_USERAGENT, 'User-Agent: Service_YouTube: ' . self::VERSION);
  352.         $result = curl_exec($ch);
  353.         if (($errno = curl_errno($ch)) != 0) {
  354.             throw new Services_YouTube_Exception('Curl returned non-null errno ' . $errno .':' . curl_error($ch));
  355.         }
  356.         curl_close($ch);
  357.         return $result;
  358.     }
  359.  
  360.     /**
  361.      * Use XML-RPC approach.
  362.      *
  363.      * @param string $prefix
  364.      * @param string $method
  365.      * @param array $parameters
  366.      * @access protected
  367.      * @return string
  368.      * @throws Services_YouTube_Exception
  369.      */
  370.     protected function useXMLRPC($prefix, $method, $parameters)
  371.     {
  372.         require_once 'XML/RPC2/Client.php';
  373.  
  374.         $options = array('prefix' => $prefix);
  375.         try {
  376.             $client = XML_RPC2_Client::create('http://' . self::URL . self::XMLRPC_PATH, $options);
  377.             $result = $client->$method($parameters);
  378.         } catch (XML_RPC2_FaultException $e) {
  379.             throw new Services_YouTube_Exception('XML_RPC Failed :' . $e->getMessage());
  380.         } catch (Exception $e) {
  381.             throw new Services_YouTube_Exception($e->getMessage());
  382.         }
  383.         return $result;
  384.     }
  385.  
  386.     /**
  387.      * parseResponse
  388.      *
  389.      * @param string $response
  390.      * @access protected
  391.      * @return SimpleXMLElement or array of the response data.
  392.      * @throws Services_YouTube_Exception
  393.      */
  394.     protected function parseResponse($response)
  395.     {
  396.         set_error_handler(array('Services_YouTube_Exception', 'errorHandlerCallback'), E_ALL);
  397.         try {
  398.             if (!$data = simplexml_load_string($response)) {
  399.                 throw new Services_YouTube_Exception('Parsing Failed. Response string is invalid');
  400.             }
  401.             if ($this->responseFormat == 'array') {
  402.                 $data = $this->forArray($data);
  403.             }
  404.             restore_error_handler();
  405.         } catch (Services_YouTube_Exception $e) {
  406.             restore_error_handler();
  407.             throw $e;
  408.         }
  409.         return $data;
  410.     }
  411.  
  412.     /**
  413.      * Parse all SimpleXMLElement to array
  414.      *
  415.      * @param mixed $object SimpleXMLElement or array
  416.      * @access protected
  417.      * @return array
  418.      */
  419.     protected function forArray($object)
  420.     {
  421.         $return = array();
  422.  
  423.         if (is_array($object)) {
  424.             foreach ($object as $key => $value) {
  425.                 $return[$key] = $this->forArray($value);
  426.             }
  427.         } else {
  428.             $vars = get_object_vars($object);
  429.             if (is_array($vars)) {
  430.                 foreach ($vars as $key => $value) {
  431.                     $return[$key] = ($key && !$value) ? null : $this->forArray($value);
  432.                 }
  433.             } else {
  434.                 return $object;
  435.             }
  436.         }
  437.         return $return;
  438.     }
  439.     // }}}
  440. }
  441.  
  442. ?>
  443.