Chapter 4. Zend_Service
Prev |  | Next

## Chapter 4. Zend_Service

## 4.1. Zend_Service_Delicious

### 4.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 4.1. Get all posts

    <?php
    $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";
    }
    ?>

### 4.1.2. Retrieving posts

Table 4.1. Methods for retrieving posts

Name | Description
--- | ---
getAllPosts | Get all posts
getRecentPosts | Get recent posts
getPosts | Get post by given date

All of these methods return `Zend_Service_Delicious_PostList` object with retrieving posts in it.

### 4.1.3. Zend_Service_Delicious_PostList

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

Example 4.2. Accessing post lists

    <?php
    $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
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.

### 4.1.4. Editing posts

Example 4.3. Post editing

    <?php
    $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 4.4. Method call chaining

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

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

### 4.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 4.5. Deleting posts

    <?php
    $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());
    ?>

### 4.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 4.6. Adding a post

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

    // create a new post
    $newPost = $delicious->createNewPost('Zend Framework', 'http://framework.zend.com');
    $newPost->setNotes('Zend Framework Home page');
    $newPost->save();
    ?>

### 4.1.7. Tags

Example 4.7. Tags

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

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

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

### 4.1.8. Bundles

Example 4.8. Bundles

    <?php
    $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'));

    ?>

### 4.1.9. Public data

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

Table 4.2. Functions for retrieving public data

Name | Description
--- | ---
getUserFans | Retrieves fans of a user
getUserNetwork | Retrieves network of a user
getUserPosts | Retrieves posts of a user
getUserTags | Retrieves tags of a user

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

Example 4.9. Retrieving public data

    <?php
    // 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'));
    ?>

#### 4.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 4.3. 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

Prev |  | Next
3.2. Zend_Http Cookie Handling | Home | Chapter 5. Zend_Rest