This topic is separated into two parts –storing and retrieving. Retrieving is in another topic. Imagine storing values to a spreadsheet that Flash can retrieve and store them into variables wherein the designer or the project manager no longer has to request for you to change the values. They can change them, themselves. For example, if you have a war game that has a “Bullet Speed” in it, you can just place a column in the spreadsheet and the designers can enter desired values themselves. A self-documenting and a better way to interact with other development teams.

Before we start, there are certain stuffs that you need to setup and download:

1. Firefox and Firebug, which enables us to determine the page header’s request and response values. Once Firebug is installed in Firefox, press F12, go to the “Net” tab and select “Enabled”

2. XAMPP (with cURL enabled) for Windows, which enables us to create a crossdomain policy file that gives us privileges to write into Google’s spreadsheets

3. A Google account to create our spreadsheets

Setting up the spreadsheet

Login to Google and create a spreadsheet. In the empty spreadsheet, search for the phrase “Public on the web”, click “Change”, select “Public on the web” and check “Allow anyone to edit (no sign-in required)”. Save and close the dialog.

Click Form > Create a form. A popup window will appear.

In the Question Title, remove the word “Sample Question 1” and enter the word “BulletSpeed” and click Done.

Remove “Sample Question 2” by clicking its Trash icon.

Click Save.

Before closing the popup window, take note of the formKey value below the window and save it. This value is required when we submit values from Flash to the spreadsheet.

Close the popup window.

By the time the popup window closes, the spreadsheet creates a “Timestamp” and “BulletSpeed” columns in rows A1 and B1, respectively. Timestamp is automatically created by the spreadsheet, which tracks changes in the spread.

Leave the spread like that for now and proceed to setting up the PHP directory and crossdomain proxy file.

Setting up a project directory and crossdomain proxy file using PHP’s cURL

In your XAMPP’s HTDocs directory, create a new directory called WarGame. Inside that directory, create a php file called crossdomain_proxy.php and paste this code:

	/* Only proxy to URLs matching this */
	$sProxyUrlRegex = '#^';
	* Make a simple status/error page.
	function showErrorStatus ($iCode=500, $sText='Internal Server Error')
		header("HTTP/1.1 $iCode $sText");
		header("Status: $iCode $sText");
		echo "<html><head><title>Oops</title></head><body><h1>" . $sText . "</h1></body></html>";

	* Replacement for curl_setopt which only exists in PHP>=5.1.3
	* @param resource $rCh
	* @param array $aCurlOpt
	function local_curl_setopt_array ($rCh, $aCurlOpt)
		foreach ($aCurlOpt as $sKey=>$sVal) {
			if (!curl_setopt($rCh, $sKey, $sVal)) {
				return false;
		return true;

	/* Set cURL options */
	function init (){
		$aCurlOpt = array (
			CURLOPT_FAILONERROR		=> true, // Fail on non-200 responses
			CURLOPT_POST			=> true,
			CURLOPT_HEADER			=> false, // Don't include header in response
			CURLOPT_USERAGENT		=> 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)',

		/* Check URL is specified */
		if (!isset($_POST['_url']) || empty($_POST['_url'])) {
			showErrorStatus(400, 'No URL specified');

		/* Set cURL URL and remove from POST before forwarding */
		$sCurlUrl = $_POST['url'];

		/* Make POST string */
		$aPostData = array();
		foreach ($_POST as $sKey=>$sVal) {
			$sKey = str_replace('_', '.', $sKey); // Revert PHP conversion nonsense
			$aPostData[] = urlencode($sKey) . '=' . urlencode($sVal);

		$sPostData = join('&', $aPostData);

		/* Do cURL stuff */
		if (false == preg_match($sProxyUrlRegex, $sCurlUrl)) {
			showErrorStatus(401, 'Naughty, you can\'t proxy to ' . $sCurlUrl);
		} else if (!$rCh = curl_init($sCurlUrl)) {
			showErrorStatus(NULL, 'Couldn\'t initialize cURL to ' . $sCurlUrl);
		} else if (false == local_curl_setopt_array($rCh, $aCurlOpt)) {
			showErrorStatus(NULL, 'Couldn\'t set cURL options');
		} else if (false == curl_setopt($rCh, CURLOPT_POSTFIELDS, $sPostData)) {
			showErrorStatus(NULL, 'Couldn\'t set POST vars');
		} else if (false == ($sResponse = curl_exec($rCh))) {
			showErrorStatus(NULL, 'Response was false');
		} else {
			/* All good */
			echo $sResponse;

Coding the AS3

function storeVariables():void
	var loader:URLLoader = new URLLoader();
	var variables:URLVariables = new URLVariables();
	variables["entry.0.single"] = "hello world";
	var key = "0AlmPB8Zwh3pZdDBjMzY5TzJjZnhTSmFmbmlyVkJXNWc";
	variables["url"] = ""+key;
	//Call the proxy page passing all the variables above.
	var request:URLRequest = new URLRequest("crossdomain_proxy.php"); = variables;
	request.method = URLRequestMethod.POST;
	loader.addEventListener(Event.COMPLETE, function(e:Event):void{

Proving the spreadsheet’s field naming convention

You may be wondering, how did “entry.0.single” get in there? I myself isn’t quite sure as to why Google named it that way. However, to determine how I got this property, I still use Firebug.

Go to the Google spreadsheet…

Tracking the changes by watching the Cache

A simple note whenever you’re trying to modify any of the .php or .as files, always clear the browser’s cache. To prove that the .php file is cached, go to the method “showErrorStatus” in “crossdomain_proxy.php”, try deleting a simple greater-than symbol in the last portion of the echoed string and determine its value in the Firebug’s “Net” tab.

echo “<html><head><title>Oops</title></head><body><h1>$sText</h1></body></html”;

Refresh your browser without clearing the cache and launch Firebug.

Locate “POST crossdomain_proxy.php” and go the the “Response” tab. If the output is not the same on the above, then your Flash page is being cached by the browser.

There you have it! Storing values to the Google spreadsheet using AS3. You may want to proceed in Retrieving Values from Google Spreadsheets using AS3 to continue on the second part of this post.