XMLRPC for Actionscript 3.0 - Free Library
I just finished porting Matt Shaw's XML-RPC for Actionscript 2.0 code to AS3. This is a really easy way to get Flash to transfer data with an XML-RPC endpoint. Matt did a great job and the conversion wasn't too difficult.
Here's some sample code to show you how to use it.
import flash.events.Event; import flash.events.ErrorEvent; import com.mattism.http.xmlrpc.*; import com.mattism.http.xmlrpc.util.*; function rpcCompleteHandler(evt:Event):void { var response:Object = rpc.getResponse(); } function rpcErrorHandler(evt:ErrorEvent):void { var fault:MethodFault = rpc.getFault(); } var rpc:Connection = new ConnectionImpl('http://localhost/xmlrpc.php'); rpc.addEventListener(Event.COMPLETE, rpcCompleteHandler); rpc.addEventListener(ErrorEvent.ERROR, rpcErrorHandler); rpc.addParam(4, XMLRPCDataTypes.INT); rpc.call('mypackage.myfunction');
Download XMLRPC for AS3
And without further ado, here are the source files:
Some notes:
- The license is LGPL
- The readme, changelog, tests, and examples are out of date.
- Here's a link to the XML-RPC for AS2 project on SourceForge
- Gregory Goldberg also ported this project to AS3 but removed the fault handling in the process
If you have any questions or problems leave a comment here. Enjoy!
Delicious
Digg
Reddit
Facebook
Google
Yahoo
Technorati

