Sunday, 19 July 2009

Flex/AIR PHP evaluator

Hi Readers,

OS X hasn't got [readline] for PHP. That is why you can't use [php -a] in command line for testing PHP codes. Sad. If you want one, you can get a similar from: by Stefan Fischerländer and David Phillips. It's an easy replacement, however, I wanted to use one that can keep my command history. If you use macports, you maybe win but I don't want to use that. (I heard it is kinda evil sometimes.) Recently I have found a bestof documentation of Flex at (Have I mentioned it is free? :) Oh yeah.) So I made a really simple AIR php -a replacement.

Concept is stupid easy. An AIR app sends a POST request for a PHP script lies on my localhost. This script evaluate the text I want to check and sends back. See? K, check the code:
The AIR part:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="" layout="absolute">

public function eval():void {
var url:String = "http://localhost/flex_eval.php";
var ur:URLRequest = new URLRequest(url);
var ul:URLLoader = new URLLoader(ur);
var uv:URLVariables = new URLVariables();

uv.phpsnippet = php_snippet.text; = uv;

ur.method = URLRequestMethod.POST;

ul.addEventListener(Event.COMPLETE, evalComplete);
ul.addEventListener(IOErrorEvent.IO_ERROR, ioError);


public function ioError(event:IOErrorEvent):void {
php_result.text = "Missing PHP file from:\n" +
"http://localhost/flex_eval.php\n\n" +
"Create one with the following code:\n\n" +
"<?php\n" +
"echo 'Flex PHP Snippet results ('.date('H:i.s').')'.\"\\n\\n\";\n" +

public function evalComplete(event:Event):void {
php_result.htmlText =;

<mx:TextArea left="10" right="279" top="10" bottom="40" id="php_snippet" backgroundColor="#2E2E2E" fontFamily="Courier New" color="#FFFFFF" fontWeight="normal" fontSize="12"/>
<mx:TextArea width="261" right="10" top="10" bottom="10" id="php_result"/>
<mx:Button label="Eval" bottom="10" left="10" right="279" click="eval();"/>

And the PHP part:
echo 'Flex PHP Snippet results ('.date('H:i.s').')'."\n\n";

You can compile the mxml stuff in Flex Builder 3.0 (and I guess also with the free Flex sdk). And here you are a quick downloadable version:

Guddai readers,

No comments:

Post a Comment

Note: only a member of this blog may post a comment.