Created
May 1, 2011 23:37
-
-
Save MorningZ/950991 to your computer and use it in GitHub Desktop.
Titanium + Zentus XML-RPC for talking to WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2008 David Crawshaw <[email protected]> | |
* | |
* Permission to use, copy, modify, and distribute this software for any | |
* purpose with or without fee is hereby granted, provided that the above | |
* copyright notice and this permission notice appear in all copies. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
*/ | |
/* | |
* An XML-RPC library for JavaScript. | |
* http://www.zentus.com/js/xmlrpc.js.html | |
* | |
* The Zentus.Request() function is the public entry point. | |
*/ | |
/* | |
* Execute an XML-RPC method and return the response to 'callback'. | |
* Parameters are passed as JS Objects, and the callback function is | |
* given a single JS Object representing the server's response. | |
*/ | |
/* | |
Using with Titanium to call WordPress data | |
Zentus.Request( | |
"http://<Your WordPress Domain>/xmlrpc.php", | |
"<WP XML-RPC method>", // Documented here: http://codex.wordpress.org/XML-RPC_wp | |
[<Array of Params>], // Make sure they are in order as the docs above state! | |
function(xml) { | |
// TiDOMDocument, to tranverse: http://www.w3.org/TR/DOM-Level-2-Core/core.html | |
}, | |
function(err) { | |
// err = error message | |
}, | |
function() { | |
// called after both success and error | |
} | |
); | |
*/ | |
var Zentus = {}; | |
Zentus.Log = { | |
"Enabled" : true, | |
"Write" : function(txt) { | |
if (Zentus.Log.Enabled) { Titanium.API.debug(txt); } | |
} | |
}; | |
Zentus.Request = function(server, method, params, callback, callErr, callFinal) { | |
if (callErr == null) { callErr = Zentus.Log.Write; } | |
var request = Titanium.Network.createHTTPClient(); | |
request.open("POST", server, true); | |
request.onreadystatechange = function() { | |
if (request.readyState != 4) { | |
return; | |
} | |
try { | |
if (request.status != 200) { | |
callErr("connection error " + request.status); | |
return; | |
} | |
var ret = null; | |
try { | |
Zentus.Log.Write("Zentus: Back from request - raw XML"); | |
Zentus.Log.Write("" + request.responseText); | |
if (request.responseXML) { | |
ret = request.responseXML; | |
} | |
else { | |
throw "bad xml: '" + request.responseText + "'"; | |
} | |
} catch (err) { | |
err.message = "xmlrpc: " + err.message; | |
callErr(err); | |
throw err; | |
} | |
try { | |
callback(ret); | |
} | |
catch (err) { | |
err.message = "callback: " + err.message; | |
callErr(err); | |
throw err; | |
} | |
} | |
finally { | |
if (callFinal) { | |
callFinal(); | |
} | |
} | |
}; | |
var sendXML = Zentus.Request.writeCall(method, params); | |
Zentus.Log.Write("Sending:"); | |
Zentus.Log.Write(sendXML); | |
request.send(sendXML); | |
}; | |
Zentus.Request.writeCall = function(method, params) { | |
var out = "<?xml version=\"1.0\"?>\n"; | |
out += "<methodCall>\n"; | |
out += "<methodName>"+ method + "</methodName>\n"; | |
if (params && params.length > 0) { | |
out += "<params>\n"; | |
for (var i=0; i < params.length; i++) { | |
out += "<param><value>"; | |
out += Zentus.Request.writeParam(params[i]); | |
out += "</value></param>"; | |
} | |
out += "</params>\n"; | |
} | |
out += "</methodCall>\n"; | |
return out; | |
}; | |
Zentus.Request.writeParam = function(param) { | |
if (param == null) { return "<nil />"; } | |
switch (typeof(param)) { | |
case "boolean": | |
return "<boolean>" + param + "</boolean>"; | |
case "string": | |
param = param.replace(/</g, "<"); | |
param = param.replace(/&/g, "&"); | |
return "<string>" + param + "</string>"; | |
case "undefined": | |
return "<nil/>"; | |
case "number": | |
return /\./.test(param) ? "<double"> + param + "</double>" : "<int>" + param + "</int>"; | |
case "object": | |
if (param.constructor == Array) { | |
out = "<array><data>\n"; | |
for (var i in param) { | |
if (param.hasOwnProperty(i)) { | |
out += " <value>"; | |
Zentus.Request.writeParam(param[i]); | |
out += "</value>\n"; | |
} | |
} | |
out += "</data></array>"; | |
return out; | |
} | |
else if (param.constructor == Date) { | |
out = "<dateTime.iso8601>"; | |
out += param.getUTCFullYear(); | |
if (param.getUTCMonth() < 10) { out += "0"; } | |
out += param.getUTCMonth(); | |
if (param.getUTCDate() < 10) { out += "0"; } | |
out += param.getUTCDate() + "T"; | |
if (param.getUTCHours() < 10) { out += "0"; } | |
out += param.getUTCHours() + ":"; | |
if (param.getUTCMinutes() < 10) { out += "0"; } | |
out += param.getUTCMinutes() + ":"; | |
if (param.getUTCSeconds() < 10) { out += "0"; } | |
out += param.getUTCSeconds(); | |
out += "</dateTime.iso8601>"; | |
return out; | |
} | |
else { /* struct */ | |
out = "<struct>\n"; | |
for (var i in param) { | |
if (param.hasOwnProperty(i)) { | |
out += "<member>"; | |
out += "<name>" + i + "</name>"; | |
out += "<value>" + Zentus.Request.writeParam(param[i]) + "</value>"; | |
out += "</member>\n"; | |
} | |
} | |
out += "</struct>\n"; | |
return out; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment