Miscellaneous

  Home arrow Miscellaneous arrow Page 5 - 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 Class


    (Page 5 of 6 )

    There are 2 things you can do here. 1 - you can set up a "variables class" (because you could have quite a few with your own, here we will only have a select few so we aren't going to do this) and then make your server class an extention of your variables class. You'd set this up like so:

    <?php
    class my_vars {
     var 
    $var_1;
     var 
    $var_2;
     var 
    $var_3;
    // end class
    class xml_server Extends my_vars {
     function 
    xml_server() {
      
    // server set up
     
    // end xml_server()
    // end class
      
    ?>

    2 - you can lump it all together. Here we only have a very small XML request so we won't really have a problem with everything under one class.

    So first, we have our class variables.

    <?php
    class xml_server {
     
    // variables to check the user built a valid request
     
    var $myXML_tag;
     var 
    $function_tag;
     var 
    $values_tag;
     var 
    $orderID_tag;
     var 
    $reference_tag;

     
    // variables to set values sent in
     
    var $function;
     var 
    $orderID;
     var 
    $reference;

     
    // error variables
     
    var $errno;
    }
      
    ?>

    Next... we create our class constructor (set variables)

    <?php
    function xml_server() {
     var 
    $this-&gt;myXML_tag false;
     var 
    $this-&gt;function_tag false;
     var 
    $this-&gt;values_tag false;
     var 
    $this-&gt;orderID_tag false;
     var 
    $this-&gt;reference_tag false;
     var 
    $this-&gt;errno "0";
    }
      
    ?>

    We set each tag to false (so when we verify it was sent we can change to true. Set errno to "0" indicating no errors!

    Next we are going to set up the xml_parse() function.(from our server page). This is a basic and primitive XML parser, we are going to check that the desired elements are in the request, that those elements have proper closing and opening tags, that the document is well-formed (almost). A major drawback is "extra info". The client application could possibly send in extra data in the XML request. We don't check for any of this extra data, but you may want to add that into your own parser.

    <?php
    function parse_xml($xml) {
     
    $success true;    // variable to verify good xml and good request
     
    $xml trim($xml);  // get rid of excess white space that may or may not be present

     /* Set _tag variables to the position in the file where it can be found
      the strpos() function returns false on failure to find the given string
      Since this is our API, we know what to look for and what is needed
     */
     
    $this-&gt;myXML_tag strpos($xml"&lt;myXML&gt;"); // 7 - this is the string length
     
    $this-&gt;function_tag strpos($xml"&lt;function&gt;"); // 10
     
    $this-orderID_tag strpos($xml"&lt;orderID&gt;"); // 9
     
    $this-&gt;reference_tag strpos($xml"&lt;reference&gt;"); // 11

     // Verify the correct elements of a request were sent in

     
    if (!$this-&gt;myXML_tag) {
      
    $success false;
      
    $this-&gt;errno "101";
     }
     if (!
    $this-&gt;function_tag) {
      
    $success false;
      
    $this-&gt;errno "101";
     }
     if (!
    $this-&gt;orderID_tag) {
      
    $success false;
      
    $this-&gt;errno "101";
     }
     if (!
    $this-&gt;reference_tag) {
      
    $success false;
      
    $this-&gt;errno "101";
     }

     
    /*
     Verify we have a well formed XML request (note: this is not a very good validator, it does not check for extra content, only that the needed content is there - but works well for what we are doing here) 
     */
     
    if ($success) {
      if (!
    strpos($xml"&lt;/myXML&gt;")) {
       
    $success false;
       
    $this-&gt;errno "100";
      }
      if (!
    strpos($xml"&lt;/function&gt;")) {
       
    $success false;
       
    $this-&gt;errno "100";
      }
      if (!
    strpos($xml"&lt;/orderID&gt;")) {
       
    $success false;
       
    $this-&gt;errno "100";
      }
      if (!
    strpos($xml"&lt;/reference&gt;")) {
       
    $success false;
       
    $this-&gt;errno "100";
      }
     }

     if (
    $success) {
      
    $j $this-&gt;function_tag 10;
      
    // grab the first char after the open tag
      
    $this-&gt;function = substr($xml$j1); 
      
    /*
       grab the characters until you hit a '&lt;'.  Note in your manual that you write with your API that '&lt;' characters will cause a fault.  It is your API so, you can do whatever you want!  Technically, you're only bound by the rules of the XML language.
      */
      
    for ($i $j 1substr($xml$i1) != "&lt;"$i++) {
       
    $this-&gt;function .= substr($xml$i1);
      }

      
    $j $this-&gt;orderID_tag 9;
      
    $this-&gt;orderID substr($xml$j1);
      for (
    $i $j 1substr($xml$i1) != "&lt;"$i++) {
       
    $this-&gt;orderID .= substr($xml$i1);
      }
      
      
    $j $this-&gt;reference_tag 11;
      
    $this-&gt;reference substr($xml$j1);
      for (
    $i $j 1substr($xml$i1) != "&lt;"$i++) {
       
    $this-&gt;reference .= substr($xml$i1);
      }
     }

     
    // return true on success, false on failure
     
    return $success;
     
    }
      
    ?>

    There's our basic xml parser - not the world's greatest, but it gets the job done. As you may have noticed by now, we are using these codes for error conditions. 100 is for badly formed XML, 101 is for missing required element, 200 is for server creation error. The system is based around 1XX - XML data, 2XX - server stuff (errors not caused by the xml request), 3XX - request info. So a badly formed XML request would yield a 100 error. If the user sends a "ordre" request (misspelled order), the request is invalid and would yield a 300 error. You can build your own error condition/code system with your own API.

    Next we will set up our xml generator using the XML DOM. The XML DOM has been replaced by the DOM in PHP5, but I don't have PHP5 so I'm still building with the old stuff. Hey, at least it's not experimental anymore! The following function will generate our XML message to send back to the user:

    <?php
    function generate_xml() {
      
    // check to make sure no errors have been found so far (from parsing)
      // if no errors build an xml response by the function
      
    if ($this-&gt;errno == "0") {
       if (
    $this-&gt;function == "order") {
        
    $xml $this-&gt;build_xml();
       }
       else {
        
    $this-&gt;errno "300"// 300 - function not recognized
        
    $xml $this-&gt;build_error_xml();
       }   
      }
      return 
    $xml;
    }

    function 
    build_xml() {

      
    $xml domxml_new_doc("1.0");

      
    // create the elements
      
    $root $xml-&gt;create_element("myXML");
      
    $values $xml-&gt;create_element("values");
      
    $id $xml-&gt;create_element("orderID");
      
    $ref $xml-&gt;create_element("reference");
      
    $response $xml-&gt;create_element("response");
      
    $order $xml-&gt;create_element("order");

      
    // create the text nodes
      
    $id_txt $xml-&gt;create_text_node($this-&gt;orderID);
      
    $ref_txt $xml-&gt;create_text_node($this-&gt;reference);
      
    $ord_txt $xml-&gt;create_text_node("confirmed");

      
    // append the elements &amp; nodes
      
    $root $xml-&gt;append_child($root);
      
    $values $root-&gt;append_child($values);
      
    $id $values-&gt;append_child($id);
      
    $id_txt $id-&gt;append_child($id_txt);
      
    $ref $values-&gt;append_child($ref);
      
    $ref_txt $ref-&gt;append_child($ref_txt);
      
    $response $root-&gt;append_child($response);
      
    $order $response-&gt;append_child($order);
      
    $ord_id $order-&gt;append_child($ord_txt);

      
    $xml $xml-&gt;dump_mem();

      return 
    $xml;
    }

    function 
    build_error_xml() {

      
    $xml domxml_new_doc("1.0");

      
    // create the elements
      
    $root $xml-&gt;create_element("myXML");
      
    $fault $xml-&gt;create_element("fault");
      
    $code $xml-&gt;create_element("code");

      
    $code_txt $xml-&gt;create_text_node($this-&gt;errno);

      
    $root $xml-&gt;append_child($root);
      
    $fault $root-&gt;append_child($fault);
      
    $code $fault-&gt;append_child($code);
      
    $code_txt $code-&gt;append_child($code_txt);

      
    $xml $xml-&gt;dump_var();

      return 
    $xml;  
    }
      
    ?>

    This is a simple xml generator to generate xml on the fly. I set up 2 different functions for the good request and bad request xml generation. The final step, as always - test...

    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 6 - Follow our Sitemap