Question

Is it possible to read the realtime updated quotes in this website through actionscript3, without having to reload the page all the time, which would be network-heavy.

In other words, is it possible to stream the quote-data directly into actionscript3 or eventually PHP from javascript?

If not does anyone have any suggestion to how to get these quotes into actionscript?

Was it helpful?

Solution

Expanding on what Theo Said... (I'm sure there is a much slicker way to do it and I'll be denounced as a hack but it works damnit)

I'm essentially just extracting the first two numbers under the EUR/USD in red.

Here's the php script to put on your server called getContent.php

<?php

$handle = fopen("getVars.php", "r");

$contents = '';
    while (!feof($handle)) {
     $contents .= fread($handle, 8192);
}

$vars = explode("&",  $contents);
$time = substr($vars[2], 5);
$difference = abs(date(s)-$time);

if($difference>5)
{

    $handle = fopen("http://www.fxstreet.com/technical/currencies-glance/pair.aspx?id=EUR/USD", "r");

    $contents = '';
        while (!feof($handle)) {
         $contents .= fread($handle, 8192);
    }

    $contents=trim($contents);
    $pos1 = strpos($contents, 'lhtml_0" innerOnUpdate="att=BID" innerfilter="format_number">');
    $str1 = substr($contents, $pos1, 100);
    $cur1 =  substr($str1, 61, 6); 
    $pos2 = strpos($contents, 'lhtml_1" innerOnUpdate="att=ASK" innerfilter="format_number">');
    $str2 = substr($contents, $pos2, 100);
    $cur2 = substr($str2, 61, 6); 

    $cachedPage = fopen("getVars.php", "w");
    $varString = "cur1=$cur1&cur2=$cur2&time=".date(s);
    fwrite($cachedPage,$varString);
    fclose($cachedPage);
    echo "cur1=$cur1&cur2=$cur2&cached=false";
}
else
{
    $handle = fopen("getVars.php", "r");

    $contents = '';
        while (!feof($handle)) {
         $contents .= fread($handle, 8192);
    }

    echo $contents."&cached=true";
}

fclose($handle);
?>

And then the actionscript

var updateTimer:Timer = new Timer(5000);
updateTimer.addEventListener(TimerEvent.TIMER, getQuotes);
updateTimer.start();
function getQuotes(e:Event):void
{
    var request:URLRequest = new URLRequest ("getContent.php");     
    var loader:URLLoader = new URLLoader (request);
    loader.addEventListener(Event.COMPLETE, onComplete);
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    loader.load(request);
}

function onComplete (event:Event):void
{
   var variables:URLVariables = new URLVariables( event.target.data );
   currency1.text = variables.cur1;
   currency2.text = variables.cur2;
}
var e:Event;
getQuotes(e);

You can see it in action here... http://www.hupcapstudios.com/getCurrency.swf

The hack part was my parsing of the page in php. I had to do some serious substring action. I'm sure anyone with a decent amount of parsing ability could write a cleaner code for extracting all the data you need.

I just thought I'd take a swing at it. Good luck :)

OTHER TIPS

maybe create a server side script checking the content of the site every 5 seconds or so. The script could parse out a "cached" version of the "quote" your looking to retrieve. Then just request this cached content via URLRequests at short intervals from your flash application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top