Source for file Delicious.php
Documentation is available at Delicious.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Stephan Schmidt <schst@php-tools.net> |
// +----------------------------------------------------------------------+
// $Id: Delicious.php 304128 2010-10-06 00:43:14Z clockwerx $
* uses PEAR error management
* uses XML_Serializer to read result
require_once 'XML/Unserializer.php';
* uses HTTP to send the request
require_once 'HTTP/Request2.php';
* Client for the REST-based webservice at http://del.ico.us
* del.icio.us is a site for social bookmarking, that means that you bookmark
* your favourite sites, assign them to one or more topics (tags) and other users
* are able to browse through the bookmarks.
* Services_Delicious allows you to
* your bookmarks from PHP.
* @author Stephan Schmidt <schst@phap-tools.net>
* @author Tatsuya Tsuruoka <ttsuruoka@php.net>
* @package Services_Delicious
protected $_apiUrl = 'https://api.del.icio.us/v1';
* XML_Unserializer, used to parse the XML
* @var object XML_Unserializer
public function __construct($user, $passwd, HTTP_Request2 $request = null )
$request = new HTTP_Request2 ();
$this->_us = &new XML_Unserializer ();
$this->_us->setOption ('parseAttributes', true );
$this->_us->setOption ('forceEnum', array (
public function setRequest(HTTP_Request2 $request) {
$this->request = $request;
* This will return an associative array containing tags
* in the keys and their occurences in the values:
$result = $this->_sendRequest ('tags', 'get');
if (PEAR ::isError ($result)) {
foreach ($result['tag'] as $tmp) {
$tags[$tmp['tag']] = $tmp['count'];
* Retrieve all of a user's bundles
* This will return an associative array containing bundles
* in the keys and their tags in the values:
* [music] => "ipod mp3 music"
$result = $this->_sendRequest ('tags/bundles', 'all');
if (PEAR ::isError ($result)) {
if (!empty ($result['bundle']) && is_array($result['bundle'])) {
if (isset ($result['bundle']['name'])) {
$bundles[$result['bundle']['name']] = $result['bundle']['tags'];
foreach ($result['bundle'] as $bundle) {
$bundles[$bundle['name']] = $bundle['tags'];
* Assign a set of tags to a single bundle
* @param string bundle name
* @param string|array list of tags
$result = $this->_sendRequest ('tags/bundles', 'set', $params);
return $this->_resultToBoolean ($result);
* @param string bundle name
$result = $this->_sendRequest ('tags/bundles', 'delete', $params);
return $this->_resultToBoolean ($result);
$result = $this->_sendRequest ('tags', 'rename', $params);
return $this->_resultToBoolean ($result);
* Get all dates on which posts have been added.
* This will return an associative array containing dates
* in the keys and their occurences in the values:
$result = $this->_sendRequest ('posts', 'dates');
if (PEAR ::isError ($result)) {
foreach ($result['date'] as $tmp) {
$dates[$tmp['date']] = $tmp['count'];
* @param string|array one or more tags
function getPosts($tags = array (), $date = null )
$result = $this->_sendRequest ('posts', 'get', $params);
if (PEAR ::isError ($result)) {
if (!empty ($result['post']) && is_array($result['post'])) {
foreach ($result['post'] as $post) {
$post['tag'] = explode(' ', $post['tag']);
* @param string|array one or more tags
* @param integer maximum amount
$params = array ('count' => $max);
$result = $this->_sendRequest ('posts', 'recent', $params);
if (PEAR ::isError ($result)) {
if (!empty ($result['post']) && is_array($result['post'])) {
foreach ($result['post'] as $post) {
$post['tag'] = explode(' ', $post['tag']);
* @param string|array one or more tags
$result = $this->_sendRequest ('posts', 'all');
if (PEAR ::isError ($result)) {
if (!empty ($result['post']) && is_array($result['post'])) {
foreach ($result['post'] as $post) {
$post['tag'] = explode(' ', $post['tag']);
* @param string|array url or all data for the post
* @param string description
* @param string extended description
* @param string tags for the items
* @param string datestamp
* @param string make the item private if "$shared = 'no'"
function addPost($url, $description = null , $extended = null , $tags = null , $date = null , $shared = null )
if (!isset ($params['dt'])) {
$date = strftime('%Y-%m-%dT%h:%i:%sZ', $date);
'description' => $description,
$result = $this->_sendRequest ('posts', 'add', $params);
return $this->_resultToBoolean ($result);
* @param string|array url or all data for the post
* @param string description
* @param string extended description
$result = $this->_sendRequest ('posts', 'delete', $params);
return $this->_resultToBoolean ($result);
* Returns the last update time for the user
$result = $this->_sendRequest ('posts', 'update');
* Auxiliary method to send a request
* @param string what to fetch
* @param array parameters
* @return array|PEAR_Error
function _sendRequest ($subject, $verb, $params = array ())
$current_time = $sec . sprintf('%03d', (integer) ($usec * 1000 ));
if ($current_time - $last_time < 1000 ) {
return PEAR ::raiseError ('Wait 1 second between queries');
foreach ($params as $key => $value) {
$url = $url . '&' . $key . '=' . urlencode($value);
$this->request->setURL ($url);
$this->request->setHeader ('User-Agent', 'PEAR::Services_Delicious');
$response = $this->request->send ();
if ($response->getStatus () !== 200 ) {
return PEAR ::raiseError ('Invalid Response Code', $response->getStatus ());
$xml = $response->getBody ();
$result = $this->_us->unserialize ($xml);
if (PEAR ::isError ($result)) {
return $this->_us->getUnserializedData ();
* convert a result from del.icio.us to a boolean
function _resultToBoolean ($result)
if (PEAR ::isError ($result)) {
if ($result == 'done' || $result == 'ok') {
if ($result['code'] == 'done') {
$error = $result['code'];
return PEAR ::raiseError ('Error from del.icio.us: '. $error);
Documentation generated on Mon, 11 Mar 2019 15:39:47 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|