Skip to content Skip to sidebar Skip to footer

Parse Csv Located On Another Server With Php, Javascript

I want to parse a dynamically generated CSV file that is on another server (Yahoo Finance), but from what I've read you can't access content on another server using XMLHTTPRequest,

Solution 1:

The problem with this is that its being run over and over again by multiple users of your site.

If you want speed, and to decrease bandwidth and performance then you should do the following:

Create a script thats executed privately by cron every X Seconds, which fetches the csv data from yahoo and stores it locally on your server.

Once that is out the way then when your users check via ajax you can just feed them the local copy.

all the users will be sharing the same cached file and your only ever running once per ten seconds, it will also be handled by another instance so users will see faster responces, you may also want to read about file locking

Also you may want to use cURL on PHP Side and

Solution 2:

Check out the example on this tutorial. They use jQuery to request a Yahoo stock quote (in CSV format) through Ajax, which is exactly what you're looking for.

Solution 3:

Use cURL it is 5x faster.

functioncurl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    return curl_exec($ch);
    curl_close ($ch);
}

$symbol = $_GET["s"];
$csv = "http://download.finance.yahoo.com/d/quotes.csv?s=" . $symbol . "&f=sl1d1t1c1ohgv&e=.csv";

$yahoo = curl($csv);

Now contents of CSV file will be in $yahoo variable, which you can parse or store it on your server (fopen, fwrite, fclose).

Solution 4:

@VerizonW: This tutorial on reading a remote file using PHP covers four methods (including the one you're presently using), I suggest you give it a read to see which method you feel most comfortable with. I tend to prefer using cURL.

Post a Comment for "Parse Csv Located On Another Server With Php, Javascript"