Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Thanked 0 Times in 0 Posts

    fetching data to local cache with javascript

    I found this code to fetch stock quotes and save them as a csv in a local cache, however no matter what i do i cannot get it to actually retrieve and save the data. Any suggestions as to where its failing would be greatly appreciated.

    // Original script by Walter Heitman Jr, first published on http://techblog.shanock.com

    // List your stocks here, separated by commas, no spaces, in the order you want them displayed:
    $stocks = "ANZ.AX,BHP.AX,NAB.AX";

    // Function to copy a stock quote CSV from Yahoo to the local cache. CSV contains symbol, price, and change
    function upsfile($stock) { copy("http://download.finance.yahoo.com/d/quotes.csv?s=$stock&f=sl1d1t1c1ohgv&e=.csv","../stockcache/".$stock.".csv"); }

    foreach ( explode(",", $stocks) as $stock ) {

    // Where the stock quote info file should be...
    $local_file = "/stockcache/".$stock.".csv";

    // ...if it exists. If not, download it.
    if (!file_exists($local_file)) { upsfile($stock); }
    // Else,If it's out-of-date by 15 mins (900 seconds) or more, update it.
    elseif (filemtime($local_file) <= (time() - 900)) { upsfile($stock); }

    // Open the file, load our values into an array...
    $local_file = fopen ("/stockcache/".$stock.".csv","r");
    $stock_info = fgetcsv ($local_file, 1000, ",");

    // ...format, and output them. I made the symbols into links to Yahoo's stock pages.
    echo "<span class=\"stockbox\"><a href=\"http://finance.yahoo.com/q?s=".$stock_info[0]."\">".$stock_info[0]."</a> ".sprintf("%.2f",$stock_info[1])." <span style=\"";
    // Green prices for up, red for down
    if ($stock_info[2]>=0) { echo "color: #009900;\">&uarr;"; }
    elseif ($stock_info[2]<0) { echo "color: #ff0000;\">&darr;"; }
    echo sprintf("%.2f",abs($stock_info[2]))."</span></span>\n";
    // Done!

  2. #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,947 Times in 4,908 Posts
    Wrong forum. There is not ONE SPECK of JavaScript code in this.

    Will some moderator please move this to the PHP forum??

    And small.j: You do know this code will only work if (a) saved as a ".php" file (NOT ".html"!!!) and (b) you have PHP enabled on your web server?
    Be yourself. No one else is as qualified.


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts