javascript - 从 JavaScript 更新的 html 读取数据

标签 javascript actionscript-3 data-structures finance

是否可以读取 this website 中实时更新的行情通过actionscript3,无需一直重新加载页面,这会占用大量网络。

换句话说,是否可以将报价数据直接从 javascript 流式传输到 actionscript3 或最终 PHP?

如果没有,有人对如何将这些引用放入 ActionScript 有任何建议吗?

最佳答案

扩展西奥所说的内容...... (我确信有一种更巧妙的方法可以做到这一点,我会被谴责为黑客,但它确实有效)

我本质上只是提取欧元/美元下的前两个红色数字。

这是要放在服务器上的名为 getContent.php 的 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);
?>

然后是 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);

您可以在此处查看它的实际效果... http://www.hupcapstudios.com/getCurrency.swf

黑客部分是我在 php 中解析页面。我必须做一些严肃的子字符串操作。我确信任何具有一定解析能力的人都可以编写更清晰的代码来提取您需要的所有数据。

我只是想尝试一下。祝你好运:)

关于javascript - 从 JavaScript 更新的 html 读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1521088/

相关文章:

c - 从随机 id 中检索元素的最佳算法

user-interface - 网页上的层次结构表示

javascript - .then() 在内部 Promise 解析之前自行解析

javascript - 滚动到 id 函数 jQuery

flash - 为什么AS3默认不使用弱引用? (在事件监听器中)

flash - 在 AS3 中滚动不流畅

循环 : "Uncaught TypeError: Cannot read property ' value' of undefined"中的 Javascript 引用

javascript - 单击按钮在 GoogleMap 中移动

javascript - 我如何找到在环绕时间轴上的两个点前进或后退?

python - 在没有循环的情况下删除 O(1) 中双嵌套字典中的键及其值?