Scripting the weather question

In my post about a flash weather module, I mentioned the word *proxy* as the thing that gets your flash to read xml while out there on the web. When you’re a designer working in flash, and not in the vicinity of some kind developer to help you out, this can be a little too cryptic and mystical. Here’s a clarification of the proxy thing, and some helpful code.

simon

I’ve been working on a personal project in flash where the scene would be affected from weather reports on the bbc, however having issues with the cross domain security thingy. I was reading your blog and just wondered if you could explain how you would go about implimenting the “the local proxy” into flash…have no idea what the code would look like for this. At the moment everything works in the flash preview while working but just doesn’t read the data when live on the internet!

hanneke

ah, not hard to answer… The local proxy would be a php file on the server, the same server the flash is on (might be in another folder). The flash gets its xml through the proxy – meaning, you construct a url pointing to the php-file, preferably with a querystring to make it more re-usable, and the php in its turn gets the xml-data. It passes that data then on to your flash, and in doing that, it seems to the flash that the data is from the same domain.

Do you have access to a server that can handle php? If not, you’d need an asp or a jsp proxy – whatever your server likes.

The url your flash would need to request is something like (“querystring” is everything after the questionmark):

var proxyPath = "http://www.example.com/includes/proxy.php?data=";
var weatherUrl = escape("http://www.otherdomain.com/xmlfeed");
dataUrl = proxyPath + weatherUrl;

…and in case you go crazy testing this live, use a simple switch to get back to your local published preview – un-comment “localtesting=true” to test using the local flash player:

//localtesting = true;
if(localtesting){
	locId = "6260"; //6260: de Bilt - 6240: Amsterdam Schiphol
	dataUrl = "data/buienradar_2009.xml";
}else{
	//var stationCode dyn from html, global var dataUrl static
	locId = stationCode;
	var proxyPath = "http://www.yourdomain.com/includes/proxy.php?data=";
	var weatherUrl = escape("http://www.externaldomain.com/xmlfeed");
	dataUrl = proxyPath + weatherUrl;
}
trace('getting dataUrl: '+dataUrl);

getWeerData(dataUrl,locId); //start loading xml

hanneke

The php-file, “proxy.php”, contains just this:

<?php
//check for allowed domain
$allowed = "yourdomain.com";
$domain = $_SERVER['HTTP_HOST'];

if($domain == $allowed){
	//get url from querystring
	$xmlData = $_GET["data"];

	header("Content-Type: text/xml");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-cache, no-store, must-revalidate");

	$returnXml = file_get_contents("$xmlData");
	echo $returnXml;
}else{
	echo 'your IP has been logged; tnx for trying.';
}
?>

simon

omg!!!! It worked even for some twitter stuff–

hanneke

The proxy trick even works for images, if you change the header (in the php-file) to:
header("Content-Type: image/png");
Handy if you ever want to display images from a twitter feed.

simon

Just one question do you know what the corect datatypes for those three variables? it didn’t like it when I set them as strings, when I removed the data type it worked like anything!

hanneke

I think the datatype of String is correct, but using the escape function locally (not over the web) might cause things to go bums up. I’ve done a twitter-reading thing just the other day, looked up the code I used there, and this is what I have there (feedUrl typed as private var feedUrl:String;):

//----RSS loading----
private function assembleTheUrl($id):String{
   if(ClipRefs.localtesting){
      feedUrl = "http://search.twitter.com/search.rss?q=from%3A"+$id;
   }else{
      var tUrl = escape("http://search.twitter.com/search.rss?q=from:"+$id);
      feedUrl = ClipRefs.proxyPath + tUrl;
   }
return feedUrl;
}

that ClipRefs.localtesting is a Boolean I set so I can check everywhere if I’m local or live…

If you’re playing around with Twitter – here’s a little thing I noticed: the policies for the http://twitter.com/ server will only allow so many requests per hour, but the http://search.twitter.com/ server has a less strict policy, and you can send more requests to it. So if your app stops working after 20 minutes or so, take a look at the search-twitter option… Twitter doesn’t advertise this of course… will save you a few days of testing…

simon

I spent a couple of weeks posting and searching forums to try and fix this!

hanneke

I know… best to have a couple of friendly developers as collegues… they would know about escape and proxy…

simon

Thanks for the headsup on that search twitter server, just pulled out my first post using that instead. Am planning on adding some bird like creatures to the scene which will display in a speech bubble their own thoughts pulled in via twitter, its fun experimenting and will provide backlinks to the final site. I’m more a designer than a developer but really enjoy coding in actionscript, planning on giving AS3 a shot some time later in the year or next year. How reliable is the twitter search server?

hanneke

better than twitter.com – when twitter has downtime, that’s the first to go – but still, I’m looking at a dead search.twitter.com feed in my rss reader right now… correction… it’s working again… nuff said :-) I like that idea of twitter speech bubbles. The twitter example I sent was from a commercial project with 15 feeds “live” on a world map – now offline – I showed the tweets at random intervals from 3 to 10 secs, and refreshed every 3 minues or so ; very “lively”.

Related to the weather thingy, could you use a moon in your scene as well? Some weather feeds give you the time of sunset/sunrise, and from that you know when it’s night; I’ve figured out a way to calculate the moon phase from the current date, and I bore everyone to death with it, me thinkig it totally awesome to show the actual moon phase in a weather app, and my collegues not able to care less…

Glad to’ve been of help; I moved from being a true-blood print designer to web designer to front end developer, so I guess I just recognized the question you had. :-)

0

in Q&A, snippets+++

post a commentsave the linkrate as favorite

leave a comment

Your e-mail address will not be made public or shared with others. Required fields are marked with a *

*
*

Optionally, you could use these HTML tags: <b> <cite> <code> <i> <strike> <strong>