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 5                                                        |
  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 304128 2010-10-06 00:43:14Z clockwerx $
  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/Request2.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@php.net>
  53.  * @package     Services_Delicious
  54.  * @version     0.5
  55.  */
  56. {
  57.     /**
  58.      * URI of the REST API
  59.      *
  60.      * @access  private
  61.      * @var     string 
  62.      */
  63.     protected $_apiUrl = 'https://api.del.icio.us/v1';
  64.  
  65.     /**
  66.      * Username
  67.      *
  68.      * @access  private
  69.      * @var     string 
  70.      */
  71.     protected $_user   = null;
  72.  
  73.     /**
  74.      * password
  75.      *
  76.      * @access  private
  77.      * @var     string 
  78.      */
  79.     protected $_passwd = null;
  80.  
  81.     /**
  82.      * XML_Unserializer, used to parse the XML
  83.      *
  84.      * @access  private
  85.      * @var     object XML_Unserializer 
  86.      */
  87.     protected $_us = null;
  88.  
  89.     /**
  90.      * Last accessed time
  91.      *
  92.      * @access  private
  93.      * @var     integer 
  94.      */
  95.     protected $_last_time = null;
  96.  
  97.     /**
  98.      * Create a new client
  99.      *
  100.      * @access  public
  101.      * @param   string      username
  102.      * @param   string      password
  103.      */
  104.     public function __construct($user$passwdHTTP_Request2 $request = null)
  105.     {
  106.         $this->_user   = $user;
  107.         $this->_passwd = $passwd;
  108.  
  109.         if (empty($request)) {
  110.             $request = new HTTP_Request2();
  111.         }
  112.  
  113.         $this->setRequest($request);
  114.  
  115.         $this->_us = &new XML_Unserializer();
  116.         $this->_us->setOption('parseAttributes'true);
  117.         $this->_us->setOption('forceEnum'array(
  118.                                                    'tag',
  119.                                                    'post',
  120.                                                    'date'
  121.                                                 )
  122.                              );
  123.  
  124.     }
  125.  
  126.     public function setRequest(HTTP_Request2 $request{
  127.         $this->request $request;
  128.     }
  129.  
  130.     /**
  131.      * Get all tags
  132.      *
  133.      * This will return an associative array containing tags
  134.      * in the keys and their occurences in the values:
  135.      * <code>
  136.      * Array
  137.      * (
  138.      *    [pear] => 1
  139.      *    [php] => 2
  140.      *)
  141.      *</code>
  142.      *
  143.      * @access  public
  144.      * @return  array 
  145.      */
  146.     function getTags()
  147.     {
  148.         $result $this->_sendRequest('tags''get');
  149.         if (PEAR::isError($result)) {
  150.             return $result;
  151.         }
  152.         $tags = array();
  153.         if (is_array($result['tag'])) {
  154.             foreach ($result['tag'as $tmp{
  155.                 $tags[$tmp['tag']] $tmp['count'];
  156.             }
  157.         }
  158.         return $tags;
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Retrieve all of a user's bundles
  164.      *
  165.      * This will return an associative array containing bundles
  166.      * in the keys and their tags in the values:
  167.      * <code>
  168.      * Array
  169.      * (
  170.      *    [music] => "ipod mp3 music"
  171.      * )
  172.      * </code>
  173.      *
  174.      * @access   public
  175.      * @return   array 
  176.      */
  177.     function getTagsBundles()
  178.     {
  179.         $result $this->_sendRequest('tags/bundles''all');
  180.         if (PEAR::isError($result)) {
  181.             return $result;
  182.         }
  183.  
  184.         $bundles = array();
  185.         if (!empty($result['bundle']&& is_array($result['bundle'])) {
  186.             if (isset($result['bundle']['name'])) {
  187.                 $bundles[$result['bundle']['name']] $result['bundle']['tags'];
  188.             else {
  189.                 foreach ($result['bundle'as $bundle{
  190.                     $bundles[$bundle['name']] $bundle['tags'];
  191.                 }
  192.             }
  193.         }
  194.  
  195.         return $bundles;
  196.     }
  197.  
  198.     /**
  199.      * Assign a set of tags to a single bundle
  200.      *
  201.      * @access   public
  202.      * @param    string          bundle name
  203.      * @param    string|array   list of tags
  204.      * @return   boolean 
  205.      */
  206.     function setTagsBundle($bundle$tags)
  207.     {
  208.         if (is_array($tags)) {
  209.             $tags implode(" "$tags);
  210.         }
  211.  
  212.         $params = array(
  213.                         'bundle'    => $bundle,
  214.                         'tags'      => $tags
  215.                     );
  216.  
  217.         $result $this->_sendRequest('tags/bundles''set'$params);
  218.  
  219.         return $this->_resultToBoolean($result);
  220.     }
  221.  
  222.     /**
  223.      * Delete a bundle
  224.      *
  225.      * @acces    public
  226.      * @param    string      bundle name
  227.      * @returnn  boolean
  228.      */
  229.     function deleteTagsBundle($bundle)
  230.     {
  231.         $params = array(
  232.                         'bundle' => $bundle
  233.                     );
  234.  
  235.         $result $this->_sendRequest('tags/bundles''delete'$params);
  236.  
  237.         return $this->_resultToBoolean($result);
  238.     }
  239.  
  240.     /**
  241.      * Rename a tag
  242.      *
  243.      * @access   public
  244.      * @param    string      old name
  245.      * @param    string      new name
  246.      * @return   boolean 
  247.      */
  248.     function renameTag($old$new)
  249.     {
  250.         $params = array(
  251.                         'old' => $old,
  252.                         'new' => $new
  253.                     );
  254.         $result $this->_sendRequest('tags''rename'$params);
  255.  
  256.         return $this->_resultToBoolean($result);
  257.     }
  258.  
  259.     /**
  260.      * Get all dates on which posts have been added.
  261.      *
  262.      * This will return an associative array containing dates
  263.      * in the keys and their occurences in the values:
  264.      * <code>
  265.      * Array
  266.      * (
  267.      *    [2004-11-01] => 1
  268.      *    [2004-11-02] => 2
  269.      *)
  270.      *</code>
  271.      *
  272.      * @access  public
  273.      * @return  array 
  274.      */
  275.     function getDates()
  276.     {
  277.         $result $this->_sendRequest('posts''dates');
  278.         if (PEAR::isError($result)) {
  279.             return $result;
  280.         }
  281.         $dates = array();
  282.         if (is_array($result['date'])) {
  283.             foreach ($result['date'as $tmp{
  284.                 $dates[$tmp['date']] $tmp['count'];
  285.             }
  286.         }
  287.         return $dates;
  288.     }
  289.  
  290.     /**
  291.      * Get posts
  292.      *
  293.      * @access   public
  294.      * @param    string|array   one or more tags
  295.      * @param    string          date
  296.      * @return   array 
  297.      */
  298.     function getPosts($tags = array()$date = null)
  299.     {
  300.         $params = array();
  301.         if (!empty($tags)) {
  302.             $params['tag'$tags;
  303.         }
  304.         if (!empty($date)) {
  305.             $params['dt'$date;
  306.         }
  307.  
  308.         $result $this->_sendRequest('posts''get'$params);
  309.         if (PEAR::isError($result)) {
  310.             return $result;
  311.         }
  312.  
  313.         $posts  = array();
  314.         if (!empty($result['post']&& is_array($result['post'])) {
  315.             foreach ($result['post'as $post{
  316.                 $post['tag'explode(' '$post['tag']);
  317.                 $posts[$post;
  318.             }
  319.         }
  320.         return $posts;
  321.     }
  322.  
  323.     /**
  324.      * Get recent posts
  325.      *
  326.      * @access   public
  327.      * @param    string|array   one or more tags
  328.      * @param    integer         maximum amount
  329.      * @return   array 
  330.      */
  331.     function getRecentPosts($tags = array()$max = 15)
  332.     {
  333.         $params = array('count' => $max);
  334.         if (!empty($tags)) {
  335.             $params['tag'$tags;
  336.         }
  337.  
  338.         $result $this->_sendRequest('posts''recent'$params);
  339.         if (PEAR::isError($result)) {
  340.             return $result;
  341.         }
  342.  
  343.         $posts  = array();
  344.         if (!empty($result['post']&& is_array($result['post'])) {
  345.             foreach ($result['post'as $post{
  346.                 $post['tag'explode(' '$post['tag']);
  347.                 $posts[$post;
  348.             }
  349.         }
  350.  
  351.         return $posts;
  352.     }
  353.  
  354.     /**
  355.      * Get all posts
  356.      *
  357.      * @access   public
  358.      * @param    string|array   one or more tags
  359.      * @param    string          date
  360.      * @return   array 
  361.      */
  362.     function getAllPosts()
  363.     {
  364.         $result $this->_sendRequest('posts''all');
  365.         if (PEAR::isError($result)) {
  366.             return $result;
  367.         }
  368.  
  369.         $posts  = array();
  370.         if (!empty($result['post']&& is_array($result['post'])) {
  371.             foreach ($result['post'as $post{
  372.                 $post['tag'explode(' '$post['tag']);
  373.                 $posts[$post;
  374.             }
  375.         }
  376.  
  377.         return $posts;
  378.     }
  379.  
  380.     /**
  381.      * Add a post
  382.      *
  383.      * @access   public
  384.      * @param    string|array   url or all data for the post
  385.      * @param    string          description
  386.      * @param    string          extended description
  387.      * @param    string          tags for the items
  388.      * @param    string          datestamp
  389.      * @param    string          make the item private if "$shared = 'no'"
  390.      * @return   boolean 
  391.      */
  392.     function addPost($url$description = null$extended = null$tags = null$date = null$shared = null)
  393.     {
  394.         if (is_array($url)) {
  395.             $params $url;
  396.             if (!isset($params['dt'])) {
  397.                 $params['dt'strftime('%Y-%m-%dT%h:%i:%sZ'time());
  398.             }
  399.         else {
  400.             if (is_null($date)) {
  401.                 $date strftime('%Y-%m-%dT%h:%i:%sZ'time());
  402.             else {
  403.                 $tmp strtotime($date);
  404.                 if ($tmp{
  405.                     $date strftime('%Y-%m-%dT%h:%i:%sZ'$date);
  406.                 }
  407.             }
  408.             $params = array(
  409.                              'url'         => $url,
  410.                              'description' => $description,
  411.                              'extended'    => $extended,
  412.                              'tags'        => $tags,
  413.                              'dt'          => $date,
  414.                              'shared'      => $shared
  415.                             );
  416.         }
  417.  
  418.         $result $this->_sendRequest('posts''add'$params);
  419.  
  420.         return $this->_resultToBoolean($result);
  421.     }
  422.  
  423.     /**
  424.      * Delete a post
  425.      *
  426.      * @access   public
  427.      * @param    string|array   url or all data for the post
  428.      * @param    string          description
  429.      * @param    string          extended description
  430.      * @param 
  431.      * @return   boolean 
  432.      */
  433.     function deletePost($url)
  434.     {
  435.         $params = array(
  436.                          'url' => $url
  437.                        );
  438.  
  439.         $result $this->_sendRequest('posts''delete'$params);
  440.  
  441.         return $this->_resultToBoolean($result);
  442.     }
  443.  
  444.     /**
  445.      * Returns the last update time for the user
  446.      *
  447.      * @access   public
  448.      * @return   string 
  449.      */
  450.     function getLastUpdate()
  451.     {
  452.         $result $this->_sendRequest('posts''update');
  453.  
  454.         return $result['time'];
  455.     }
  456.  
  457.     /**
  458.      * Auxiliary method to send a request
  459.      *
  460.      * @access   private
  461.      * @param    string      what to fetch
  462.      * @param    string      action
  463.      * @param    array       parameters
  464.      * @return   array|PEAR_Error
  465.      */
  466.     function _sendRequest($subject$verb$params = array())
  467.     {
  468.         list($usec$secexplode(' 'microtime());
  469.         $current_time $sec sprintf('%03d'(integer)($usec * 1000));
  470.         $last_time $this->_last_time;
  471.         $this->_last_time = $current_time;
  472.         if ($current_time $last_time < 1000{
  473.             return PEAR::raiseError('Wait 1 second between queries');
  474.         }
  475.  
  476.         $url sprintf('%s/%s/%s?'$this->_apiUrl$subject$verb);
  477.         foreach ($params as $key => $value{
  478.             if (is_array($value)) {
  479.                 $value implode(' '$value);
  480.             }
  481.             $url $url '&' $key '=' urlencode($value);
  482.         }
  483.  
  484.         $this->request->setURL($url);
  485.         $this->request->setAuth($this->_user$this->_passwd);
  486.         $this->request->setHeader('User-Agent''PEAR::Services_Delicious');
  487.  
  488.         $response $this->request->send();
  489.         if ($response->getStatus(!== 200{
  490.             return PEAR::raiseError('Invalid Response Code'$response->getStatus());
  491.         }
  492.  
  493.         $xml $response->getBody();
  494.  
  495.         $result $this->_us->unserialize($xml);
  496.         if (PEAR::isError($result)) {
  497.             return $result;
  498.         }
  499.         return $this->_us->getUnserializedData();
  500.     }
  501.  
  502.     /**
  503.      * convert a result from del.icio.us to a boolean
  504.      * value or PEAR_Error
  505.      *
  506.      * @access   private
  507.      * @param    mixed 
  508.      * @return   boolean 
  509.      */
  510.     function _resultToBoolean($result)
  511.     {
  512.         if (PEAR::isError($result)) {
  513.             return $result;
  514.         }
  515.         if ($result == 'done' || $result == 'ok'{
  516.             return true;
  517.         }
  518.  
  519.         if ($result['code'== 'done'{
  520.             return true;
  521.         }
  522.         if (is_string($result)) {
  523.             $error $result;
  524.         else {
  525.             $error $result['code'];
  526.         }
  527.         return PEAR::raiseError('Error from del.icio.us: '$error);
  528.     }
  529. }

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