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.4 2005/03/21 22:11::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.             $options array_merge(array('url'=>urlencode($url))$options);
  174.         else {
  175.             $options = array('url' => urlencode($url));
  176.         }
  177.         
  178.         /* Build cache URI */
  179.  
  180.         $filename $url."cosmos".join("-",$options);
  181.  
  182.         /* Check if cached */
  183.         
  184.         if (!empty($this->_cache&& $cache $this->_cache->get($filename)) {
  185.             return $cache;
  186.         }
  187.  
  188.         /* Not cached */
  189.  
  190.         $value $this->_sendRequest('cosmos',$options);
  191.  
  192.         if (PEAR::isError($value&& !empty($this->_cache)) {
  193.             $this->_cache->save($value);
  194.         }
  195.         
  196.         return $value;
  197.     }
  198.  
  199.     /**
  200.      *  The search lets you see what blogs contain a given search string
  201.      *
  202.      * @access        public
  203.      * @param        string    query
  204.      * @param        array    options
  205.      * @return        array 
  206.      */
  207.  
  208.     function search($query$options = array()) 
  209.     {
  210.         /* Check for invalid options */
  211.         
  212.         $valid_options = array('start','limit','claim');
  213.         if (is_array($options)) {
  214.             $options $this->_checkOptions($options$valid_options);
  215.             $options array_merge(array('query'=>urlencode($query))$options);
  216.         else {
  217.             $options = array('query'=>urlencode($query));
  218.         }
  219.         
  220.         /* Build cache URI */
  221.  
  222.         $filename "search.".join("_",$query).join("_",$options);
  223.         
  224.         /* Check if cached */
  225.         
  226.         if ($cache $this->_cache->get($filename)) {
  227.             return $cache;
  228.         }
  229.  
  230.         /* Not cached */
  231.  
  232.         $value $this->_sendRequest('search',$options);
  233.  
  234.         if (PEAR::isError($value&& !empty($this->_cache)) {
  235.             $this->_cache->save($value);
  236.         }
  237.  
  238.         return $value;
  239.     }
  240.  
  241.     /**
  242.      *  The getinfo query tells you things that Technorati knows about a user
  243.      *
  244.      * @access        public
  245.      * @param        string    username
  246.      * @return        array 
  247.      */
  248.     
  249.     function getInfo($username
  250.     {
  251.  
  252.         $options = array('username'=>urlencode($username));
  253.         
  254.         /* Build cache URI */
  255.  
  256.         $filename = "getinfo.$username";
  257.  
  258.         /* Check if cached */
  259.         
  260.         if ($cache $this->_cache->get($filename)) {
  261.             return $cache;
  262.         }
  263.  
  264.         /* Not cached */
  265.  
  266.         $value $this->_sendRequest('getinfo',$options);
  267.  
  268.         if (PEAR::isError($value&& !empty($this->_cache)) {
  269.             $this->_cache->save($value);
  270.         }
  271.     
  272.         return $value;
  273.     }
  274.  
  275.     /**
  276.      *  The outbound query lets you see what blogs are linked to from a given
  277.      *    blog, including their associated info.
  278.      *
  279.      * @access        public
  280.      * @param        string    url
  281.      * @param        array    options
  282.      * @return        array 
  283.      */
  284.      
  285.     function outbound($url$options = array()) 
  286.     {
  287.         /* Check for invalid options */
  288.         
  289.         $valid_options = array('start');
  290.         if (is_array($options)) {
  291.             $options $this->_checkOptions($options$valid_options);
  292.             $options array_merge(array('url'=>urlencode($url))$options);
  293.         else {
  294.             $options = array('url'=>urlencode($url));
  295.         }
  296.         
  297.         /* Build cache URI */
  298.  
  299.         $filename $url."outbound.".join("_",$options);
  300.  
  301.         /* Check if cached */
  302.         
  303.         if ($cache $this->_cache->get($filename)) {
  304.             return $cache;
  305.         }
  306.  
  307.         /* Not cached */
  308.  
  309.         $value $this->_sendRequest('outbound',$options);
  310.  
  311.         if (PEAR::isError($value&& !empty($this->_cache)) {
  312.             $this->_cache->save($value);
  313.         }
  314.     
  315.         return $value;
  316.     }
  317.  
  318.     /**
  319.      *  The bloginfo query provides info on what blog, if any, is
  320.      *    associated with a given URL
  321.      *
  322.      * @access        public
  323.      * @param        string    url
  324.      * @return        array 
  325.      */
  326.      
  327.     function blogInfo($url
  328.     {
  329.         $options['url'urlencode($url);
  330.         
  331.         /* Build cache URI */
  332.  
  333.         $filename $url."bloginfo.";
  334.  
  335.         /* Check if cached */
  336.         
  337.         if ($cache $this->_cache->get($filename)) {
  338.             return $cache;
  339.         }
  340.  
  341.         /* Not cached */
  342.  
  343.         $value $this->_sendRequest('bloginfo',$options);
  344.  
  345.         if (PEAR::isError($value&& !empty($this->_cache)) {
  346.             $this->_cache->save($value);
  347.         }
  348.     
  349.         return $value;
  350.     }
  351.  
  352.     /**
  353.      *  The tag query allows you to get a list of posts with the given tag
  354.      *    associated with it. This API query is currently experimental.
  355.      * 
  356.      * @access        public
  357.      * @param        string    url
  358.      * @param        array    options
  359.      * @return        array 
  360.      */
  361.  
  362.     function tag($tag$options = array()) 
  363.     {
  364.         /* Check for invalid options */
  365.         
  366.         $valid_options = array('limit','start','format',
  367.             'excerptsize','topexcerptsize');
  368.         if (is_array($options)) {
  369.             $options $this->_checkOptions($options$valid_options);
  370.             $options array_merge(array('tag'=>$tag)$options);
  371.         else {
  372.             $options = array('tag' => $tag);
  373.         }
  374.         
  375.         /* Build cache URI */
  376.  
  377.         $filename = "tag.{$tag}";
  378.  
  379.         /* Check if cached */
  380.         
  381.         if ($cache $this->_cache->get($filename)) {
  382.             return $cache;
  383.         }
  384.  
  385.         /* Not cached */
  386.  
  387.         $value $this->_sendRequest('tag',$options);
  388.  
  389.         if (PEAR::isError($value&& !empty($this->_cache)) {
  390.             $this->_cache->save($value);
  391.         }
  392.     
  393.         return $value;
  394.     }
  395.      
  396.     /**
  397.      * TopTags lets you retrieve a list of the most popular post tagd
  398.      * tracked by Technorati
  399.      *
  400.      * @access        public
  401.      * @return        array 
  402.      */
  403.      
  404.      function topTags($options
  405.      {
  406.          $valid_options = array('limit','start');
  407.          if (is_array($options)) {
  408.             $options $this->_checkOptions($options$valid_options);
  409.             $options array_merge(array('url'=>urlencode($url))$options);
  410.         else {
  411.             $options = array('url' => urlencode($url));
  412.         }
  413.         
  414.         /* Build cache URI */
  415.  
  416.         $filename $url."cosmos".join("-",$options);
  417.  
  418.         /* Check if cached */
  419.         
  420.         if ($cache $this->_cache->get($filename)) {
  421.             return $cache;
  422.         }
  423.  
  424.         /* Not cached */
  425.  
  426.         $value $this->_sendRequest('toptags',$options);
  427.  
  428.         if (PEAR::isError($value&& !empty($this->_cache)) {
  429.             $this->_cache->save($value);
  430.         }
  431.         
  432.         return $value;
  433.     }
  434.     
  435.     /**
  436.      *  This lets users retrieve their Attention.XML
  437.         *  This API query is currently experimental.
  438.      *
  439.      * @access        public
  440.      * @param        string    username
  441.      * @param        string    password
  442.      * @return        array 
  443.      */
  444.      
  445.     function attention($user$password
  446.     {
  447.         
  448.         $options = array('username' => $user'password' => md5($password));
  449.         
  450.         /* Build cache URI */
  451.  
  452.         $filename = "attention.{$user}";
  453.  
  454.         /* Check if cached */
  455.         
  456.         if ($cache $this->_cache->get($filename)) {
  457.             return $cache;
  458.         }
  459.  
  460.         /* Not cached */
  461.  
  462.         $value $this->_sendRequest('attention',$options);
  463.  
  464.         if (PEAR::isError($value&& !empty($this->_cache)) {
  465.             $this->_cache->save($value);
  466.         }
  467.         
  468.         return $value;    
  469.     }
  470.  
  471.     /**
  472.      *  This posts a new Attention.XML file to the Technorati system.
  473.      *  This API query is currently experimental. This is the one call that
  474.      *    doesn't use _sendRequest, because it needs to POST a file.
  475.      *
  476.      * @access        public
  477.      * @param        string    username
  478.      * @param        string    password
  479.      * @return        boolean 
  480.      */
  481.      
  482.     function attentionPost($user$password$file
  483.     {
  484.         
  485.         $options = array('username' => $user'password' => md5($password));
  486.         
  487.         /* Build cache URI */
  488.  
  489.         $filename = "attention.{$user}";
  490.  
  491.         /* We don't cache this query */
  492.  
  493.         $request =new HTTP_Request($this->_apiUrl."attention");
  494.         $request->setMethod(HTTP_REQUEST_METHOD_POST);
  495.         $addfile $request->addFile("attention.xml",$file);
  496.  
  497.         if (PEAR::isError($addfile)) {
  498.             return $addfile;
  499.         }
  500.  
  501.         $request->addPostData("username"$user);
  502.         $request->addPostData("password"md5($password));
  503.         $request->addHeader('User-Agent','Services_Technorati');
  504.  
  505.         $request->sendRequest();
  506.         
  507.         if ($request->getResponseCode(!== 200{
  508.             return PEAR::raiseError('Invalid Response Code'$request->getResponseCode());
  509.         }
  510.         
  511.         $result $request->getResponseBody();
  512.  
  513.         if (is_object($this->_xmlUs)) {
  514.             $this->_xmlUs =new XML_Unserializer();
  515.             $this->_xmlUs->setOption('parseAttributes',true);
  516.         }
  517.         
  518.         $result $this->_xmlUs->unserialize($result);
  519.  
  520.         if (PEAR::isError($result)) {
  521.             return $result;
  522.         }
  523.         if (!empty($result['document']['result']['error'])) {
  524.             return PEAR::raiseError("Technorati Response Error",
  525.                 $value['document']['result']['error']);
  526.         }
  527.  
  528.         $value $this->_xmlUs->getUnserializedData();
  529.         
  530.         /* Store in cache */
  531.         
  532.         $this->_cache->save($value);
  533.         
  534.         return $value;
  535.     }
  536.  
  537.     /**
  538.      *  Send everything out
  539.      *
  540.      * @access        private
  541.      * @param        string    type of query
  542.      * @param        array    parameters
  543.      * @return        array|PEAR_ERROR
  544.      */
  545.      
  546.     function _sendRequest($query$options = array()) 
  547.     {
  548.         /* Do all the nitty gritty HTTP stuff. Except attentionPost */
  549.         $url sprintf("%s/%s?key=%s",$this->_apiUrl,$query,$this->_apiKey);
  550.         
  551.         foreach ($options as $key => $value{
  552.             $url $url '&' $key '=' urlencode($value);
  553.         }
  554.  
  555.         $request =new HTTP_Request($url);
  556.         $request->addHeader('User-Agent','Services_Technorati');
  557.         
  558.         $request->sendRequest();
  559.         if ($request->getResponseCode(!== 200{
  560.             return PEAR::raiseError('Invalid Response Code'
  561.                 $request->getResponseCode());
  562.         }
  563.         
  564.         $result $request->getResponseBody();
  565.  
  566.         if (is_object($this->_xmlUs)) {
  567.             $this->_xmlUs =new XML_Unserializer();
  568.             $this->_xmlUs->setOption('parseAttributes',true);
  569.         }
  570.         
  571.         $result $this->_xmlUs->unserialize($result);
  572.  
  573.         if (PEAR::isError($result)) {
  574.             return $result;
  575.         }
  576.         if (!empty($result['document']['result']['error'])) {
  577.             return PEAR::raiseError("Technorati Response Error",
  578.                 $value['document']['result']['error']);
  579.         }
  580.         $unserialized $this->_xmlUs->getUnserializedData();
  581.         if (!empty($unserialized['document']['result']['error'])) {
  582.             return PEAR::raiseError("Technorati Response Error",
  583.                 $value['document']['result']['error']);
  584.         }
  585.         return $unserialized
  586.     }
  587.     
  588.     /**
  589.      * raise errors if options are specified that aren't allowed
  590.      *
  591.      * @access        private
  592.      * @param        array        specified options
  593.      * @param        array        acceptable options
  594.      */
  595.      
  596.     function _checkOptions($current$accepted
  597.     {
  598.         foreach ($current as $option => $value{
  599.             if (in_array($option,$accepted)) {
  600.                 $accepted_options[$option$value;
  601.             else {
  602.                 PEAR::raiseError("Invalid option passed to Query",$option);
  603.             
  604.         }
  605.     
  606. }
  607.  
  608. ?>

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