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.
- $actsAs = array('webservice');
Is the most simple use of it. There are a number of setup options that you can set as well. They are mostly related to the socket connection, and in most cases don’t need much fiddling.
- timeout The timeout for the requests defaults to 30 seconds.
- persistent Should the connection be persistent, opened with pfsock.
- defaultUrl The default URL to be used.
- port The port for the connection defaults to 80.
- protocol The protocol for connection defaults to tcp.
- followRedirect Should redirects be followed. Defaults to
Above are the configuration options that can be set in the model
$actsAs array. They can also be set on any request through $options[‘options’]
Requests are made with the
request() method. A simple use would look like:
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.
The above would do a search on google for ‘mark story’.
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.
Update I’ve added in Kim’s suggestion to allow multi-dimensional array to be posted. Thanks Kim.
Update I’ve moved the webservice behavior to GitHub You can find the newest version there. The link below will be kept for historical reference.