Getting started with REST using Zend Framework

Featured Image

I was playing with SOAP for the last few days. I must say, it looks somewhat complicated in some areas. After some google-ing on SOAP vs REST stuff I decided to write this simple ZendFramework REST how to. Actualy this is a PHP part of tutorial since I was working on a PHP and C# solution using Mono framework for C# part can be found on my site. My desire was to create a web service in PHP environment and consume that service in C# environment.Getting started with REST using ZendFramework. Here’s the PHP REST server part.

Basic example is really, really simple. Aren’t they always. Seriously, it’s not my intention to go into outermost details.

Creating a REST server in ZendFramework can be simple as 3 lines of code. For the sake of simplicity, let’s suppose we’ll place all of our code logic in one file, the IndexController.php. The basic building block of each and every Zend Framework application. Code for creating a REST server looks like

public function restAction()
{
$server = new Zend_Rest_Server();
$server->setClass(‘CustomTestClass’);
$server->handle();
exit;
}

Notice the CustomTestClass? This is the name of the class we wish to make availabe trough REST server. For the sake of simplicity, this class is placed in same file as our IndexController class. Below is the sample of class body

class CustomTestClass
{
/**
* Write to a file
*
* @param string $string
* @return string Some return message
*/
public function sayHello($name)
{
$message = ‘Hello ‘.$name;
return $message;
}
}

Now all that’s left is to make a call to our REST server using GET method. Since our REST server code is placed inside restAction function in IndexController, it can be accessed using url like

http://localhost/myzendapp/index.php/index/rest

And the result should look like

<?xml version=”1.0″ encoding=”UTF-8″?>
<rest generator=”zend” version=”1.0″>
<response>
<message>No Method Specified.</message>
</response>
<status>failed</status>
</rest>

direct_link_to_rest_with_no_params

Although these are error messages, it’s ok, it means our REST server is working. Now if we were to enter url like

http://localhost/myzendapp/index.php/index/rest?method=sayHello

we would get response error message like
<?xml version=”1.0″ encoding=”UTF-8″?>
<CustomTestClass generator=”zend” version=”1.0″>
<sayHello>
<response>
<message>Invalid Method Call to sayHello. Requires 1, 0 given.</message>
</response>
<status>failed</status>
</sayHello>
</CustomTestClass>

direct_link_to_rest_with_method_assigned_but_no_params

This too is ok. It simply means we omited the parametars we were supose to pass to function. Remember, our sayHello function has signature like

public function sayHello($name)

meaning we should pass it a parametar $name. Now if we were to type into our browser url like

http://localhost/myzendapp/index.php/index/rest?method=sayHello&name=Branko

We would get response like

<?xml version=”1.0″ encoding=”UTF-8″?>
<CustomTestClass generator=”zend” version=”1.0″>
<sayHello>
<response>Hello Branko</response>
<status>success</status>
</sayHello>
</CustomTestClass>

As I said, simple example, more like proof of concept. Hope you find it usefull as a entry point to REST-full Zend development.

16
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

CTO / Magento Certified Developer Plus @inchoo. Father, husband!

Other posts from this author

Discussion 16 Comments

Add Comment
  1. Willian

    hey,
    you really save me. your site was the last of my list to learn how to use zend_rest, cause i dont find any other articles about this. and what i found just say “its very simple, bla bla bla”. so when i implements on my local server give me “No Method Specified”. no matter how i google it i dont get answers. till now.
    now i will put qotsa to play and work hard cause have many thing to do with zend_rest
    thanks a lot

  2. Nice to hear it helped.

  3. Thanks very much for this. It’s just what I needed.

  4. Thanks as well from me. Clear & Easy just what I needed.

  5. vudn.socialnetwork

    1000 * thanks about that

  6. Jasper

    Nice tutorial, helped me a lot… I’m now researching on how to handle authentication etc… :)

  7. I have a plan to implement a REST api to my project. But my framework is not ZEND. It is so easy …

    Thanks, this post make me to think deep into the ZEND.

  8. Raj

    Restful services made easy with this tutorial :) Nice one, liked it

  9. Danish shrestha

    really helped

  10. Mr Man

    Thanks! That’s a really clear example! Cheers. G

  11. Juan

    HI. i have a question. how do i do to use the zend rest client with an application made in ruby on rails? Thanks

  12. b_dubb

    how would i pass a variable from the restAction to the CustomClass? i’m thinking of a variable passed via the url

    thanks

    b

  13. naritai

    Thank you!
    Really helpful for me :)

  14. Oh my god, Finally understand the stuff.
    Thank you so much…:)

  15. Deva Karthik

    Its very Clear, thanks a ton.

  16. Badaruddin Chachar

    thanks for tutorial because its quite simple to understand, further i need your little help, how to call sayHello from c#. the link you provided is no more existing i think

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top