Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Create Your Own Custom API
MISCELLANEOUS

Create Your Own Custom API
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 28
    2005-02-02

    Table of Contents:
  • Create Your Own Custom API
  • Developing the basic API layout
  • The Client
  • The Server
  • The Server Class
  • The "Test" Step

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Create Your Own Custom API - The Server


    (Page 4 of 6 )

    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-&gt;parse_xml($request_xml);
    }
    else {
     
    $x-&gt;errno "200";
    }

    // Section 4: generate XML response
    $results $x-&gt;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.

    More Miscellaneous Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap