Simple data storage from Flash to ExpressionEngine

26 Aug

Am working in a great project at the time. The backend is my fav cms ExpressionEngine and I play with audio programming, mutitouch, and phidgets all day. Good times!

Just thought that I’d post some experiences in getting data from Flash to ExpressionEngine since I’ve spent some time on that. The initial idea was to use SafeCracker with EE. This works to some extent, but there are a couple hiccups. Mostly it’s because of the XID required to do safe forms and 302 redirects. Due to this, we had to find a another path. For anything more advanced, we’ll use AMFie (ActionScript Remoting for EE, developed by bjornbjorn) but for the simple stuff, we’ll just use the Metaweblogs API plugin that comes with EE. It’s simple, but it’s all you need to just save some text easily (Getting it out is super easy with a custom template).

I was a little puzzled that there were only AS1 and AS2 libraries for XMLRPC. If you know of an updated AS3 Metaweblog API for Flash, please post it in the comments. Documentation for the Metaweblog API was rather crappy since the main site is broken but I found a couple resources that helped me solve how the XML were supposed to look. If you want to see how it’s done, there’s code in the extended entry. This is no API, but it does show how to insert data. It’s the same for the other methods.

Here’s the code snippet I use with the Metaweblog API:

        /**
         * Stores the users progression
         * Uses the Metaweblog API module in ExpressionEngine
         *  
         * @param session_id The id returned from getSession();
         * @param key A key you define for use with your application
         * @param value A value for the key
         * 
         */    
        public static function saveContent( session_id:String, key:String, value:String ):void
        {
            // The message that the MetaWeblog API wants
            var payload:String = "";
            payload += "";
            payload += "    metaWeblog.newPost";
            payload += "    ";
            payload += "        ";
            payload += "            2";                    // EE channel
            payload += "        

";
payload += "

"; payload += " myuser"; // EE user payload += "

";
payload += "

"; payload += " mypass"; // EE password payload += "

";
payload += "

"; payload += " "; payload += " "; // Struct with members per field payload += " "; payload += " title"; payload += " "+session_id+" - "+key+" - "+value+""; payload += " "; payload += " "; payload += " mt_excerpt"; payload += " "+session_id+""; payload += " "; payload += " "; payload += " description"; payload += " "+key+""; payload += " "; payload += " "; payload += " mt_text_more"; payload += " "+value+""; payload += " "; payload += " "; payload += " mt_keywords"; payload += " mt_keywords"; payload += " "; payload += " "; payload += " "; payload += "

";
payload += "

"; payload += " 1"; payload += "

";
payload += "

";
payload += "

";

var req:URLRequest = new URLRequest("http://example.com/?ACT=45&id=1"); // This value, you'll find on EE's metaweblog api module info page
req.method = URLRequestMethod.POST;
req.data = payload;

// Flash will add User-Agent and Content-Length for us, but Content-Type we set ourselves
var contentType:URLRequestHeader = new URLRequestHeader("Content-Type", "text/xml");
req.requestHeaders.push(contentType);

xml_request = new URLLoader();
xml_request.addEventListener( Event.COMPLETE, saveContentResult );
xml_request.addEventListener( IOErrorEvent.IO_ERROR, error );
xml_request.addEventListener( HTTPStatusEvent.HTTP_STATUS, httpResponse );
xml_request.load(req);
}

26 August 2011 at 2:07 pm

0 Responses to Simple data storage from Flash to ExpressionEngine

  1.