问题
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