Miscellaneous
  Home arrow Miscellaneous arrow Page 5 - Create Your Own Custom API
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Create Your Own Custom API
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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


       · The script is excellent..please change 2 things1) in the constructor please...
       · HI,I'm pretty new to this so perhaps I shouldn't be venturing into this at all...
     

    MISCELLANEOUS ARTICLES

    - Stopping CSRF Attacks in Your PHP Applicatio...
    - Quick and Dirty AJAX Tutorial
    - Flickr Puzzle Mashup
    - The PAVISE of Security
    - Creating a CAPTCHA with PHP
    - Sending SMS Thru HTTP
    - The Postal Fix - Part 2
    - Adding Mail with Exim
    - The Postal Fix - Part 1
    - Create Your Own Custom API
    - Adding Drop Shadows with PHP
    - Writing a Basic Authentication System in PHP
    - Overlapping Images with GD
    - Using Sockets in PHP
    - Dynamic CSS with PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT