Chapter 2. Zend_Service

Table of Contents

2.1. Zend_Service_Delicious
2.1.1. Introduction
2.1.2. Retrieving posts
2.1.3. Zend_Service_Delicious_PostList
2.1.4. Editing posts
2.1.5. Deleting posts
2.1.6. Adding new posts
2.1.7. Tags
2.1.8. Bundles
2.1.9. Public data
2.2. Zend_Service_Simpy
2.2.1. Introduction
2.2.2. Links
2.2.3. Tags
2.2.4. Notes
2.2.5. Watchlists

2.1. Zend_Service_Delicious

2.1.1. Introduction

Zend_Service_Delicious is simple API for using del.icio.us XML and JSON web services. This component gives you read-write access to posts at del.icio.us if you provide credentials. It also allows read-only access to public data of all users.

Example 2.1. Get all posts

$delicious = new Zend_Service_Delicious('username', 'password');
$posts = $delicious->getAllPosts();

foreach ($posts as $post) {
    echo "--\n";
    echo "Title: {$post->getTitle()}\n";
    echo "Url: {$post->getUrl()}\n";
}
            

2.1.2. Retrieving posts

Class provides three methods for post retrieving: getPosts, getRecentPosts and getAllPosts. All of these methods return Zend_Service_Delicious_PostList object which holds all retrieved posts.

/**
 * Get posts matching the arguments. If no date or url is given, most recent date will be used.
 *
 * @param string $tag Optional filtering by tag
 * @param Zend_Date $dt Optional filtering by date
 * @param string $url Optional filtering by url
 * @return Zend_Service_Delicious_PostList
 */
public function getPosts($tag = null, $dt = null, $url = null);
            
/**
 * Get recent posts
 *
 * @param string $tag   Optional filtering by tag
 * @param string $count Maximal number of posts to be returned (default 15)
 * @return Zend_Service_Delicious_PostList
 */
public function getRecentPosts($tag = null, $count = 15);
        
/**
 * Get all posts
 *
 * @param string $tag Optional filtering by tag
 * @return Zend_Service_Delicious_PostList
 */
public function getAllPosts($tag = null);
        

2.1.3. Zend_Service_Delicious_PostList

For easier data access this class implements Countable, Iterator and ArrayAccess interfaces.

Example 2.2. Accessing post lists

$delicious = new Zend_Service_Delicious('username', 'password');
$posts = $delicious->getAllPosts();

// count posts
echo count($posts);

// iterate over posts
foreach ($posts as $post) {
    echo "--\n";
    echo "Title: {$post->getTitle()}\n";
    echo "Url: {$post->getUrl()}\n";
}

// get post as from the array
echo $posts[0]->getTitle();
            

[Note] Note

Methods ArrayAccess::offsetSet() and ArrayAccess::offsetUnset() in this implementation just throw errors. So code like unset($posts[0]); and $posts[0] = 'A'; will throw exception because this properties are read-only.

2.1.4. Editing posts

Example 2.3. Post editing

$delicious = new Zend_Service_Delicious('username', 'password');
$posts = $delicious->getPosts();

// set title
$posts[0]->setTitle('New title');
// save changes
$posts[0]->save();
            

Every setter method returns the post so you can chain method calls.

Example 2.4. Method call chaining

$delicious = new Zend_Service_Delicious('username', 'password');
$posts = $delicious->getPosts();

$posts[0]->setTitle('New title')
         ->setNotes('New notes')
         ->save();
            

2.1.5. Deleting posts

There are two way to delete a post, by specifying post URL or by calling delete() method on a post.

Example 2.5. Deleting posts

$delicious = new Zend_Service_Delicious('username', 'password');

// by specifying date
$delicious->deletePost('http://framework.zend.com');

// or by calling a method on a post
$posts = $delicious->getPosts();
$posts[0]->delete();

// the second way actually does this
$delicious->deletePost($posts[0]->getUrl());
            

2.1.6. Adding new posts

When adding a post first you need to call createNewPost() method which returns Zend_Service_Delicious_Post object. When you edit the post you need to save it to del.icio.us database by calling a save() method.

Example 2.6. Adding a post

$delicious = new Zend_Service_Delicious('username', 'password');

// create a new post and save it (with method call chaining)
$delicious->createNewPost('Zend Framework', 'http://framework.zend.com')
          ->setNotes('Zend Framework Homepage')
          ->save();

// create a new post and save it  (without method call chaining)
$newPost = $delicious->createNewPost('Zend Framework', 'http://framework.zend.com'); 
$newPost->setNotes('Zend Framework Homepage'); 
$newPost->save(); 
            

2.1.7. Tags

Example 2.7. Tags

$delicious = new Zend_Service_Delicious('username', 'password');

// get all tags
print_r($delicious->getTags());

// rename tag ZF to zendFramework
$delicious->renameTag('ZF', 'zendFramework');
            

2.1.8. Bundles

Example 2.8. Bundles

$delicious = new Zend_Service_Delicious('username', 'password');

// get all bundles
print_r($delicious->getBundles());

// delete bundle someBundle
$delicious->deleteBundle('someBundle');

// add bundle
$delicious->addBundle('newBundle', array('tag1', 'tag2'));
            

2.1.9. Public data

del.icio.us web API allows access to data of all users.

Table 2.1. Functions for retrieving public data

Name Description Return type
getUserFans Retrieves fans of a user Array
getUserNetwork Retrieves network of a user Array
getUserPosts Retrieves posts of a user Zend_Service_Delicious_PostList
getUserTags Retrieves tags of a user Array

[Note] Note

When using only these methods username and password are note required when constructing Zend_Service_Delicious object.

Example 2.9. Retrieving public data

// username and password are not required
$delicious = new Zend_Service_Delicious();

// get fans of user someUser
print_r($delicious->getUserFans('someUser'));

// get network of user someUser
print_r($delicious->getUserNetwork('someUser'));

// get tags of user someUser
print_r($delicious->getUserTags('someUser'));
            

2.1.9.1. Public posts

When retrieving public posts with getUserPosts() method the Zend_Service_Delicious_PostList object is returned but it contains Zend_Service_Delicious_SimplePost objects which hold only basic data of a post, this objects only contain URL, title, notes and tags.

Table 2.2. Methods of Zend_Service_Delicious_SimplePost class

Name Description Return type
getNotes Returns notes of a post String
getTags Returns tags of a post Array
getTitle Returns title of a post String
getUrl Returns URL of a post String