PHP XML-RPC Responses

An XMP-RPC request can get two types of responses. One on successful execution of service, and one on erroring out. If an XML-RPC request was successful, we will receive the data we requested. If there was an error, a special XML-RPC fault message will be returned.

Like regular web page requests response, a header will be returned with the results in the body of XML RPC response.

Example of a successful XML-RPC response:

HTTP/1.x 200 OK
Date: Fri, 01 Feb 2010 23:34:43 GMT
Server: Apache/1.3.33 (Marwin) PHP/5.1.4 DAV/1.0.3
X-Powered-By: PHP/5.1.4
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/xml
<?xml version="1.0" encoding="iso-8859-1"?>
<methodResponse>
<params>
<param>
<value><string>25 Years</string></value>
</param>
</params>
</methodResponse>

The methodResponse is the root element in a response. The params child will contain each value that is returned and enclosed in a param element. There can also be a multiple values returned like an array or a struct in addition to single values.

Example of array returned from a service:

<methodResponse>
<params><param><value>
<array>
<data>
<value><string>system.doIt</string></value>
<value><string>system.listAll</string></value>
<value><string>system.getAge</string></value>
</data>
</array>
</value></param></params>
</methodCall>

In a failed request response, an XML-RPC fault will be generated. The methodResponse will have a fault element instead of a params element. The methodResponse will always have either a params child or a fault child, but not both. An XML-RPC fault is basically a struct that is returned. There are two named members in the XML-RPC fault struct. A faultString is a human readable string containing the error message, and faultCode, which is an integer assigned by the service. Neither faultString or faultCode are defined or standardized by the XML-RPC specifications. They depend solely on the server implementation.

Example of XML-RPC fault:

HTTP/1.x 200 OK
Date: Fri, 01 Feb 2010 23:41:18 GMT
Server: Apache/1.3.33 (Marwin) PHP/5.1.4 DAV/1.0.3
X-Powered-By: PHP/5.1.4
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/xml
<?xml version="1.0"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><int>4</int></value>
</member>
<member>
<name>faultString</name>
<value><string>Too many parameters.</string></value>
</member>
</struct>
</value>
</fault>
</methodResponse>

Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

Filed Under: PHP XML

Tags:

RSSComments (0)

Trackback URL

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.