Ignoring crossdomain.xml policy with AS3

February 8th, 2008

In AS2, when you wanted to load in an external file (i.e. .jpg) from a remote server that did not have a crossdomain.xml file allowing you to communicate with the server, you could easily override this with the MovieClipLoader class. As long as you did not try to alter the image with the Bitmap object, you were in good shape for the most part.

With MovieClipLoader depreciated in AS3, you can still override the absense of crossdomain.xml utilized on the remote server when loading in images. However, the way to do it is a little trickier. Observe:

package {

import flash.display.*;
import flash.events.Event;
import flash.net.*;
import flash.system.LoaderContext;

public class FeaturedProduct extends Sprite {

public var imgLoader:Loader;
public var context:LoaderContext;

public function FeaturedProduct(){

buildImage();
}

private function buildImage():void {
context = new LoaderContext();
context.checkPolicyFile = false;

imgLoader = new Loader();
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
var urlRequest:URLRequest = new URLRequest(”http://www.someremoteurl.com/images/sampleimg.jpg”);
imgLoader.load(urlRequest,context);
}

private function completeListener(e:Event):void {
this.addChild(imgLoader);
}
}
}

In particular, look at the LoaderContext() usage. Without going too much into detail, you can read much more thorough documentation on this at Adobe

Google API and crossdomain.xml

January 26th, 2008

Google has made some great API’s that can be super handy. However, I was updating a new [temp]orary site for Pitch that loads the current weather in Madison, WI using Google’s Weather API. Building this locally took me about an hour and everything worked great here. I was thrilled to be able to load in the XML data and parse it with E4x easily. However, when I published the page, the weather wasn’t loading. Sure enough, Google’s crossdomain.xml policy does not let just any ol’ domain request information from it. However, there is a way around this that is actually pretty easy with PHP. Using a proxy approach, you can ‘trick’ the URL path you are calling. So instead of calling “http://www.google.com/ig/api?weather=New York, NY” directly, you can call: “xml_proxy.php?url=http://www.google.com/ig/api?weather=New York, NY” and have PHP retrieve your data for you.

In that xml_proxy.php file resides the following code:

<?php
$post_data = $HTTP_RAW_POST_DATA;
$header[] = “Content-type: text/xml”;
$header[] = “Content-length: “.strlen($post_data);

// create a new cURL resource
$ch = curl_init( $_GET[’url’] );
// set Return Transfer, Timout and Htttp Header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

// grab URL and pass it to the browser
$xmlData = curl_exec($ch);

if (curl_errno($ch)) {
echo curl_error($ch);
} else {
curl_close($ch);
echo $xmlData;
}
?>

And then in ActionScript (I’m using AS3 here), you just point to the URL like this:

//load in xml file
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener( Event.COMPLETE, handleComplete );
xmlLoader.load( new URLRequest (’xml_proxy.php?url=http://www.google.com/ig/api?weather=New York,NY’) );

Make sure the xml_proxy.php file is on your server and that you are pointing to it correctly.

Give One XO Laptop, Get One

December 1st, 2007

Give One X0 Laptop, Get One

Until the end of December, One Laptop Per Child is offering something I was a little surprised (and excited) to see. If you are in the US or Canada, you can take part in their Give One Get One program, where you can donate a laptop to a child in the developing world and get one for yourself… ur… “the child in your life”. What I think is cool about this is that it allows you to have hands on experience with these laptops, since this is the first time they are being made to the general public and see, first hand, what the target recipients are going to experience.

I was a little torn with the idea of ‘I give you, you give me’ approach which is so typical in our society, but at the same time I want to see what all of the fuss is about and I would think that by buying one for myself, I’m helping counter the R&D costs that went into this. Eventually though, if I’m convinced enough, my plan is to donate more in 2008 and hopefully convince others to do the same. The idea sounds great.

Meta Serif Released

November 20th, 2007

I’m super excited about the release of FF Meta Serif. While I’ve been using FF Meta (sans) for Pitch branding, it seemed like such a good font not to have a serif version to accompany it. Last week, FontShop released the good news. So here we are:

Meta Serif

There’s a great article with the background of building this font here. It’s definitely a pricey font, but if you are serious about your type faces, this shouldn’t be a sticker shock… especially if you find a client to cover the cost.

Flash 10 enhancements?

October 9th, 2007

Imagine being able to convert C/C++ into ActionScript 3. Holy crap. At Adobe MAX 2007 in Chicago this year, one of the “potential” upcoming features in Flash 10 will be just that. Peter Elst had a few sneak peaks where he demonstrated this feature along with a few others. I hope they’re not just pulling our chain. This would be amazing.

Flash On the Beach, a perfect reason to go to England

September 19th, 2007

I’m heading out to Brighton, UK in November for my fourth conference this year. Highly recommended for anyone in the area or, like me, who just want to have a good reason to go to England and meet up with a bunch of friends. The speaker list is pretty amazing too.

flash on the beach

Flash Player 9 hitting over 90% of internet users

September 5th, 2007

Adobe recently posted updated penetration stats for the Flash 9 player. It’s great to see it hit over 90% and I glad I’ve been working with AS3 on several projects so far this year. Can’t wait for more and this is a great tool to justify it to clients.

Fast Food Visualizer

August 25th, 2007

I just posted a site that covers a topic I brought up at FlashBelt this year. Taking everyday accessible information and making it visually accessible. As a sample, I used fast food nutritional data. You can view the site and download the source code (AS3 and XML) here.

New Pitch Labs site

July 6th, 2007

There’s been so much going on lately that I simply don’t have time to update the Pitch Website. Unfortunately, it’s been a temporary page since Pitch started operating at the end of 2006… that doesn’t mean I have nothing to show. On the contrary, there are some exciting projects to boast as well as experimental works. Up to now, I’ve put some work up on my personal site, but there’s still quite a bit to say… so here we go with Pitch Labs. This site is going to be reskinned this summer, but the content will start flowing here that will primarily be geared towards some of the new things that I and Pitch’s summer intern Nick are working on.