Both sides of a quick and simple web service


To clarify the article title, when I say web service, I mean a piece of software that will receive and send out data via the HTTP protocol. Furthermore, this data is usually transmitted in XML format. I will also call this XML RPC occasionally, but there is also a specification called XML-RPC so things can get confusing. Again in summary, this post will describe a very simple way to send and receive method calls and responses using XML and HTTP POST.

Sending your request
This will use the PHP cURL extension, so make sure your PHP is compiled with the correct options. The XML parser later will also use SimpleXML, which needs PHP 5 and the SimpleXML extension.

The following function will submit a query to an arbitrary web server, and also allow you to pass a POST string through to it.

   function cURL($link, $poststring){
        $ch = curl_init();

        curl_setopt ($ch, CURLOPT_URL, $link);
        curl_setopt ($ch, CURLOPT_POST, 1);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $poststring);
        curl_setopt ($ch, CURLOPT_HEADER, 0);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 15);

        $response = trim(curl_exec ($ch));
        return $response;
   } //cURL

Now, you should decide on a format for your XML requests; we’ll use something very simple here:

   <?xml version='1.0'?>
   <request>
      <user>Mike</user>
      <type>getDate</type>
   </request>

This will ask another server for the current time, and use basic user name authentication.

To send the request:

   $poststring = "request=$xml";
   $response = cURL('http://server.com/xml.php', $poststring);

Receiving the request
On the other side of the connection we write some code to parse the request (i.e. this is in xml.php hosted on server.com).

   $xml = (isset($_POST['request'])) ? $_POST['request'] : false;
   if (!$xml) { return; }

Now the above XML we sent is stored in the $xml variable. To process it, we can use the following code:

   $obj = simplexml_load_string($xml);

   $error = '';

   if ((string)$obj->user == 'Mike') {
      switch ((string)$obj->type) {
         case 'getDate' :
            $response = "<?xml version='1.0'?>
               <response>
                  <date>".date('l, d F Y \a\t H:i:s', time())."</date>
               </response>";
            break;

         default :
            $error = 'Invalid method';
      } //switch
   } else {
      $error = 'Invalid user';
   } //else

   if ($error) {
      print "<?xml version='1.0'?>
         <response>
            <error>$error</error>
         </response>";
   } else {
      print $response;
   } //else

After checking the user name the method name is handled, and if it doesn’t exist on this implementation an error is returned. Printing the XML response is how we pass data back to the server that requested the service - if you look back you’ll see that the cURL function returns whatever content the URL returned, i.e. our printed XML.

That should be everything, this should of course be expanded into a class or structured methods.

Share this post
  • Digg
  • StumbleUpon
  • Reddit
  • del.icio.us
  • Facebook
  • muti
  • Mixx
  • Google
  • laaik.it

This entry was posted on Tuesday, April 3rd, 2007 at 3:48 am and is filed under Code, MySQL. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “Both sides of a quick and simple web service”

  1. Rena Washington said this on

    q286pgq5hild77cd

  2. private-seo-soft.blogspot.com said this on

    Very useful information thanx! I also have some intresting info in my blog about how to write tic checker here. Thanx!

Leave a Reply