Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

Yahoo CSV to HTML via PHP -- no longer works

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...