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.1 2004/11/23 19:00:29 schst 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@php-tools.net>
  52.  * @package        Services_Delicious
  53.  * @version        0.1
  54.  */
  55. {
  56.    /**
  57.     * URI of the REST API
  58.     *
  59.     * @access  private
  60.     * @var     string 
  61.     */
  62.     var $_apiUrl 'http://del.icio.us/api';
  63.     
  64.    /**
  65.     * Username
  66.     *
  67.     * @access  private
  68.     * @var     string 
  69.     */
  70.     var $_user   = null;
  71.  
  72.    /**
  73.     * password
  74.     *
  75.     * @access  private
  76.     * @var     string 
  77.     */
  78.     var $_passwd = null;
  79.     
  80.    /**
  81.     * XML_Unserializer, used to parse the XML
  82.     *
  83.     * @access  private
  84.     * @var     object XML_Unserializer 
  85.     */
  86.     var $_us = null;
  87.     
  88.    /**
  89.     * Create a new client
  90.     *
  91.     * @access  public
  92.     * @param   string      username
  93.     * @param   string      password
  94.     */
  95.     function Services_Delicious($user$passwd)
  96.     {
  97.         $this->_user   $user;
  98.         $this->_passwd $passwd;
  99.     }
  100.     
  101.    /**
  102.     * Get all tags
  103.     *
  104.     * This will return an associative array containing tags
  105.     * in the keys and their occurences in the values:
  106.     * <code>
  107.     * Array
  108.     * (
  109.     *    [pear] => 1
  110.     *    [php] => 2
  111.     *)
  112.     *</code>
  113.     *
  114.     * @access  public
  115.     * @return  array 
  116.     */
  117.     function getTags()
  118.     {
  119.         $result $this->_sendRequest('tags''get');
  120.         if (PEAR::isError($result)) {
  121.             return $result;
  122.         }
  123.         $tags = array();
  124.         foreach ($result['tag'as $tmp{
  125.             $tags[$tmp['tag']] $tmp['count'];
  126.         }
  127.         return $tags;
  128.     }
  129.     
  130.    /**
  131.     * Rename a tag
  132.     *
  133.     * @access   public
  134.     * @param    string      old name
  135.     * @param    string      new name
  136.     * @return   boolean 
  137.     */
  138.     function renameTag($old$new)
  139.     {
  140.         $params = array(
  141.                         'old' => $old,
  142.                         'new' => $new
  143.                     );
  144.         $result $this->_sendRequest('tags''rename'$params);
  145.         
  146.         return $this->_resultToBoolean($result);
  147.     }
  148.  
  149.    /**
  150.     * Get all tags
  151.     *
  152.     * This will return an associative array containing dates
  153.     * in the keys and their occurences in the values:
  154.     * <code>
  155.     * Array
  156.     * (
  157.     *    [2004-11-01] => 1
  158.     *    [2004-11-02] => 2
  159.     *)
  160.     *</code>
  161.     *
  162.     * @access  public
  163.     * @return  array 
  164.     */
  165.     function getDates()
  166.     {
  167.         $result $this->_sendRequest('posts''dates');
  168.         if (PEAR::isError($result)) {
  169.             return $result;
  170.         }
  171.         $dates = array();
  172.         foreach ($result['date'as $tmp{
  173.             $dates[$tmp['date']] $tmp['count'];
  174.         }
  175.         return $dates;
  176.     }
  177.     
  178.    /**
  179.     * Get posts
  180.     *
  181.     * @access   public
  182.     * @param    string|array   one or more tags
  183.     * @param    string          date
  184.     * @return   array 
  185.     */
  186.     function getPosts($tags = array()$date = null)
  187.     {
  188.         $params = array();
  189.         if (!empty($tags)) {
  190.             $params['tag'$tags;
  191.         }
  192.         if (!empty($date)) {
  193.             $params['dt'$date;
  194.         }
  195.         
  196.         $result $this->_sendRequest('posts''get'$params);
  197.         if (PEAR::isError($result)) {
  198.             return $result;
  199.         }
  200.  
  201.         $posts  = array();
  202.         foreach ($result['post'as $post{
  203.             $post['tag'explode(' '$post['tag']);
  204.             array_push($posts$post);
  205.         }
  206.         return $posts;
  207.     }
  208.     
  209.    /**
  210.     * Get recent posts
  211.     *
  212.     * @access   public
  213.     * @param    string|array   one or more tags
  214.     * @param    integer         maximum amount
  215.     * @return   array 
  216.     */
  217.     function getRecentPosts($tags = array()$max = 15)
  218.     {
  219.         $params = array('count' => $max);
  220.         if (!empty($tags)) {
  221.             $params['tag'$tags;
  222.         }
  223.         
  224.         $result $this->_sendRequest('posts''get'$params);
  225.         if (PEAR::isError($result)) {
  226.             return $result;
  227.         }
  228.  
  229.         $posts  = array();
  230.         foreach ($result['post'as $post{
  231.             $post['tag'explode(' '$post['tag']);
  232.             array_push($posts$post);
  233.         }
  234.         
  235.         return $posts;
  236.     }
  237.  
  238.    /**
  239.     * Get all posts
  240.     *
  241.     * @access   public
  242.     * @param    string|array   one or more tags
  243.     * @param    string          date
  244.     * @return   array 
  245.     */
  246.     function getAllPosts()
  247.     {
  248.         $result $this->_sendRequest('posts''all');
  249.         if (PEAR::isError($result)) {
  250.             return $result;
  251.         }
  252.  
  253.         $posts  = array();
  254.         foreach ($result['post'as $post{
  255.             $post['tag'explode(' '$post['tag']);
  256.             array_push($posts$post);
  257.         }
  258.         
  259.         return $posts;
  260.     }
  261.  
  262.    /**
  263.     * Add a post
  264.     *
  265.     * @access   public
  266.     * @param    string|array   url or all data for the post
  267.     * @param    string          description
  268.     * @param    string          extended description
  269.     * @param 
  270.     * @return   boolean 
  271.     */
  272.     function addPost($url$description = null$extended = null$tags = null$date = null)
  273.     {
  274.         if (is_array($url)) {
  275.             $params $url;
  276.             if (!isset($params['dt'])) {
  277.                 $params['dt'strftime('%Y-%m-%dT%h:%i:%sZ'time());
  278.             }
  279.         else {
  280.             if (is_null($date)) {
  281.                 $date strftime('%Y-%m-%dT%h:%i:%sZ'time());
  282.             else {
  283.                 $tmp strtotime($date);
  284.                 if ($tmp{
  285.                     $date strftime('%Y-%m-%dT%h:%i:%sZ'$date);
  286.                 }
  287.             }
  288.             $params = array(
  289.                              'url'         => $url,
  290.                              'description' => $description,
  291.                              'extended'    => $extended,
  292.                              'tags'         => $tags,
  293.                              'dt'          => $date
  294.                             );
  295.         }
  296.         
  297.         $result $this->_sendRequest('posts''add'$params);
  298.         
  299.         return $this->_resultToBoolean($result);
  300.     }
  301.  
  302.    /**
  303.     * Delete a post
  304.     *
  305.     * @access   public
  306.     * @param    string|array   url or all data for the post
  307.     * @param    string          description
  308.     * @param    string          extended description
  309.     * @param 
  310.     * @return   boolean 
  311.     */
  312.     function deletePost($url)
  313.     {
  314.         $params = array(
  315.                          'url' => $url
  316.                        );
  317.  
  318.         $result $this->_sendRequest('posts''delete'$params);
  319.         
  320.         return $this->_resultToBoolean($result);
  321.     }
  322.  
  323.    /**
  324.     * Auxiliary method to send a request
  325.     *
  326.     * @access   private
  327.     * @param    string      what to fetch
  328.     * @param    string      action
  329.     * @param    array       parameters
  330.     * @return   array|PEAR_Error
  331.     */
  332.     function _sendRequest($subject$verb$params = array())
  333.     {
  334.         $url sprintf('%s/%s/%s?'$this->_apiUrl$subject$verb);
  335.         foreach ($params as $key => $value{
  336.             if (is_array($value)) {
  337.                 $value implode(' '$value);
  338.             }
  339.             $url $url '&' $key '=' urlencode($value);
  340.         }
  341.         
  342.         $request &new HTTP_Request($url);
  343.         $request->setBasicAuth($this->_user$this->_passwd);
  344.         $request->addHeader('User-Agent''PEAR::Services_Delicious' );
  345.         
  346.         $request->sendRequest();
  347.         if ($request->getResponseCode(!== 200{
  348.             return PEAR::raiseError('Invalid Response Code'$request->getResponseCode());
  349.         }
  350.         
  351.         $xml $request->getResponseBody();
  352.         
  353.         if (!is_object($this->_us)) {
  354.             $this->_us &new XML_Unserializer();
  355.             $this->_us->setOption('parseAttributes'true);
  356.             $this->_us->setOption('forceEnum'array(
  357.                                                        'tag',
  358.                                                        'post',
  359.                                                        'date'
  360.                                                    )
  361.                                   );
  362.         }
  363.         
  364.         $result $this->_us->unserialize($xml);
  365.         if (PEAR::isError($result)) {
  366.             return $result;
  367.         }
  368.         return $this->_us->getUnserializedData();
  369.     }
  370.  
  371.    /**
  372.     * convert a result from del.icio.us to a boolean
  373.     * value or PEAR_Error
  374.     *
  375.     * @access   private
  376.     * @param    mixed 
  377.     * @return   boolean 
  378.     */
  379.     function _resultToBoolean($result)
  380.     {
  381.         if (PEAR::isError($result)) {
  382.             return $result;
  383.         }
  384.         if ($result == 'done'{
  385.             return true;
  386.         }
  387.         
  388.         if ($result['code'== 'done'{
  389.             return true;
  390.         }
  391.         if (is_string($result)) {
  392.             $error $result;
  393.         else {
  394.             $error $result['code'];
  395.         }
  396.         return PEAR::raiseError('Error from del.icio.us: '$error);
  397.     }
  398. }
  399. ?>

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