Help on Real time mulitple stock quote script  - php or javascript

Treder
Treder used Ask the Experts™
on
Hi,

I'm trying to create a script which outputs basic information on 5 stocks, which updates every 30 seconds or so.  

In fact there is a google app based script which comes very close to showing what I'm trying to do already.  I have included it (via iframe) to the following page:

http://www.nwlist.net/test/stockquote/index.html 

It updates and gives a colour class to the price change according to '+' or '-'.  However the Ads by Google etc and the fact I can't style it makes it a no-go for me.

I've hammed together a php page which outputs the basic info I need but it doesn't update and doesn't interpret the different class for the price change:

http://www.nwlist.net/test/stockquote/basic.php

The php code for this (minus body and html tags is:

<style type="text/css">
#boxtable {
	border-bottom: 7px solid #9BAFF1;
    border-collapse: collapse;
    border-top: 7px solid #9BAFF1;
    font-family: "Lucida Sans Unicode","Lucida Grande",Sans-Serif;
    font-size: 12px;
    margin: 10px;
    width: 300px;
}
#boxtable td {
    background: none repeat scroll 0 0 #E8EDFF;
    border-left: 1px solid #AABCFE;
    border-right: 1px solid #AABCFE;
    color: #666699;
    padding: 8px;
}
</style>
</head>

<body>


<table id="boxtable">
  <tr>
<?php
$row = 1;
if (($handle = fopen("http://finance.yahoo.com/d/quotes.csv?s=^FTSE&f=nl1c1", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo "<td>" . $data[$c] . "</td>\n";
        }
    }
    fclose($handle);
}
?>
  </tr>
  <tr>
<?php
$row = 1;
if (($handle = fopen("http://finance.yahoo.com/d/quotes.csv?s=^GSPC&f=nl1c1", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo "<td>" . $data[$c] . "</td>\n";
        }
    }
    fclose($handle);
}
?>
  </tr>
  <tr>
<?php
$row = 1;
if (($handle = fopen("http://finance.yahoo.com/d/quotes.csv?s=E3X.FGI&f=nl1c1", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo "<td>" . $data[$c] . "</td>\n";
        }
    }
    fclose($handle);
}
?>
  </tr>
  <tr>
<?php
$row = 1;
if (($handle = fopen("http://finance.yahoo.com/d/quotes.csv?s=^N225&f=nl1c1", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo "<td>" . $data[$c] . "</td>\n";
        }
    }
    fclose($handle);
}
?>
  </tr>
  <tr>
<?php
$row = 1;
if (($handle = fopen("http://finance.yahoo.com/d/quotes.csv?s=510210.SS&f=nl1c1", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo "<td>" . $data[$c] . "</td>\n";
        }
    }
    fclose($handle);
}
?>
  </tr>
</table>

Open in new window


I would really appreciate help with this.  I've spent probably a couple of days now looking at all sorts of different implementations and examples trawled on the www.  But nothing either works or give me what I want.  Here are some of the resources I've tried:

http://usefulphp.blogspot.co.uk/2010/04/stock-quotes-with-php.html 
http://www.phptoys.com/tutorial/getting-stock-quote.html
http://www.phptoys.com/product/micro-stock.html

And for any others reference, I found this page giving yahoo quotes.csv format tags very useful:
http://www.gummy-stuff.org/Yahoo-data.htm

Best,
Tony
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
What is the question here?  Do you need help developing an application?  If so, it might be good to hire a professional developer.  You may need to have some budget to buy access to a data source for this kind of information.  My clients pay about $2,000 per month for a Bloomberg data feed and they get this information in near-real-time.  It is cheaper to get it with a delay and there are even some "free" (meaning "advertising supported") data feeds out there.  Looks like you have found some of those.

Here is an example of the Yahoo return data:
"S&P 500", 1385.14, -5.64
Given that, you can look at the third field and interpret it as a positive number (green) or negative number (red).  You can use inline CSS styling to color the display.  I believe that this would be $data[2] in the code samples above.  You can use a client-side action, maybe something using AJAX or jQuery to cause repeated queries against the Yahoo web service.  You might want to check the terms of service carefully before you do this -- there may be limitations on how often you can call the service or how you are permitted to use the data you get back from the service.

Author

Commented:
Ray,
Many thanks for your reply. $2000 a month - i hope whatever they are doing its paying them back!  My needs are much more crude - its basic info and doesnt have to Be real time.  I'll check the terms of service, hopefully I won't get too down in the dumps.

I'm a front end guy, so I'm comfortable in HTML/CSS land. But i try to have a go at any php that i think i might be ok at.  Actually I did task this to a developer for $$ and they solution that was offered was based on the first URL I gave above (but I've tidied it up a bit!).

Seems which ever place a turn to on this, there is another hurdle to jump.

Many thanks.
Most Valuable Expert 2011
Top Expert 2016
Commented:
My client is a privately held multi-billion dollar financial management firm so $2,000 a month is about the same as what they pay for janitorial services.  But that aside, please have a look at this.  Let me know if that gets you closer to your objectives.
http://www.laprbass.com/RAY_temp_treder.php
<?php // RAY_temp_treder.php
error_reporting(E_ALL);
echo "<pre>";

// STOCK QUOTES WE WANT
$names = array
( 'FTSE'
, 'GSPC'
, 'E3X.FGI'
)
;

// REMOTE DATA SOURCE
$url = "http://finance.yahoo.com/d/quotes.csv?s=^####&f=nl1c1";

// ACQUIRE THE DATA
$dat = array();
foreach ($names as $name)
{
    $lnk = str_replace('####', $name, $url);
    $str = file_get_contents($lnk);
    $dat[$name] = $str;
}

// SHOW THE DATA KEYS AND VALUES
var_dump($dat);

// INTERPRET THE DATA INTO SOMETHING INTERESTING
foreach ($dat as $key => $str)
{
    // REMOVE SPURIOUS END-OF-LINE CHARACTERS
    $str = trim($str);

    // BREAK THE STRING APART INTO AN ARRAY ON THE COMMAS
    $arr = explode(',', $str);

    // POSITION ZERO OF THE ARRAY IS THE NAME OF THE SECURITY
    $nom = trim($arr[0], '"');

    // POSITION ONE IS THE CURRENT VALUE
    $val = number_format($arr[1], 2);

    // POSITION TWO IS THE DELTA
    $chg = $arr[2];

    // FORMAT THE DELTA FOR DISPLAY IN COLOR
    $color = 'black';
    $dir = ' UNCHANGED ';
    if (is_numeric($chg))
    {
        if ($chg > 0)
        {
            $dir = ' UP ';
            $color = 'green';
        }
        if ($chg < 0)
        {
            $dir = ' DOWN ';
            $color = 'red';
        }
    }
    $chg_display
    = '<span style="color:'
    . $color
    . ';">'
    . $chg
    . '</span>'
    ;

    // SHOW THE INFORMATION WE RECOVERED
    echo PHP_EOL
    . 'TRADING SYMBOL '
    . $key
    . ' REFERS TO THE '
    . $nom
    . ' WHICH IS AT '
    . $val
    . $dir
    . $chg_display
    ;
}

Open in new window

Author

Commented:
Ray, thanks very much for that.  I will have a study and see if I can mold to my needs.  Do you have a paypal address so that I can buy you a coffee?
Most Valuable Expert 2011
Top Expert 2016

Commented:
You can find my email address in my profile here at EE, however there is no payment solicited or expected.  We're all volunteers in community here.
http://www.experts-exchange.com/M_3774417.html

All the best, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial