- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
One of the best things about open source is that if enough people want to do something, somebody's going to create a library to make it easy. Witness the Incutio PHP XML-RPC Library.
XML-RPC, as we've already discussed, is a type of simple Web service geared at telling a remote computer to do something, whether it's to return the current time or to add information to a database. The process involves sending XML files back and forth over an HTTP connection, so it can be accomplished in virtually any programming language on virtually any platform, but still, who wants to muck about creating and decoding XML, when it would be simpler to have a library handle that?
Today we're going to create a very simple identity verification service that uses the Incutio PHP XML-RPC Library for both the server and client. You can download the library at http://scripts.incutio.com/xmlrpc/.
Ultimately, the program will enable users to register a username and password, and will enable a site to check on a username/password combination. But we'll start by creating a very simple client that checks to see how many users are currently in the system. As you may recall, the XML message we're sending should look something like this:
<?xml version="1.0"?> <methodCall> <methodName>ident.getUserCount</methodName> <params></params> </methodCall>
This message tells the server to execute the function ident.getUserCount
and return the results. The resulting XML looks something like this:
<?xml version="1.0"?> <methodResponse> <params> <param> <value> <int>42</int> </value> </param> </params> </methodResponse>
So let's start by creating a simple client to send the request:
<?php include('IXR_Library.inc.php'); $client = new IXR_Client('http://www.nicholaschase.com/phpxmlrpc/server.php'); ?>
First, we load the library, and then we instantiate the client class. From there, creating and sending the request is straightforward:
... $client = new IXR_Client('http://www.nicholaschase.com/phpxmlrpc/server.php'); $client->query('ident.getUserCount'); print $client->getResponse(); ...
The results, which are automatically converted to PHP data types by the library, should look something like this:
42
Sending parameters is pretty simple as well, accomplished by adding them to the query:
... $client->query('ident.userExists', 'roadnick'); print $client->getResponse(); ...
The result, depending on the username you send, is a Boolean value of true or false, which gets converted to a one or zero.
You can also pass multiple parameters as an array:
... $params[0] = "roadnick"; $params[1] = "mypassword"; $client->query('ident.userLogin', $params); print $client->getResponse(); ...
In all of these cases, we have been dealing with and simple responses, but complex responses are handled just as easily, because they are returned to PHP as arrays.
Another time in which you are dealing with arrays is when the server returns an error. For example, if you try to create a username that already exists, the server will send back an error message:
... $params[0] = "roadnick"; $params[1] = "mypassword"; $client->query('ident.createUser', $params); print_r ($client->getResponse()); ...
In this case, you can see the results of the print_r
statement:
Array ( [faultCode] => -1 [faultString] => Username roadnick already exists. )
Of course, it would be much cleaner to deal with the errors within the code:
... $params[0] = "roadnick"; $params[1] = "mypassword"; if (!$client->query('ident.createUser', $params)){ die('Error: '.$client->getErrorCode()." -- ".$client->getErrorMessage()); } else { print_r ($client->getResponse()); } ...
Fortunately, the library is smart enough to determine that it is actually an error and not the typical response.
So that's the client side. It turns out the creating the server is also straightforward. The library supports the creation of the server as an object, but for those of you using PHP 4, you can also create it procedurally.
Start by creating the functions you want to serve:
<?php include('IXR_Library.inc.php'); function getUserCount($args) { return 42; } function userExists($args) { if ($args == "roadnick"){ return true; } else { return false; } } function userLogin($args){ $username = $args[0]; $password = $args[1]; if ($username == 'roadnick' && $password == 'mypassword'){ return "Valid login."; } else { return "Invalid login. Please try again."; } } function createUser($args){ $username = $args[0]; $password = $args[1]; if ($username == 'roadnick'){ return new IXR_Error(-1, 'Username '.$username.' already exists.'); } else { return "User created."; } } ...
Obviously, these are placeholder functions for which the logic can be replaced later, after the server is up and running.
Now you can create the actual server by instantiating the IXR_Server
class:
... return "User created."; } $functionMapping = array( 'ident.getUserCount' => 'getUserCount', 'ident.userExists' => 'userExists', 'ident.userLogin' => 'userLogin', 'ident.createUser' => 'createUser' ); $server = new IXR_Server($functionMapping); ?>
The functionMapping
array maps of the names you will pass any actual request to the functions to which they correspond. Finally, create the server.
As simple as this is, it is only a basic look at what you can do with the Incutio PHP XML-RPC Library. The library also enables you to process multiple calls in a single request and to create servers that will let a client know what methods are available, among other features.