我正在使用 ajax 和 php 从 yahoo Finance 获取实时价值,即 ftse。我想以数组格式存储所有值,并想比较最后 2 个最近更新的值。
以下是我的javascript:
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ftse.php",true);
xmlhttp.send();
}
setInterval(loadXMLDoc,1000);
</script>
以下是我的php 代码。
<div id="myDiv">
<?php
// Setup Variables
$stockList = "^ftse";
$f = "l1";
$host = "http://finance.yahoo.com/d/quotes.csv";
$requestUrl = $host."?s=".$stockList."&f=".$f."&e=.csv";
// Pull data (download CSV as file)
$filesize=2000;
$handle = fopen($requestUrl, "r");
$raw = fread($handle, $filesize);
fclose($handle);
// Split results, trim way the extra line break at the end
$quotes = explode("\n\n",trim($raw));
foreach($quotes as $quoteraw) {
$quoteraw = str_replace(", I", " I", $quoteraw);
$quote = explode(",", $quoteraw);
// output the first element of the array, the Company Name
}
echo "<br><center><font size='30' color='green'>".$raw."<br>";
?>
</div>
以下是我的ftse.php代码。
<?php
// Setup Variables
$stockList = "^ftse";
$f = "l1";
$host = "http://finance.yahoo.com/d/quotes.csv";
$requestUrl = $host."?s=".$stockList."&f=".$f."&e=.csv";
// Pull data (download CSV as file)
$filesize=2000;
$handle = fopen($requestUrl, "r");
$raw = fread($handle, $filesize);
fclose($handle);
// Split results, trim way the extra line break at the end
$quotes = explode("\n\n",trim($raw));
foreach($quotes as $quoteraw) {
$quoteraw = str_replace(", I", " I", $quoteraw);
$quote = explode(",", $quoteraw);
// output the first element of the array, the Company Name
}
echo "<br><center><font size='30' color='green'>".$raw."<br>";
?>
问题是如果值较小,则应以红色打印,如果大于则应以绿色打印。