Services_Technorati
[ class tree: Services_Technorati ] [ index: Services_Technorati ] [ all elements ]

Source for file Technorati.php

Documentation is available at Technorati.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: James Stewart <james@jystewart.net>                          |
  17. // +----------------------------------------------------------------------+
  18. //        $id: Technorati.php,v 0.5.5 2005/04/16 16:49:00 jystewart Exp $
  19. //
  20.  
  21. /**
  22.  * using PEAR error management
  23.  */
  24.  
  25. require_once 'PEAR.php';
  26.  
  27. /**
  28.  * using XML_Serializer for processing
  29.  */
  30.  
  31. require_once 'XML/Unserializer.php';
  32.  
  33. /**
  34.  * uses HTTP to send requests
  35.  */ 
  36.  
  37. require_once 'HTTP/Request.php';
  38.  
  39. /**
  40.  * uses Cache_Lite for caching
  41.  */
  42.  
  43.  require_once 'Cache/Lite.php';
  44.  
  45. /**
  46.  * Services_Technorati
  47.  *
  48.  * Client for Technorati's REST-based webservices
  49.  *
  50.  * Technorati is a blog search engine with a number of tools to help
  51.  * explore and utilise the blogosphere. The API provides enhanced
  52.  * access to all the site's features
  53.  *
  54.  * @author        James Sytewart <james@jystewart.net>
  55.  * @package        Services_Technorati
  56.  * @version        0.5.5
  57.  * @todo        update once attention.xml query is stabilised
  58.  * @link        http://pear.php.net/package/Services_Technorati
  59.  */
  60.  
  61. {
  62.     /**
  63.      * URI of the REST API
  64.      *
  65.      * @access        private
  66.      * @var        string 
  67.      */
  68.      var $_apiUrl 'http://api.technorati.com';
  69.      
  70.      /**
  71.     * API Key
  72.     *
  73.     * @access        private
  74.     * @var        string 
  75.     */
  76.      var $_apiKey = null;
  77.      
  78.      /**
  79.     * Hours To Cache
  80.     *
  81.     * @access        private
  82.     * @var        int 
  83.     */
  84.      var $_hoursToCache;
  85.      
  86.      /**
  87.       * Path To Cache
  88.       *
  89.       * @access        private
  90.       *    @var        string 
  91.       */
  92.      var $_pathToCache;
  93.      
  94.      /**
  95.       * XML_Unserializer, used to parse the XML
  96.       *
  97.       * @access        private
  98.       * @var        object XML_Unserializer 
  99.       */
  100.      var $_xmlUs = null;
  101.      
  102.      /**
  103.       * Create our client
  104.       *
  105.       * @access        public
  106.       * @param        string        apiKey
  107.       * @param        int            hoursToCache
  108.       * @param        string        pathToCache
  109.       */
  110.     
  111.     function Services_Technorati($apiKey$hoursToCache = null$pathToCache = null)
  112.     {
  113.             $this->_apiKey $apiKey;
  114.             
  115.             if (empty($hoursToCache)) {
  116.                 $this->_hoursToCache $hoursToCache;
  117.                 $this->_pathToCache $pathToCache;
  118.                 $cache_options = array(
  119.                     'cacheDir' => $pathToCache,
  120.                     'lifeTime' => (3600 * $hoursToCache)
  121.                 );
  122.                 $this->_cache = new Cache_Lite($cache_options);
  123.             }
  124.     }
  125.  
  126.      /**
  127.       * Factory methods to create client
  128.       *
  129.       * @access        public
  130.       * @param        string        apiKey
  131.       * @param        int            hoursToCache
  132.       * @param        string        pathToCache
  133.       * @return        Services_Technorati object
  134.       */    
  135.       
  136.     function factory($apiKey$hoursToCache ''$pathToCache '')
  137.     {
  138.         return new Services_Technorati($apiKey$hoursToCache$pathToCache);
  139.     }
  140.     
  141.     /**
  142.      * KeyInfo provides information on daily usage of an API key.
  143.      * A Technorati API key is typically limited to 500 requests
  144.      * per day, where a day is measured as 00:00-23:59 PST
  145.      * This function does not cache, and this call does not use any
  146.      * of the daily request allocation for the given key.
  147.      *
  148.      * @access        public
  149.      * @return        array 
  150.      */
  151.      
  152.      function keyInfo({
  153.          return $this->_sendRequest('keyinfo');
  154.      }
  155.  
  156.      
  157.     /**
  158.      * Cosmos lets you see what blogs are linking to a given URL
  159.      *
  160.      * @access        public
  161.      * @param        string    url
  162.      * @param        array    options
  163.      * @return        array 
  164.      */
  165.      
  166.     function cosmos($url$options
  167.     {
  168.         /* Check for invalid options */
  169.         
  170.         $valid_options = array('type','limit','start','current','claim','highlight');
  171.         if (is_array($options)) {
  172.             $options $this->_checkOptions($options$valid_options);
  173.             if (PEAR::isError($options)) {
  174.                 return $options;
  175.             }
  176.             $options array_merge(array('url'=>urlencode($url))$options);
  177.         else {
  178.             $options = array('url' => urlencode($url));
  179.         }
  180.         
  181.         /* Build cache URI */
  182.  
  183.         $filename $url."cosmos".join("-",$options);
  184.  
  185.         /* Check if cached */
  186.         
  187.         if (!empty($this->_cache&& $cache $this->_cache->get($filename)) {
  188.             return $cache;
  189.         }
  190.  
  191.         /* Not cached */
  192.  
  193.         $value $this->_sendRequest('cosmos',$options);
  194.  
  195.         if (PEAR::isError($value&& !empty($this->_cache)) {
  196.             $this->_cache->save($value);
  197.         }
  198.         
  199.         return $value;
  200.     }
  201.  
  202.     /**
  203.      *  The search lets you see what blogs contain a given search string
  204.      *
  205.      * @access        public
  206.      * @param        string    query
  207.      * @param        array    options
  208.      * @return        array 
  209.      */
  210.  
  211.     function search($query$options = array()) 
  212.     {
  213.         /* Check for invalid options */
  214.         
  215.         $valid_options = array('start','limit','claim');
  216.         if (is_array($options)) {
  217.             $options $this->_checkOptions($options$valid_options);
  218.             if (PEAR::isError($options)) {
  219.                 return $options;
  220.             }
  221.             $options array_merge(array('query'=>urlencode($query))$options);
  222.         else {
  223.             $options = array('query'=>urlencode($query));
  224.         }
  225.         
  226.         /* Build cache URI */
  227.  
  228.         $filename "search.".join("_",$query).join("_",$options);
  229.         
  230.         /* Check if cached */
  231.         
  232.         if ($cache $this->_cache->get($filename)) {
  233.             return $cache;
  234.         }
  235.  
  236.         /* Not cached */
  237.  
  238.         $value $this->_sendRequest('search',$options);
  239.  
  240.         if (PEAR::isError($value&& !empty($this->_cache)) {
  241.             $this->_cache->save($value);
  242.         }
  243.  
  244.         return $value;
  245.     }
  246.  
  247.     /**
  248.      *  The getinfo query tells you things that Technorati knows about a user
  249.      *
  250.      * @access        public
  251.      * @param        string    username
  252.      * @return        array 
  253.      */
  254.     
  255.     function getInfo($username
  256.     {
  257.  
  258.         $options = array('username'=>urlencode($username));
  259.         
  260.         /* Build cache URI */
  261.  
  262.         $filename = "getinfo.$username";
  263.  
  264.         /* Check if cached */
  265.         
  266.         if ($cache $this->_cache->get($filename)) {
  267.             return $cache;
  268.         }
  269.  
  270.         /* Not cached */
  271.  
  272.         $value $this->_sendRequest('getinfo',$options);
  273.  
  274.         if (PEAR::isError($value&& !empty($this->_cache)) {
  275.             $this->_cache->save($value);
  276.         }
  277.     
  278.         return $value;
  279.     }
  280.  
  281.     /**
  282.      *  The outbound query lets you see what blogs are linked to from a given
  283.      *    blog, including their associated info.
  284.      *
  285.      * @access        public
  286.      * @param        string    url
  287.      * @param        array    options
  288.      * @return        array 
  289.      */
  290.      
  291.     function outbound($url$options = array()) 
  292.     {
  293.         /* Check for invalid options */
  294.         
  295.         $valid_options = array('start');
  296.         if (is_array($options)) {
  297.             $options $this->_checkOptions($options$valid_options);
  298.             if (PEAR::isError($options)) {
  299.                 return $options;
  300.             }
  301.             $options array_merge(array('url'=>urlencode($url))$options);
  302.         else {
  303.             $options = array('url'=>urlencode($url));
  304.         }
  305.         
  306.         /* Build cache URI */
  307.  
  308.         $filename $url."outbound.".join("_",$options);
  309.  
  310.         /* Check if cached */
  311.         
  312.         if ($cache $this->_cache->get($filename)) {
  313.             return $cache;
  314.         }
  315.  
  316.         /* Not cached */
  317.  
  318.         $value $this->_sendRequest('outbound',$options);
  319.  
  320.         if (PEAR::isError($value&& !empty($this->_cache)) {
  321.             $this->_cache->save($value);
  322.         }
  323.     
  324.         return $value;
  325.     }
  326.  
  327.     /**
  328.      *  The bloginfo query provides info on what blog, if any, is
  329.      *    associated with a given URL
  330.      *
  331.      * @access        public
  332.      * @param        string    url
  333.      * @return        array 
  334.      */
  335.      
  336.     function blogInfo($url
  337.     {
  338.         $options['url'urlencode($url);
  339.         
  340.         /* Build cache URI */
  341.  
  342.         $filename $url."bloginfo.";
  343.  
  344.         /* Check if cached */
  345.         
  346.         if ($cache $this->_cache->get($filename)) {
  347.             return $cache;
  348.         }
  349.  
  350.         /* Not cached */
  351.  
  352.         $value $this->_sendRequest('bloginfo',$options);
  353.  
  354.         if (PEAR::isError($value&& !empty($this->_cache)) {
  355.             $this->_cache->save($value);
  356.         }
  357.     
  358.         return $value;
  359.     }
  360.  
  361.     /**
  362.      *  The tag query allows you to get a list of posts with the given tag
  363.      *    associated with it. This API query is currently experimental.
  364.      * 
  365.      * @access        public
  366.      * @param        string    url
  367.      * @param        array    options
  368.      * @return        array 
  369.      */
  370.  
  371.     function tag($tag$options = array()) 
  372.     {
  373.         /* Check for invalid options */
  374.         
  375.         $valid_options = array('limit','start','format',
  376.             'excerptsize','topexcerptsize');
  377.         if (is_array($options)) {
  378.             $options $this->_checkOptions($options$valid_options);
  379.             if (PEAR::isError($options)) {
  380.                 return $options;
  381.             }
  382.             $options array_merge(array('tag'=>$tag)$options);
  383.         else {
  384.             $options = array('tag' => $tag);
  385.         }
  386.         
  387.         /* Build cache URI */
  388.  
  389.         $filename = "tag.{$tag}";
  390.  
  391.         /* Check if cached */
  392.         
  393.         if ($cache $this->_cache->get($filename)) {
  394.             return $cache;
  395.         }
  396.  
  397.         /* Not cached */
  398.  
  399.         $value $this->_sendRequest('tag',$options);
  400.  
  401.         if (PEAR::isError($value&& !empty($this->_cache)) {
  402.             $this->_cache->save($value);
  403.         }
  404.     
  405.         return $value;
  406.     }
  407.      
  408.     /**
  409.      * TopTags lets you retrieve a list of the most popular post tagd
  410.      * tracked by Technorati
  411.      *
  412.      * @access        public
  413.      * @param        array    options
  414.      * @return        array 
  415.      */
  416.      
  417.      function topTags($options
  418.      {
  419.          $valid_options = array('limit','start');
  420.          if (is_array($options)) {
  421.             $options $this->_checkOptions($options$valid_options);
  422.             if (PEAR::isError($options)) {
  423.                 return $options;
  424.             }
  425.             $options array_merge(array('url'=>urlencode($url))$options);
  426.         else {
  427.             return PEAR::raiseError('You must supply options as an array');
  428.         }
  429.         
  430.         /* Build cache URI */
  431.  
  432.         $filename $url."cosmos".join("-",$options);
  433.  
  434.         /* Check if cached */
  435.         
  436.         if ($cache $this->_cache->get($filename)) {
  437.             return $cache;
  438.         }
  439.  
  440.         /* Not cached */
  441.  
  442.         $value $this->_sendRequest('toptags',$options);
  443.  
  444.         if (PEAR::isError($value&& !empty($this->_cache)) {
  445.             $this->_cache->save($value);
  446.         }
  447.         
  448.         return $value;
  449.     }
  450.     
  451.     /**
  452.      *  This lets users retrieve their Attention.XML
  453.         *  This API query is currently experimental.
  454.      *
  455.      * @access        public
  456.      * @param        string    username
  457.      * @param        string    password
  458.      * @return        array 
  459.      */
  460.      
  461.     function attention($user$password
  462.     {
  463.         
  464.         $options = array('username' => $user'password' => md5($password));
  465.         
  466.         /* Build cache URI */
  467.  
  468.         $filename = "attention.{$user}";
  469.  
  470.         /* Check if cached */
  471.         
  472.         if ($cache $this->_cache->get($filename)) {
  473.             return $cache;
  474.         }
  475.  
  476.         /* Not cached */
  477.  
  478.         $value $this->_sendRequest('attention',$options);
  479.  
  480.         if (PEAR::isError($value&& !empty($this->_cache)) {
  481.             $this->_cache->save($value);
  482.         }
  483.         
  484.         return $value;    
  485.     }
  486.  
  487.     /**
  488.      *  This posts a new Attention.XML file to the Technorati system.
  489.      *  This API query is currently experimental. This is the one call that
  490.      *    doesn't use _sendRequest, because it needs to POST a file.
  491.      *
  492.      * @access        public
  493.      * @param        string    username
  494.      * @param        string    password
  495.      * @return        boolean 
  496.      */
  497.      
  498.     function attentionPost($user$password$file
  499.     {
  500.         
  501.         $options = array('username' => $user'password' => md5($password));
  502.         
  503.         /* Build cache URI */
  504.  
  505.         $filename = "attention.{$user}";
  506.  
  507.         /* We don't cache this query */
  508.  
  509.         $request =new HTTP_Request($this->_apiUrl."attention");
  510.         $request->setMethod(HTTP_REQUEST_METHOD_POST);
  511.         $addfile $request->addFile("attention.xml",$file);
  512.  
  513.         if (PEAR::isError($addfile)) {
  514.             return $addfile;
  515.         }
  516.  
  517.         $request->addPostData("username"$user);
  518.         $request->addPostData("password"md5($password));
  519.         $request->addHeader('User-Agent','Services_Technorati');
  520.  
  521.         $request->sendRequest();
  522.         
  523.         if ($request->getResponseCode(!== 200{
  524.             return PEAR::raiseError('Invalid Response Code'$request->getResponseCode());
  525.         }
  526.         
  527.         $result $request->getResponseBody();
  528.  
  529.         if (is_object($this->_xmlUs)) {
  530.             $this->_xmlUs =new XML_Unserializer();
  531.             $this->_xmlUs->setOption('parseAttributes',true);
  532.         }
  533.         
  534.         $result $this->_xmlUs->unserialize($result);
  535.  
  536.         if (PEAR::isError($result)) {
  537.             return $result;
  538.         }
  539.         if (!empty($result['document']['result']['error'])) {
  540.             return PEAR::raiseError("Technorati Response Error",
  541.                 $value['document']['result']['error']);
  542.         }
  543.  
  544.         $value $this->_xmlUs->getUnserializedData();
  545.         
  546.         /* Store in cache */
  547.         
  548.         $this->_cache->save($value);
  549.         
  550.         return $value;
  551.     }
  552.  
  553.     /**
  554.      *  Send everything out
  555.      *
  556.      * @access        private
  557.      * @param        string    type of query
  558.      * @param        array    parameters
  559.      * @return        array|PEAR_ERROR
  560.      */
  561.      
  562.     function _sendRequest($query$options = array()) 
  563.     {
  564.         /* Do all the nitty gritty HTTP stuff. Except attentionPost */
  565.         $url sprintf("%s/%s?key=%s",$this->_apiUrl,$query,$this->_apiKey);
  566.         
  567.         foreach ($options as $key => $value{
  568.             $url $url '&' $key '=' urlencode($value);
  569.         }
  570.  
  571.         $request =new HTTP_Request($url);
  572.         $request->addHeader('User-Agent','Services_Technorati');
  573.         
  574.         $request->sendRequest();
  575.         if ($request->getResponseCode(!== 200{
  576.             return PEAR::raiseError('Invalid Response Code'
  577.                 $request->getResponseCode());
  578.         }
  579.         
  580.         $result $request->getResponseBody();
  581.  
  582.         if (is_object($this->_xmlUs)) {
  583.             $this->_xmlUs =new XML_Unserializer();
  584.             $this->_xmlUs->setOption('parseAttributes',true);
  585.         }
  586.         
  587.         $result $this->_xmlUs->unserialize($result);
  588.  
  589.         if (PEAR::isError($result)) {
  590.             return $result;
  591.         }
  592.         if (!empty($result['document']['result']['error'])) {
  593.             return PEAR::raiseError("Technorati Response Error",
  594.                 $value['document']['result']['error']);
  595.         }
  596.         $unserialized $this->_xmlUs->getUnserializedData();
  597.         if (!empty($unserialized['document']['result']['error'])) {
  598.             return PEAR::raiseError("Technorati Response Error",
  599.                 $value['document']['result']['error']);
  600.         }
  601.         return $unserialized;
  602.     }
  603.     
  604.     /**
  605.      * raise errors if options are specified that aren't allowed
  606.      *
  607.      * @access        private
  608.      * @param        array        specified options
  609.      * @param        array        acceptable options
  610.      */
  611.      
  612.     function _checkOptions($current$accepted
  613.     {
  614.         foreach ($current as $option => $value{
  615.             if (in_array($option,$accepted)) {
  616.                 $accepted_options[$option$value;
  617.             else {
  618.                 PEAR::raiseError("Invalid option passed to Query",$option);
  619.             
  620.         }
  621.     
  622. }
  623.  
  624. ?>

Documentation generated on Mon, 11 Mar 2019 14:21:13 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.