Yahoo CSV to HTML via PHP — no longer works

99封情书 提交于 2019-12-14 03:33:36

问题


I was using a PHP script to convert Yahoo finance quotes to an HTML web page. But suddenly the web page stopped showing the data after a year of working perfectly and there were no code changes at all. Here is my code:

<table>
<tr>
<?php $fp = fopen ("http://finance.yahoo.com/d/quotes.csv?s=VIP&f=l1c1p2rj1&e=.csv","r");
        $data = fgetcsv ($fp, 1000, ",") ?>
<td>Vimpel-Communications</td>
<td><?php echo $data[0] ?></td>
<td><?php echo $data[1] ?></td>
<td><?php echo $data[2] ?></td>
<td><?php echo $data[3] ?></td>
<td><?php echo $data[4] ?></td>
<td><?php echo $data[5] ?></td>
</tr>
</table>

And here is a test page of the actual site: http://bricadr.com/test.php Can anyone help or does anyone know what happened, or how I can fix this? Additionally, if anyone has a sever, can you see if this code works on your server? Perhaps my hosting company turned off some functionality that allowed this to previously work.

Thank you in advance!

Brian


回答1:


Update: Tested it on my server. When not parsed, commented in the HTML is a notice of a 301 redirect. The new page is "http://download.finance.yahoo.com/d/quotes.csv?s=VIP&f=l1c1p2rj1&e=.csv", simply change your URL. I've updated my below code if you would like to use it.

Anyway, here's a little effient-etized version of your code, using cURL because it is much faster than fopen. I also used explode because for some reason the cvs function was not working on my server.

$curl=curl_init();
curl_setopt ($curl,CURLOPT_URL,"http://download.finance.yahoo.com/d/quotes.csv?s=VIP&f=l1c1p2rj1&e=.csv");
curl_setopt ($curl,CURLOPT_HEADER,0);
ob_start();
curl_exec ($curl);
curl_close ($curl);
$data=ob_get_clean();
$data=explode(",",$data);
$data=str_replace('"','',$data);
foreach ($data as $results)
  echo "<td>$results</td>";

Working here.



来源:https://stackoverflow.com/questions/10646853/yahoo-csv-to-html-via-php-no-longer-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!