Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - 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 
Dedicated Servers  
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


    (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


       · 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 3 hosted by Hostway