NYCPHP Meetup

NYPHP.org

[nycphp-talk] Pear XML_RPC + AJAX

Matthew Terenzio matt at jobsforge.com
Fri May 27 12:39:50 EDT 2005


On May 27, 2005, at 12:10 PM, Nestor Florez wrote:

> Mathew,
>
> I do not understand, What did you accomplished and can we see a 
> working example of this.


Ajax is a way to update a web page without reloading using  the 
xmlhttprequest object in javascript.
If you tie that in to an XML-RPC call you can have a web-services style 
data being written to a page in real time.

I knew about this for a while but was just happy to spend the time to 
get it to work for me.

  here's an example.

on the client (which is just an html page, no server side processing 
necessary)
I fed the javascript function below the url (the server), the XML_RPC 
method I want to invoke, and the value, in this case a string.

The XMLHTTP REQUEST object does its magic and if the XML_RPC returns an 
XML Doc. The processReqChange() waits for a reply and if its Ok (200) I 
can handle the  reply how i'd like . (I use a delegate() function which 
I feed the XML_RPC method type to so I hnow what kind of value I'm 
expecting.

I can explain more thoroughly later, but I'm late for work.

I'm just starting to work with this stuff. It might be old hat for some 
around here.
But basically, it enables you to call a remote procedure from somewhere 
on the web and display it within a web page without the need to reload 
the page.
I'm not going to mention  a Stock Ticker or anything like that : ). 
There are many possible uses.

<script language="javascript">
//config for server
var server = "PointToYourPHPScriptWhichUsesPearXML_RPC_andhas some 
methods defined "

var isIE = false;
var req;

function sendXMLRPC(url, method, value) {
     //branch for native object
     type = method;
     if (window.XMLHttpRequest) {
         req = new XMLHttpRequest();
         req.onreadystatechange = processReqChange;

         req.open("POST", url, true);
         req.setRequestHeader("Content-Type", "text/xml");
         req.send('<?xml version="1.0"?>\n'+
'<methodCall>\n'+
'<methodName>'+ method + '</methodName>\n'+
'<params>\n'+
'<param>\n'+
'<value>\n'+
'<string>' + value + '</string>\n'+
'</value>\n'+
'</param>\n'+
'</params>\n'+
'</methodCall>\n');
     //branch for Active X object
     } else if (window.ActiveXObject) {
         isIE = true;
         req = new ActiveXObject("Microsoft.XMLHTTP");
         if (req) {
             req.onreadystatechange = processReqChange;
             req.open("POST", url, true);
             req.setRequestHeader("Content-Type", "text/xml");
         req.send('<?xml version="1.0"?>\n'+
'<methodCall>\n'+
'<methodName>method</methodName>\n'+
'<params>\n'+
'<param>\n'+
'<value>\n'+
'<string>value</string>\n'+
'</value>\n'+
'</param>\n'+
'</params>\n'+
'</methodCall>\n');
         }
     }
}

function processReqChange() {
     // only if req shows "loaded"
     if (req.readyState == 4) {
         // only if "OK"
         if (req.status == 200) {
         //hand off to delegate function
			delegate(type);
          } else {
             alert("There was a problem retrieving the XML data:\n" +
                 req.statusText);
          }
     }
}




More information about the talk mailing list