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

Source for file Delicious.php

Documentation is available at Delicious.php

  1. <?PHP
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.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. // | Authors: Stephan Schmidt <schst@php-tools.net>                       |
  17. // +----------------------------------------------------------------------+
  18. //
  19. //    $Id: Delicious.php,v 1.4 2006/04/23 16:37:29 ttsuruoka Exp $
  20.  
  21. /**
  22.  * uses PEAR error management
  23.  */
  24. require_once 'PEAR.php';
  25.  
  26. /**
  27.  * uses XML_Serializer to read result
  28.  */
  29. require_once 'XML/Unserializer.php';
  30.  
  31. /**
  32.  * uses HTTP to send the request
  33.  */
  34. require_once 'HTTP/Request.php';
  35.  
  36. /**
  37.  * Services_Delicious
  38.  *
  39.  * Client for the REST-based webservice at http://del.ico.us
  40.  *
  41.  * del.icio.us is a site for social bookmarking, that means that you bookmark
  42.  * your favourite sites, assign them to one or more topics (tags) and other users
  43.  * are able to browse through the bookmarks.
  44.  *
  45.  * Services_Delicious allows you to
  46.  * - get
  47.  * - add
  48.  * - delete
  49.  * your bookmarks from PHP.
  50.  *
  51.  * @author      Stephan Schmidt <schst@phap-tools.net>
  52.  * @author      Tatsuya Tsuruoka <ttsuruoka@p4life.jp>
  53.  * @package     Services_Delicious
  54.  * @version     0.3
  55.  */
  56. {
  57.    /**
  58.     * URI of the REST API
  59.     *
  60.     * @access  private
  61.     * @var     string 
  62.     */
  63.     var $_apiUrl 'http://del.icio.us/api';
  64.     
  65.    /**
  66.     * Username
  67.     *
  68.     * @access  private
  69.     * @var     string 
  70.     */
  71.     var $_user   = null;
  72.  
  73.    /**
  74.     * password
  75.     *
  76.     * @access  private
  77.     * @var     string 
  78.     */
  79.     var $_passwd = null;
  80.     
  81.    /**
  82.     * XML_Unserializer, used to parse the XML
  83.     *
  84.     * @access  private
  85.     * @var     object XML_Unserializer 
  86.     */
  87.     var $_us = null;
  88.     
  89.    /**
  90.     * Last accessed time
  91.     *
  92.     * @access  private
  93.     * @var     integer 
  94.     */
  95.     var $_last_time = null;
  96.  
  97.    /**
  98.     * Create a new client
  99.     *
  100.     * @access  public
  101.     * @param   string      username
  102.     * @param   string      password
  103.     */
  104.     function Services_Delicious($user$passwd)
  105.     {
  106.         $this->_user   $user;
  107.         $this->_passwd $passwd;
  108.     }
  109.     
  110.    /**
  111.     * Get all tags
  112.     *
  113.     * This will return an associative array containing tags
  114.     * in the keys and their occurences in the values:
  115.     * <code>
  116.     * Array
  117.     * (
  118.     *    [pear] => 1
  119.     *    [php] => 2
  120.     *)
  121.     *</code>
  122.     *
  123.     * @access  public
  124.     * @return  array 
  125.     */
  126.     function getTags()
  127.     {
  128.         $result $this->_sendRequest('tags''get');
  129.         if (PEAR::isError($result)) {
  130.             return $result;
  131.         }
  132.         $tags = array();
  133.         foreach ($result['tag'as $tmp{
  134.             $tags[$tmp['tag']] $tmp['count'];
  135.         }
  136.         return $tags;
  137.     }
  138.     
  139.    /**
  140.     * Rename a tag
  141.     *
  142.     * @access   public
  143.     * @param    string      old name
  144.     * @param    string      new name
  145.     * @return   boolean 
  146.     */
  147.     function renameTag($old$new)
  148.     {
  149.         $params = array(
  150.                         'old' => $old,
  151.                         'new' => $new
  152.                     );
  153.         $result $this->_sendRequest('tags''rename'$params);
  154.         
  155.         return $this->_resultToBoolean($result);
  156.     }
  157.  
  158.    /**
  159.     * Get all dates on which posts have been added.
  160.     *
  161.     * This will return an associative array containing dates
  162.     * in the keys and their occurences in the values:
  163.     * <code>
  164.     * Array
  165.     * (
  166.     *    [2004-11-01] => 1
  167.     *    [2004-11-02] => 2
  168.     *)
  169.     *</code>
  170.     *
  171.     * @access  public
  172.     * @return  array 
  173.     */
  174.     function getDates()
  175.     {
  176.         $result $this->_sendRequest('posts''dates');
  177.         if (PEAR::isError($result)) {
  178.             return $result;
  179.         }
  180.         $dates = array();
  181.         foreach ($result['date'as $tmp{
  182.             $dates[$tmp['date']] $tmp['count'];
  183.         }
  184.         return $dates;
  185.     }
  186.     
  187.    /**
  188.     * Get posts
  189.     *
  190.     * @access   public
  191.     * @param    string|array   one or more tags
  192.     * @param    string          date
  193.     * @return   array 
  194.     */
  195.     function getPosts($tags = array()$date = null)
  196.     {
  197.         $params = array();
  198.         if (!empty($tags)) {
  199.             $params['tag'$tags;
  200.         }
  201.         if (!empty($date)) {
  202.             $params['dt'$date;
  203.         }
  204.         
  205.         $result $this->_sendRequest('posts''get'$params);
  206.         if (PEAR::isError($result)) {
  207.             return $result;
  208.         }
  209.  
  210.         $posts  = array();
  211.         foreach ($result['post'as $post{
  212.             $post['tag'explode(' '$post['tag']);
  213.             array_push($posts$post);
  214.         }
  215.         return $posts;
  216.     }
  217.     
  218.    /**
  219.     * Get recent posts
  220.     *
  221.     * @access   public
  222.     * @param    string|array   one or more tags
  223.     * @param    integer         maximum amount
  224.     * @return   array 
  225.     */
  226.     function getRecentPosts($tags = array()$max = 15)
  227.     {
  228.         $params = array('count' => $max);
  229.         if (!empty($tags)) {
  230.             $params['tag'$tags;
  231.         }
  232.         
  233.         $result $this->_sendRequest('posts''recent'$params);
  234.         if (PEAR::isError($result)) {
  235.             return $result;
  236.         }
  237.  
  238.         $posts  = array();
  239.         foreach ($result['post'as $post{
  240.             $post['tag'explode(' '$post['tag']);
  241.             array_push($posts$post);
  242.         }
  243.         
  244.         return $posts;
  245.     }
  246.  
  247.    /**
  248.     * Get all posts
  249.     *
  250.     * @access   public
  251.     * @param    string|array   one or more tags
  252.     * @param    string          date
  253.     * @return   array 
  254.     */
  255.     function getAllPosts()
  256.     {
  257.         $result $this->_sendRequest('posts''all');
  258.         if (PEAR::isError($result)) {
  259.             return $result;
  260.         }
  261.  
  262.         $posts  = array();
  263.         foreach ($result['post'as $post{
  264.             $post['tag'explode(' '$post['tag']);
  265.             array_push($posts$post);
  266.         }
  267.         
  268.         return $posts;
  269.     }
  270.  
  271.    /**
  272.     * Add a post
  273.     *
  274.     * @access   public
  275.     * @param    string|array   url or all data for the post
  276.     * @param    string          description
  277.     * @param    string          extended description
  278.     * @param 
  279.     * @return   boolean 
  280.     */
  281.     function addPost($url$description = null$extended = null$tags = null$date = null)
  282.     {
  283.         if (is_array($url)) {
  284.             $params $url;
  285.             if (!isset($params['dt'])) {
  286.                 $params['dt'strftime('%Y-%m-%dT%h:%i:%sZ'time());
  287.             }
  288.         else {
  289.             if (is_null($date)) {
  290.                 $date strftime('%Y-%m-%dT%h:%i:%sZ'time());
  291.             else {
  292.                 $tmp strtotime($date);
  293.                 if ($tmp{
  294.                     $date strftime('%Y-%m-%dT%h:%i:%sZ'$date);
  295.                 }
  296.             }
  297.             $params = array(
  298.                              'url'         => $url,
  299.                              'description' => $description,
  300.                              'extended'    => $extended,
  301.                              'tags'         => $tags,
  302.                              'dt'          => $date
  303.                             );
  304.         }
  305.         
  306.         $result $this->_sendRequest('posts''add'$params);
  307.         
  308.         return $this->_resultToBoolean($result);
  309.     }
  310.  
  311.    /**
  312.     * Delete a post
  313.     *
  314.     * @access   public
  315.     * @param    string|array   url or all data for the post
  316.     * @param    string          description
  317.     * @param    string          extended description
  318.     * @param 
  319.     * @return   boolean 
  320.     */
  321.     function deletePost($url)
  322.     {
  323.         $params = array(
  324.                          'url' => $url
  325.                        );
  326.  
  327.         $result $this->_sendRequest('posts''delete'$params);
  328.         
  329.         return $this->_resultToBoolean($result);
  330.     }
  331.  
  332.    /**
  333.     * Auxiliary method to send a request
  334.     *
  335.     * @access   private
  336.     * @param    string      what to fetch
  337.     * @param    string      action
  338.     * @param    array       parameters
  339.     * @return   array|PEAR_Error
  340.     */
  341.     function _sendRequest($subject$verb$params = array())
  342.     {
  343.         list($usec$secexplode(' 'microtime());
  344.         $current_time $sec sprintf('%03d'(integer)($usec * 1000));
  345.         $last_time $this->_last_time;
  346.         $this->_last_time $current_time;
  347.         if ($current_time $last_time < 1000{
  348.             return PEAR::raiseError('Wait 1 second between queries');
  349.         }
  350.         
  351.         $url sprintf('%s/%s/%s?'$this->_apiUrl$subject$verb);
  352.         foreach ($params as $key => $value{
  353.             if (is_array($value)) {
  354.                 $value implode(' '$value);
  355.             }
  356.             $url $url '&' $key '=' urlencode($value);
  357.         }
  358.         
  359.         $request &new HTTP_Request($url);
  360.         $request->setBasicAuth($this->_user$this->_passwd);
  361.         $request->addHeader('User-Agent''PEAR::Services_Delicious' );
  362.         
  363.         $request->sendRequest();
  364.         if ($request->getResponseCode(!== 200{
  365.             return PEAR::raiseError('Invalid Response Code'$request->getResponseCode());
  366.         }
  367.         
  368.         $xml $request->getResponseBody();
  369.         
  370.         if (!is_object($this->_us)) {
  371.             $this->_us &new XML_Unserializer();
  372.             $this->_us->setOption('parseAttributes'true);
  373.             $this->_us->setOption('forceEnum'array(
  374.                                                        'tag',
  375.                                                        'post',
  376.                                                        'date'
  377.                                                     )
  378.                                  );
  379.         }
  380.         
  381.         $result $this->_us->unserialize($xml);
  382.         if (PEAR::isError($result)) {
  383.             return $result;
  384.         }
  385.         return $this->_us->getUnserializedData();
  386.     }
  387.  
  388.    /**
  389.     * convert a result from del.icio.us to a boolean
  390.     * value or PEAR_Error
  391.     *
  392.     * @access   private
  393.     * @param    mixed 
  394.     * @return   boolean 
  395.     */
  396.     function _resultToBoolean($result)
  397.     {
  398.         if (PEAR::isError($result)) {
  399.             return $result;
  400.         }
  401.         if ($result == 'done'{
  402.             return true;
  403.         }
  404.         
  405.         if ($result['code'== 'done'{
  406.             return true;
  407.         }
  408.         if (is_string($result)) {
  409.             $error $result;
  410.         else {
  411.             $error $result['code'];
  412.         }
  413.         return PEAR::raiseError('Error from del.icio.us: '$error);
  414.     }
  415. }
  416. ?>

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