使用PHP,JavaScript解析另一台服务器上的CSV [英] Parse CSV located on another server with PHP, JavaScript

查看:147
本文介绍了使用PHP,JavaScript解析另一台服务器上的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要解析另一个服务器(Yahoo Finance)上的动态生成的CSV文件,但是根据我读过的内容,您无法使用XMLHTTPRequest访问另一个服务器上的内容,至少不能使用CSV格式。 p>

我想到了这个问题的解决方案,我想知道这是正确的方式还是最佳实践,因为我是相对新的发展。这是我使用PHP的想法:

 <?php 
$ symbol = $ _GET [ s];

$ path =http://download.finance.yahoo.com/d/quotes.csv?s=。 $符号。 & f = sl1d1t1c1ohgv& e = .csv;
$ yahooquote = fopen($ path,r);

while(!feof($ yahooquote))
{
$ line = fgets($ yahooquote,1000);
echo $ line。 < br />;
}

fclose($ yahooquote);
?>

这会返回一个包含CSV文件行的html文件,然后可以使用JavaScript / jQuery XMLHTTPRequest在客户端解析它。



这是最好的方法吗?你有什么想法/建议吗?请注意,这必须快速,因为它必须每10秒运行一次才能更新+100股票报价。提前感谢。

解决方案

查看教程。他们使用jQuery请求通过Ajax的,这是你在寻找什么雅虎股票报价(CSV格式)。


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, at least not in CSV format.

I've thought of a solution to this problem and I would like to know if this is the "right" way or best practice to do this, as I am relatively new to development. This is what I've come up with using PHP:

<?php
    $symbol = $_GET["s"];

    $path = "http://download.finance.yahoo.com/d/quotes.csv?s=" . $symbol . "&f=sl1d1t1c1ohgv&e=.csv";
    $yahooquote = fopen($path, "r");

    while(!feof($yahooquote))
    {
        $line = fgets($yahooquote, 1000);
        echo $line . "<br />";
    }

    fclose($yahooquote);
?>

This returns an html file which has the lines of the CSV file and then I can use JavaScript/jQuery XMLHTTPRequest to parse it on the client side.

Is this the best way to do this? do you have any ideas/suggestions? Please keep in mind that this has to be fast as it has to run every 10 seconds to update +100 stock quotes. Thanks in advance.

解决方案

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.

这篇关于使用PHP,JavaScript解析另一台服务器上的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