Comments
Great work.
tks;
I was just wondering "Do I need to port my AS2 XML-RPC classes?" - I guess not! Looking forward to trying this out... Thanks!! (and thanks Google!)
Any chance you fixed some of the bugs :)
Hey, thanks for the original code, Matt! Made my life a lot easier, too.
Hello,
good work. However I made an update to allow to use structures of structures inside:
File MethodCallImpl.as, update line 143:
then use
Hmm, how do I pass an array to addParam with this? I keep getting ReferenceError: Error #1069 when I try do it like:
var myArray = [1,2]
rpc.addParam(myArray, XMLRPCDataTypes.ARRAY)
I used Peter Parente's XMLRPC library in AS2 and this was valid with:
var myArray = [1,2]
server.Call("add", myArray, listener)
Don't suppose you could port his lib could you Daniel? ;) (the interface seems tighter)
Andy....
I create my array as follows, and works cool...
var arr:Array = new Array({type: XMLRPCDataTypes.STRING, value:"test string"},{type: XMLRPCDataTypes.INT, value:5})
rpc.addParam(arr, XMLRPCDataTypes.ARRAY);
I like it because it keeps the standards nice 'n tight, the fact that you have to conform to XMLRPC standard, by casting your array elements datatypes.
Thank you so much Justin! I was totally missing the concept of adding type and value.
Thank you also to Matt for the original library, and Daniel for the AS3 port, great work.
ack, both the AS2 and AS3 versions wont accept zero or negative values as int parameters?
At the server end I'm getting:
{'type': 'int', 'value': '0'}
Instead of just a zero :(
Sorry, negatives work, zero not.
"Sorry, negatives work, zero not."
Hey Andy. I haven't been able to address your comment because I've been out of town on business. Were you able to resolve the issue with zero values?
Hi Daniel,
I'm afraid not, I've had to resort to converting all instances of 0 to 1 then converting them back on the server side :/
rpc.addParam(0, XMLRPCDataTypes.INT);
rpc.call('sendInt');
Returns at the server end: {'type': 'int', 'value': '0'}
rpc.addParam(1, XMLRPCDataTypes.INT);
rpc.call('sendInt');
Returns at the server end: 1
I've tested the xmlrpc server with a python client and it works fine with 0s from python.
Any help would be much appreciated.
I have found 2 bugs in MethodCallImp.as
anyone knows what its the purpose of this code?
because i´m not very familiar with xmlrpc
but this is what i did to fix it
Ricardo, thanks very much for sharing the code for the bug fixes! When I get a chance I'll test it out and add it to the download above.
Great news, thank you Ricardo!
Hi there,
Would someone explain what the following error means?
Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: https://
at com.mattism.http.xmlrpc::ConnectionImpl()[\src\com\mattism\http\xmlrpc\ConnectionImpl.as:57
The code seems to be creating the raw XML request correctly, but when it tries to send the XML-RPC request, that error is spitted out.
Help anyone? :(
Hi Magdalena,
That error occurs when there is a bad response (or no response) from the server. Looking at your error message, it seems that the URL is missing. When you call the constructor,
ConnectionImpl(url:String), make sure the full URL is being passed through.Hope that helps!
Hi danielgm! :)
I've passed the full URL to the ConnectionImp constructor.
Requesting the same XML-RPC from the same domain to the same target URL works though.
So I figure it's something to do with cross-domain policy restriction.
But even after placing a cross-domain file under the target domain root directory, it still issues the same error :(
The error seems to happen when URLLoader is loading the request (I placed another event listener for IOErrorEvent.IO_ERROR)
So when "this._response.load(request);" is being executed in ConnectionImpl.as, this is the full error came out:
[IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: https://[ROOT_DOMAIN]/~[USER_NAME]/cgi-bin/xmlrpc/v1/" errorID=2032]
I can't say I've had this issue before but there seems to be a lot of info following the link below. Maybe that will help if you haven't found it already. (-:
http://www.judahfrangipane.com/blog/?p=87
I'm currently working on a homework assignment that involves Flash using XML-RPC to interface with our professor's web service (So thanks to Matt and Dan so I didn't have to do this assignment in Java!) But I'm curious if the same security restrictions still apply from the AS2 to AS3 conversion.
I've everything setup the way it should be, but when I send my request I only ever receive error 'XMLRPC Fault (0):' and I'm unsure as to what's going on. Do I have to ask my prof to configure his server to allow Flash to interact with it like back in AS2?
Thanks for any help!
Hi Bruce,
I'm not sure what the error is without digging into it, but it could be the Flash player security. Might be a good idea to try the cross-domain policy file.
Good luck!
Dan,
Yeah, it was the lack of a policy file that was causing my problem and I can now receive a response.
My new problem, however, is that the responses are all empty!
For example, when I send out a request to my prof's server to get back an Array of Strings, which succeeds, but when I try to trace the data I get empty values:
trace( response ); -> ,,,
trace( response.length ); -> 4
The length is correct, but I cannot discern why the values are all empty.
I do not know if the fact that the webService was written in Java has any bearing on the matter. Classmates have been successful with other languages (Java, Python, Javascript).
Sorry again for being a nuisance and thank you!
To figure out what's going wrong, I'd start by opening up ParserImpl.as and adding a trace statement to the parse method to make sure all the values are actually in the XML:
If the response looks good, try setting the debug variable to true and see if any of the output from that helps.
In ParserImpl:
if (node.nodeKind() == 'text') { return node.*; }Why "node.*"? The text node doesnt have children does it???
I had a problem where the _parse method was being called with the node:
(but without those spaces!)
So, thats an element with a single child text node.
The response object was then empty (could this be related to the problem Bruce was having?).
My desired result was:
So I changed the above code (I know its a hack) to:
if (node.toXMLString().indexOf("<") >= 0) { var textParent:Object = node.parent(); var textString:String = textParent.toString(); return new XML(textString); } else { return node.*; }}
The textParent.toString() does all the decoding for me!!!
Problem with the formatting there:
if (node.toXMLString().indexOf("<") >= 0) {The "<" should be the encoded form (i.e. [ampersand]lt;)
Hi Mark,
I think you're right that
node.*can be replaced withnode.As for decoding the XML entities, I did some poking around and ended up writing an entire blog post on it, so check it out. It turns out that using
nodeinstead ofnode.*will fix the decoding problem.Dan
The library download link above has been updated. Enjoi.
Superb! Tremendous stuff, all works a treat. Thanks very much.
One other thing - is there any way to specify a timeout?
I'm calling the eXist XML-RPC but if eXist is not running then none of the listeners are ever called after a RPC call is made.
I didnt need to specify a timeout. Instead I just listen for an IO error by adding this line:
to just after you instantiate the URLLoader.
Hope this helps someone.
Hi I dont suppose you would have an idiot proof usage example that refers to wordpress blogs?
I thought it would be like,
_rpc = new ConnectionImpl('http://domain/blog/xmlrpc.php');
_rpc.addEventListener(Event.COMPLETE, rpcCompleteHandler);
_rpc.addEventListener(ErrorEvent.ERROR, rpcErrorHandler);
var arr:Array = new Array({type: XMLRPCDataTypes.STRING, value:"my blog id"},{type: XMLRPCDataTypes.STRING, value:"my user name"},{type: XMLRPCDataTypes.STRING, value:"my password"},{type: XMLRPCDataTypes.INT, value:10)})
_rpc.addParam(arr, XMLRPCDataTypes.ARRAY);
_rpc.call("metaWeblog.getRecentPosts");
but a trace of the response objects reveals every single property to be undefined - something must be going right here to even have these properties, right? but im not sure what it is that's wrong - i guess it could be wordpress and not flash specific and i may be asking the wrong people, but...
eg
for(var i in response)
{
trace(i + ' : ' + response[i]);
for(var j in response[i])
{
trace(' ' + j + ' : ' + response[j]);
}
}
reveals
0 : [object Object]
link : undefined
wp_password : undefined
post_status : undefined
mt_excerpt : undefined
wp_slug : undefined
userid : undefined
postid : undefined
dateCreated : undefined
mt_keywords : undefined
title : undefined
mt_allow_comments : undefined
date_created_gmt : undefined
categories : undefined
custom_fields : undefined
mt_text_more : undefined
description : undefined
permaLink : undefined
mt_allow_pings : undefined
wp_author_id : undefined
wp_author_display_name : undefined
if you have any ideas i'd be super grateful.
d
Using your code, which seemed to work beautifully for a few hours, however I came upon a security sandbox violation error. Any ideas on how I can get around this?
Here's the error I'm getting after passing rpc.call('metaWeblog.getRecentPosts'); Error actually occurs no matter what I pass through rpc.call.
Here's the error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at ParserImpl/parse()[\src\com\mattism\http\xmlrpc\ParserImpl.as:39]
at com.mattism.http.xmlrpc::ConnectionImpl/parseResponse()[\src\com\mattism\http\xmlrpc\ConnectionImpl.as:115]
at com.mattism.http.xmlrpc::ConnectionImpl/_onLoad()[\src\com\mattism\http\xmlrpc\ConnectionImpl.as:103]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
Any ideas?
Thankyou very much for the port of this library to AS3.
I am successfully using it to communicate back and forth between a Drupal CMS.
I just received this error when posting my last comment:
* warning: pg_query() [function.pg-query]: Query failed: ERROR: column "notify" of relation "dm_comments" does not exist at character 24 in /home/dmclaren/http/danielmclaren.net/includes/database.pgsql.inc on line 125.
* user warning: query: update dm_comments set notify = 1 where cid = 1223 in /home/dmclaren/http/danielmclaren.net/includes/database.pgsql.inc on line 144.
Thanks for the feedback, Ray. Should be fixed now...
Hi Steve,
My first guess would be a malformed response (combined with fairly rigid parsing). It looks like parseResponse is being called with a null value. You might try tracing the responseXML variable in ConnectionImpl at line 91 and check to see that the two following statements actually refer to an XML element.
Good luck!
When running locally i connect successfully to my XML-RPC server running on mydomain.com:8080. But when i upload the movie and run it from anotherdomain.com it's stops working.
It seems to be connecting to the server. However, the servers log output is:
This doenst happen when i run the same movie from my pc or if i run an html file with the movie embedded from my pc, any help will be much appreciated.
Not sure if a bad get request exception would result from it, but first thing that comes to mind is crossdomain policy files. Do you have these in place?
I wonder, is there any way to use xmlrpc calls to protected area ?
My weba pplication use Basic Auth, when I try to pass l/p via http string it fails. (e.g. https://login:pass@some.u.rl/
Thanks in advance.
I couldn't find the documentation of this project, even in the author's page.
Do anyone can post a link with useful information about this lib?
[I'am sorry for any english mistake =]
hello i am using as2 and the not new classes. how do i add a parameter whose datatype is file? im using the file reference class and i would like to send it via addParam function.
I need only an example showing how I can get information from the returned object. I am a newbie in AS and I don't have any idea how the library works (XML-RPC is not problem for me. I have coded a XML-RPC server and client in PHP without problems). I tried to read the source code, without sucess.
Can you post only a example of that involves interpretation of the returned data?
Does the zip file contain the latest code, Flash is giving me a lot of errors (99) such as...
A type identifier is expected after the ':'.
Hi,
Just wondering if Ricardo's bug fixes are implemented with the current download of the xml-rpc for AS3.
Thanks,
Rommel
So I was having passing struct's as they were getting sent as objects instead of xml nodes. So I changed line 164 in MethodCallImpl to the following:
var MemTypeNode:XML = <{parameter.value[x].type}>{parameter.value[x].value}; MemberNode.appendChild(MemTypeNode)I've ported the three wordpress files found in the original library.
Still testing, but seems to work :)
1) Wordpress.as
2) WordPressPost.as
WordpressCategories.as
Hi Twanneman,
Thanks for the wordpress class file code-listings. Much-appreciated!
Daniel
Hi Daniel!!
Thanks for the AS3 update version.
I was making that ("dirty") job also a few months ago, but because i did not have much free time i stopped.
I've been developping a simple API for Wordpress XML-RPC methods, i'm just finished to make all the codding work! :)
Sonner will be availiable for download.
The current methods supported are:
catLister.as -> Lists all categories in the blog.
insertCat.as -> Inserts a new category
deleteCat.as -> Deletes a category
insertPage.as -> Insert a new page.
deletePage.as -> Delete a page from blog
editPage.as -> Update a page with new changes
insertPost.as -> Inserts a new post
deletePost.as -> Deletes a post
editPost.as -> Update a post with new changes
getRecentPosts.as -> Get last posts (defined "how many posts")
getPages.as -> Get all blog pages
getAuthors.as -> Get authors/user in blog
getOptions.as -> Get blog seetings
uploadFile.as -> Send binary / image file to blog
That's it, gave me a few days to figure out some output from xml-rpc but the work is done! Probably the API will work also on other blogs platforms that use XML-RPC.
The API uses your classes, so the credits will be granted to you also!
When i release the API i'll notify you! For the moment i'm testing a smal AIR app to make sure all works.
Best Regards from Portugal! :)
i tried to copy and paste ricardo's bug fix from above, but the browser is processing the markup and the code is all worng. How can i get that fix in properly and does it address the original mishandling of '"' when sending over a String?
Thanks very much
Post new comment