The server is where we really start getting into things - so let's really get into it! Here, we're going to set up the server which will actually include our server class (from a different file) - so we can make this object oriented, much easier to extend, change and upgrade/update at a later time. So to start - we'll set up the basic "info retrieval" functionality.
<?php include('server_class.php');
// Section 1: Info retreival if (isset($HTTP_RAW_POST_DATA)) { $request_xml = $HTTP_RAW_POST_DATA; } else { $request_xml = implode("\r\n", file('php://input')); }
// Section 2: Create Server $x = new xml_server();
// Section 3: parse XML if ($x) { $success = $x->parse_xml($request_xml); } else { $x->errno = "200"; }
// Section 4: generate XML response $results = $x->generate_xml();
// Section 5: send XML response print $results; ?>
And that, lady's and gentlemen, is a server. Not much to it really. Some things to note:
Section 1. 99% of the time your server is going to work fine with the line "$request_xml = $HTTP_RAW_POST_DATA;" - but mine didn't. So, after much searching - I found the only workaround - and that is grabing the XML sent in from the input stream. It works - does the trick and what we want it to do. Again, this is only needed if the $HTTP_RAW_POST_DATA is NOT available.
Section 2. Simple enough - this is just using our "yet-to-be-built" class (which has already been included at the top of the code). It does the same thing as the server_create() function in XMLRPC, kinda. It just creates a server of our own.
Section 3. For starters, we need to verify our server was created - if not, we'll set our fault variable ($errno) to a fault code which the user will recognize as "Our mistake, the server wasn't created". For that to happen, you'll need to write your "user manual" and include that with your code. Secondly, if the server is created, we'll parse the xml (also done with our "yet-to-be-created" class).
Section 4. Next, well generate the xml (once again with our "yet-to-be-created" class!). The generate_xml() function is going to return valid XML data which in Section 5, we return. Notice, we don't "return" it, we print it out. This is how our server sends back the message. Whatever you "print" to output in the server will be returned as a string(that's how we set up in our client) to the client. Note - the first thing output, is going to be the only thing returned - so you can only use "print" once!
So far, everything has been short and simple. Now, hopefully that won't change. And for this tutorial, it really won't. But, for your safety, and out of my sheer concern for you, I'm letting you know now, all the "bad" things I mentioned in the beginning of this tutorial happen because of our server class. But then, you gotta do what you gotta do. So next up, the class, the nuts and bolts, the actual "workhorse" of our API.