Printing multi line XML while maintaining the formatting in PHP

A co-worker of mine introduced me to this tip for maintaining the formatting of multi line information in PHP; in this case it is XML. It uses heredoc. The syntax is basically three <<< and then the name of the type of variable...

Here is a code snippet of it in use:

<?php
 
echo <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<someNode>
	{$massiveAmountOfXML}
</someNode>
XML;
 
?>

HTTP Post in PHP without a Form

For one reason or another I had trouble getting this to work so I thought I would post my code snippet that I used here.

<?php
<?php
ini_set('display_errors', E_ALL);
 
class PostWithOutAForm {
 
	var $_returned_message;
	var $data = array();
 
	public function __construct() {	}
 
	public function callPong($msg) {
		$data['message'] = $msg;
 
		$post_str = '';
		foreach($data as $key=>$val) {
			$post_str .= $key.'='.urlencode($val).'&';
		}
		$post_str = substr($post_str, 0, -1);
 
		// Initialize cURL
		$ch = curl_init('http://localhost:8888/TargetOfPost.php');
		curl_setopt($ch, CURLOPT_POST,				count($data));
		curl_setopt($ch, CURLOPT_POSTFIELDS, 		$post_str);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 	TRUE);
 
		// Execute...
		$output = curl_exec($ch);
 
		 if (!$output) {
			$output = curl_error($ch);
		}
		curl_close($ch);
 
		// Display the result
		return $output;
	}
}
?>

Conditional Switch Statements in AS3

This is something I wasn’t aware that you could do in a switch/case statement until recently.  A lot of times developers will handle this with nested if/else statements.

For Example:

var myNum:Number = 7;
 
switch (true)
{
  case (myNum < 5):
  trace ("< 5")
break;
 
case (myNum > 5):
  trace ("> 5")
  break;
}

Using switch (true)/switch (false) is pretty cool. You can use it for any sort of true/false comparisons, including comparing strings. The first one to evaluate true will trip the statement.

Exclude Filter for Google Analytics Tracking

1. Create a bookmark in your browser with the code snippet below:

javascript:var pageTracker = _gat._getTracker("UA-#######-1"); pageTracker._setVar('remove-me');alert('Set.')

2. Replace ####### with your Google Analytics code.
3. Create a filter in GA to exclude the cookie (remove-me) from tracking.
4. When you visit your site, hit this bookmark.

You could also create a page to set the cookie…

Use WordPress as a CMS for Flash

Press2Flash (not to be confused with FlashPress) seems to be a very full featured way of using WordPress as a CMS for your Flash site. You can get all your posts/comments etc and present them however you would like. There is ASDoc documentation for it and the example he provides in the download is very simple to use. If you are looking for a CMS for your Flash site, give Press2Flash a look.

Load Drupal view with Flash and AMFPHP

I recently have been getting into Drupal at work and one of the things we were needing to do was get information out of our Drupal cms install to use in Flash. One method for doing this is to use the NetConnection class in Flash for which I have the code available to download below. In order to do this, setup your Drupal install with the proper modules and permissions.

1. Download and enable a few modules in Drupal.
* AmfPHP
* Services (enable views service)
2. Create a view with the info you want to show in flash
3. Set the proper permissions on your views. (Described in the modules’ documentation)
4. Download my code below, putting in the path to your amfphp install as well as the name of the view you created in step 2.

Note: I had to downgrade to php 5.2.13 in my MAMP install because I was getting the “Function ereg() is deprecated” error but this can be fixed as described in the post if you want to use PHP 5.3+

I am hoping that someone will make a zendamf module since amfphp is not being actively improved.

  NetConnection.zip (24.4 KiB, 124 hits)

Embed XML in AS3

There are two methods that I have found for for doing this. (Essentially the same thing, just depends on your coding style and how you want to keep things organized.)

Method 1:

  Embed XML (8.9 KiB, 135 hits)

package  {
 
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.utils.ByteArray;
 
	public class EmbedXML extends MovieClip {
 
		[Embed(source="my.xml", mimeType="application/octet-stream")] 
		protected const EmbeddedXML:Class;
		private var _xml:XML;
 
		public function EmbedXML():void  {
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}
 
		private function init(e:Event):void {
			//_xml = XML(new EmbeddedXML());
 
			var contentfile:ByteArray = new EmbeddedXML();
			var contentstr:String = contentfile.readUTFBytes( contentfile.length );
			_xml =  new XML( contentstr );
			trace(_xml);
		}
	}
}

Method 2:

  Embed XML Class (8.9 KiB, 103 hits)

//.as file that you embed the xml in
package {
	import flash.utils.ByteArray;
 
	[Embed(source="my.xml",mimeType="application/octet-stream")]
 
	public class MyXML extends ByteArray {
 
		public function MyXML():void {	}
	}
}
//usage:
package  {
 
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.utils.ByteArray;
 
	public class EmbedXMLTwo extends MovieClip {
 
		private var _xml:XML;
 
		public function EmbedXMLTwo():void  {
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}
 
		private function init(e:Event):void {
			//var myxml:MyXML = new MyXML();
			//_xml = XML (myxml);
 
			var contentfile:ByteArray = new MyXML();
			var contentstr:String = contentfile.readUTFBytes( contentfile.length );
			_xml =  new XML( contentstr );
			trace(_xml);
		}	
	}
}

Load XML with LoaderMax

I did have my own set of classes that I use for loading files(images/xml etc) but since I use Greensock for tweening I figured I should use it for loading my assets as well.

//import the classes, u dont need easing for this but you should be using it
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.*;
 
//above your class constructor function
private var _loaderMax:LoaderMax;
private var _xml:XML;	
 
//in your code where you want to use it
private function loadXML():void {
_loaderMax = new LoaderMax({name:"mainQueue", onComplete:completeHandler, onError:errorHandler});
_loaderMax.append( new XMLLoader("yourFile.xml", {name:"xmlDoc"}) );
_loaderMax.load();
}
 
private function completeHandler(event:LoaderEvent):void {
_xml = LoaderMax.getContent("xmlDoc");
}
 
private function errorHandler(event:LoaderEvent):void {
	trace("error occured with " + event.target + ": " + event.text);
}

Save in Flash Builder causes Flash to Publish

If you are coding your Flash project in Flash Builder, this can be very annoying. To make sure this does not occur, go into your Flash Builder 4 Preferences, select General, then Workspace, and then uncheck Build Automatically.

Create a png with php and as3

For a project I was on awhile ago, I needed to decorate a character with different items of clothing. When the user was done ‘dressing’ their character, they would press a button and the whole character would shrink down so it would fit as an ornament on a tree. In order to not confuse the issue, I am going to only show the code that enables you to take a movieclip and create a png from it. In order to do this, you will need to include the Flex SDK as a part of your flash build (for the Base64Encoder class) and you need to download as3corelib. To include the Flex SDK, in your Flash preferences add this line: (Mac OSX)

//VERSION will prob be 3.5, 4 etc...
/Applications/Adobe Flash Builder 4/sdks/flex_sdk_VERSION/frameworks/projects/framework/src

Code after the jump…

  CreatePNG (44.4 KiB, 173 hits)

Read more

 

Switch to our mobile site