Welcome Guest, Not a member yet? Register   Sign In
Using XML-RPC class to respond to non-XML requests
#1

[eluser]oribani[/eluser]
Hi,

I was looking for a painless way to send XML back to a client who sent a simple/classic POST request (non-XML). Unfortunately, if you want to use the XML-RPC classes, it looks like you are locked into needing to have a XML request from the client in order to build and send a XML response. While a good alternative would be to use the JSON model, I have easy JavaScript that can quickly parse what I need from a simple XML response, so I dug into the XML-RPC class and pulled the code I needed to make one into a helper function that I wanted to share below. If there's a supported way to do this, I'd like to hear about it.

Code:
/**
    *
    * Build and sends a XML response to client
    *
    * @param array $response Pre-built CI xmlrpc response array
    *
    * @return DOES NOT RETURN
    *
    */
   if (!function_exists('build_and_send_xml_response'))
   {
      function build_and_send_xml_response($response)
      {
         $CI =& get_instance();
         $CI->load->library('xmlrpc');

         $response_result = $CI->xmlrpc->send_response($response);
         $payload = '<?xml version="1.0" encoding="UTF-8"?'.'>'."\n"
                  . $response_result->prepare_response();

         header("Content-Type: text/xml");
         header("Content-Length: ".strlen($payload));

         // anti-cache headers
         //
         header('Last-Modified: Thu, 22 Jul 1999 10:00:00 GMT');
         header('Expires: ' . gmdate('D, j M Y H:i:s') . ' GMT');
         header('Cache-Control: no-store, no-cache, must-revalidate');
         header('Cache-Control: post-check=0, pre-check=0', FALSE);
         header('Pragma: no-cache');

         echo $payload;
         exit;
      }
   }

That "Pre-built CI xmlrpc response array" argument is easily created per the user guide documentation, but since its syntax is easy to trip over, I also abstracted it into an easy helper function (just note that it creates a structure that is specific to what my application does and my JavaScript that parses it):

Code:
/**
    *
    * Sends our own XML response to client
    *
    * @param mixed $response_code    Response code,
    *                                usually 2xx or 5xx
    * @param mixed $response_message Response message,
    *                                usually "OK", "ERROR",
    *                                or "FATAL"
    * @param mixed $payload          Whatever data that should
    *                                be sent back to the client
    *
    * @return DOES NOT RETURN
    *
    */
   if (!function_exists('send_xml_response'))
   {
      function send_xml_response($response_code, $response_message, $payload)
      {
         return build_and_send_xml_response(
            array(
               array(
                  'response_code' => $response_code,
                  'response_message' => $response_message,
                  'payload' => $payload,
               ),
               'struct'
            )
         );
      }
   }

So this way, my controller can validate POST data normally (with the form validator class) and send back a response in XML with a simple call to send_xml_response(...);




Theme © iAndrew 2016 - Forum software by © MyBB