Changes between Version 1 and Version 2 of ObserverTutorial/Output
- Timestamp:
- 06/21/10 16:42:25 (3 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ObserverTutorial/Output
v1 v2 1 = Adapting calculator's output = 2 For a web service to be able to send results back to Unicorn, it needs to implement a specific format called an observation response. Basically, this is an XML document listing all the messages (errors, warnings and information) that the web service produced. 3 4 The following code shows how such a response would look like for the calculator. It contains two normal messages and an error. As you can see, each message has a type (info, warn, error), a context (optional) and a title. In our case, the context contains the arithmetic expression and the title contains the corresponding result. Each message element can have more data, like a longer text, an associated group, ... As you can see, there's also a status which is used to express if the test succeeded or failed. For a detailed description of this format check out its dedicated documentation. 5 1 6 {{{ 2 7 #!xml … … 22 27 23 28 }}} 29 So, back to our calculator, we already have our results in an array and errors in another one, so it's easy to turn them into a Unicorn response: 30 24 31 {{{ 25 32 #!xml … … 43 50 <?php endforeach ?> 44 51 </observationresponse> 52 }}} 53 Finally, we add it to the list of supported outputs in our main PHP script: 45 54 55 {{{ 56 #!php 57 $valid_formats[] = 'ucn'; 46 58 }}} 59 Et voilà, we can now call the calculator with the parameter output=ucn to get results in Unicorn format. 60 61 The [wiki:ObserverTutorial/Contract last step] as an observer developper will be to tell Unicorn how it can contact the service and which parameters to use. This is what we call the contract.
