Flash "/>

Simple data storage from Flash to ExpressionEngine

26 August 2011 at 2:07 pm

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 += "<methodCall>";
            payload += "    <methodName>metaWeblog.newPost</methodName>";
            payload += "    <params>";
            payload += "        <param>";
            payload += "            <value><i4>2</i4></value>";                    // EE channel
            payload += "        </param><p>";<br />
            payload += "        </p><param>";
            payload += "            <value>myuser</value>";                        // EE user
            payload += "        </param><p>";<br />
            payload += "        </p><param>";
            payload += "            <value>mypass</value>";                        // EE password
            payload += "        </param><p>";<br />
            payload += "        </p><param>";
            payload += "            <value>";
            payload += "                <struct>";                                // Struct with members per field
            payload += "                    <member>";
            payload += "                        <name>title</name>";
            payload += "                        <value>"+session_id+" - "+key+" - "+value+"</value>";
            payload += "                    </member>";
            payload += "                    <member>";
            payload += "                        <name>mt_excerpt</name>";
            payload += "                        <value>"+session_id+"</value>";
            payload += "                    </member>";
            payload += "                    <member>";
            payload += "                        <name>description</name>";
            payload += "                        <value>"+key+"</value>";
            payload += "                    </member>";
            payload += "                    <member>";
            payload += "                        <name>mt_text_more</name>";
            payload += "                        <value>"+value+"</value>";
            payload += "                    </member>";
            payload += "                    <member>";
            payload += "                        <name>mt_keywords</name>";
            payload += "                        <value>mt_keywords</value>";
            payload += "                    </member>";
            payload += "                </struct>";
            payload += "            </value>";
            payload += "        </param><p>";<br />
            payload += "        </p><param>";
            payload += "            <value><i4>1</i4></value>";
            payload += "        </param><p>";<br />
            payload += "    </p>
</params><p>";<br />
            payload += "</p>
</methodCall><p>";<br />
            <br />
            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<br />
            req.method = URLRequestMethod.POST;<br />
            req.data = payload;<br />
            <br />
            // Flash will add User-Agent and Content-Length for us, but Content-Type we set ourselves<br />
            var contentType:URLRequestHeader = new URLRequestHeader("Content-Type", "text/xml");<br />
            req.requestHeaders.push(contentType);<br />
            <br />
            xml_request = new URLLoader();<br />
            xml_request.addEventListener( Event.COMPLETE, saveContentResult );<br />
            xml_request.addEventListener( IOErrorEvent.IO_ERROR, error );<br />
            xml_request.addEventListener( HTTPStatusEvent.HTTP_STATUS, httpResponse );<br />
            xml_request.load(req);<br />
        }