Introducing the Webservice Behavior

As webservices grow so does the need for being able to communicate with them in an easy fashion. This simple blog alone uses 2 webservices. The recent tracks at the bottom is a feed I pull from Last.fm and my spam protection is provided by Akismet. When first building my site I looked for an already built solution and found a partial solution in Felix GeisendÃ¶rfer’s WebModel. However, I wanted to remove its dependancy on cURL as it is not always available, and transform it into a behavior. Transformation to a behavior was an easy process. Removing curl and manually writing all the Socket code was not an appealing prospect. Thankfully, CakePHP 1.2 has the new CakeSocket class which eases the creation of socket connections. The result is a behavior that has no extension dependancies and is a behavior for easy reuse. This is a PHP5 class, so if you are on PHP4 too bad.

Using the Webservice Behavior

Using the webservice behavior is quite easy. Simply add it to the $actsAs array for your model.

Making Requests

This would make a request against the defaultUrl set in the model settings and return the content of that request. The request() method connects to the defaultUrl if no url is supplied. Several request types are supported with ‘get’, ‘post’ and ‘xml’ being the completed types. I have plans to add SOAP as well, as soon as I can wrap my head around the documentation. A second argument allows you set additional headers, data, url, and options for the connection.

Debugging your Requests

There is built-in capabilities to introspect on what is going on in your request calls. Using getRequestInfo() will return an array of information pertaining to the last made request. Headers for both the request and response, as well as cookies, data and connection options will be returned. I found this to be very handy in my own development, and I hope you will as well.

Bonus Round XMLRPC

As a bonus when downloading the WebserviceBehavior you get an XmlRpcMessage class as well. This is a very simple class to enable the transmission of XMLRPC requests. I haven’t done any testing with complicated payloads. But for simple requests it works quite well. When making requests with the type of xml, supplied data is automatically converted into an XMLRPC message and sent for you. The one caveat is that you need to supply a methodName as well.

This will format up an XML message and send it. You can also you the XMLRPC class on its own of course. It is a full class with a usable interface. But that is another day and another article. Included with the class are some tests, they cover the typical use cases that I have come across so far, but will be expanded as I use it more, so check back for updates to the classes and tests.

As always I’d love to hear any feedback you have for this, and I hope you find them useful.

Glad you found it useful David, I’ve got plans to release my Akismet model here as well. But if you can’t wait the API documentation gives a good overview on how to do. The API call you want is ‘comment-check’

mark story on 7/4/08

Mark, really nice code! Thanks for sharing it, IÂ´m gonna use it in my next app, thatÂ´s for sure! One detail, in your last example, I guess your variable name is called $vars, or the inverse in the request call…

Very nice! I’m needing this im my company. I have created a datasource, but not is the best. This is better.

When you not use a database, simple make $useTable = false in model?

Some issues: – In setup (line 104), use array_merge until am. – In line 342, use isset($heads[‘Set-Cookie’]. This is better than array_key_exists. – I’m not tested in action, but the behavior not parse XMLRPC response?

Nicely done.. Was trying it out and on sites that return bad cookie parts, the setCookie sometimes echoes a Notice: Undefined offset: 1 in /usr/www/dev/indv/jad/monitorize/models/behaviors/webservice.php on line 229

gthub says “site does not exist” for download. git clone does not work either because a “private key” failure. I really do not understand why people do not stay with established technologies and providers of FREE services – I NEVER had a download problem with sf.net in the last years and github just seems to make trouble every second day.

Jones: There are several guides on github about how to use it. Did you use the public clone url? Furthermore, there is no cost to use github. As for why not to use sourceforge, well for one there is no support for git. When I started looking for a place to host my code, I was also interested in learning to use git. Github was an excellent way to learn how to use git and be able to share code at the same time. I’m sorry that doesn’t float your boat. I know of many people other than myself who use github daily without trouble, so it can’t be all that bad.

mark story on 1/21/09

This looks great guys. Where is the example on “How to use” that!!! There should be minimum set of controller, model and view to describe further.

Recent Artwork

Links

Mark is a designer and web-developer, working with standards compliant HTML and CSS. He has been building websites since 2000. Currently he is employed at Freshbooks as a developer, and actively contributes to open source projects specifically CakePHP. He uses this site as a place to share what he has learned and made.