无法真正弄清楚如何简化这一点。我想要的是获取这几页的内容并在我的网站上显示。是正确的方法只是为我想要的每个页面重复file_get_contents,或者我可以用另一种方式(也许是一个file_get_contents函数中的所有网站?)。此外,其余的代码只是重复自我。我这整个代码只是减慢了我的网站。如果有人愿意伸出援助之手:),非常感谢你。
<?php
//Obtain Quote Info - This collects the Microsoft Stock Info
$quote10 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=NYSE:GLD');
$quote9 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXEURO:PX1');
$quote8 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXDJSTOXX:SX5E');
$quote7 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXFTSE:UKX');
$quote4 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXNIKKEI:NI225');
$quote5 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=SHA:000001');
$quote4 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXDJX:.DJI');
$quote3 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXNASDAQ:.IXIC');
$quote2 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXSP:.INX');
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG');
$quote1 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=NASDAQ:YHOO');
//Remove CR's from ouput - make it one line
$json = str_replace("\n", "", $quote);
$json1 = str_replace("\n", "", $quote1);
$json2 = str_replace("\n", "", $quote2);
$json3 = str_replace("\n", "", $quote3);
$json4 = str_replace("\n", "", $quote4);
$json5 = str_replace("\n", "", $quote5);
$json6 = str_replace("\n", "", $quote6);
$json7 = str_replace("\n", "", $quote7);
$json8 = str_replace("\n", "", $quote8);
$json9 = str_replace("\n", "", $quote9);
$json10 = str_replace("\n", "", $quote10);
//echo $json['45'];
//Remove //, [ and ] to build qualified string
$data = substr($json, 4, strlen($json) -5);
$data1 = substr($json1, 4, strlen($json1) -5);
$data2 = substr($json2, 4, strlen($json2) -5);
$data3 = substr($json3, 4, strlen($json3) -5);
$data4 = substr($json4, 4, strlen($json4) -5);
$data5 = substr($json5, 4, strlen($json5) -5);
$data6 = substr($json6, 4, strlen($json6) -5);
$data7 = substr($json7, 4, strlen($json7) -5);
$data8 = substr($json8, 4, strlen($json8) -5);
$data9 = substr($json9, 4, strlen($json9) -5);
$data10 = substr($json10, 4, strlen($json10) -5);
//decode JSON data
$json_output = json_decode($data, true);
$json_output1 = json_decode($data1, true);
$json_output2 = json_decode($data2, true);
$json_output3 = json_decode($data3, true);
$json_output4 = json_decode($data4, true);
$json_output5 = json_decode($data5, true);
$json_output6 = json_decode($data6, true);
$json_output7 = json_decode($data7, true);
$json_output8 = json_decode($data8, true);
$json_output9 = json_decode($data9, true);
$json_output10 = json_decode($data10, true);
//Un-remark print_r to see all array keys
//print_r($json_output);
//Output Stock price array key.
echo "\n".$json_output['t'],"\n".$json_output['l'],"\n".$json_output['cp'];echo "<br />";
echo "\n".$json_output1['t'],"\n".$json_output1['l'],"\n".$json_output1['cp'];echo "<br />";
echo "\n".$json_output2['t'],"\n".$json_output2['l'],"\n".$json_output2['cp'];echo "<br />";
echo "\n".$json_output3['t'],"\n".$json_output3['l'],"\n".$json_output3['cp'];echo "<br />";
echo "\n".$json_output4['t'],"\n".$json_output4['l'],"\n".$json_output4['cp'];echo "<br />";
echo "\n".$json_output5['t'],"\n".$json_output5['l'],"\n".$json_output5['cp'];echo "<br />";
echo "\n".$json_output6['t'],"\n".$json_output6['l'],"\n".$json_output6['cp'];echo "<br />";
echo "\n".$json_output7['t'],"\n".$json_output7['l'],"\n".$json_output7['cp'];echo "<br />";
echo "\n".$json_output8['t'],"\n".$json_output8['l'],"\n".$json_output8['cp'];echo "<br />";
echo "\n".$json_output9['t'],"\n".$json_output9['l'],"\n".$json_output9['cp'];echo "<br />";
echo "\n".$json_output10['t'],"\n".$json_output10['l'],"\n".$json_output10['cp'];
答案 0 :(得分:5)
只需为每个循环使用一个数组和一个。
$stock_array = array('NYSE:GLD', 'INDEXEURO:PX1', 'INDEXDJSTOXX:SX5E', etc..);
foreach ($stock_array as $stock) {
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q='.$stock);
$json = str_replace("\n", "", $quote);
$data = substr($json, 4, strlen($json) -5);
$json_output = json_decode($data, true);
echo "\n".$json_output['t'],"\n".$json_output['l'],"\n".$json_output['cp'];echo "<br />";
}
希望这有帮助, 菲尔
答案 1 :(得分:2)
一些想法:
1)如果主要问题是它减慢了您的网站速度,请使用Memcached或类似方法缓存该值,因此您不必每秒触发5000个请求以获取不会更改的信息。
2)你是否可以让每个用户的浏览器与javascript异步加载,以便每个块在可用时加载,从而允许导航等立即加载?
3)如果你真的需要对来自google api的这些数据进行一些数据按摩,你可以拥有一个单独的php页面,就像一个handle_google_data.php文件,用户的浏览器与javascript异步点击。
4)你可能会在整个代码中抛出一些microtime(true)语句,看看花费的时间最长(我很肯定会是api调用)。
祝你好运! -Z。答案 2 :(得分:1)
尝试这个并修改$json_var
以获得所需的输出。
$file_arr
包含您需要调用的文件路径。
$output_var = '';
function my_function($file_path)
{
$quote = file_get_contents($file_path);
$json = str_replace("\n", "", $quote);
$data = substr($json, 4, strlen($json) -5);
$json_output = json_decode($data, true);
$json_var = "\n".$json_output['t'].",\n".$json_output['l'].",\n".$json_output['cp'].'<br />';
return $json_var;
}
$file_arr = array('0'=>'http://finance.google.com/finance/info?client=ig&q=NYSE:GLD','1'=>'http://finance.google.com/finance/info?client=ig&q=INDEXEURO:PX1');
foreach($file_arr as $k=>$v):
$output_var .= my_function($v);
endforeach;
echo $output_var;
答案 3 :(得分:0)
您可以将链接放在这样的数组中,然后通过它们运行foreach。
$quotelinks = array(
'http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG',
'http://finance.google.com/finance/info?client=ig&q=NASDAQ:YHOO',
'http://finance.google.com/finance/info?client=ig&q=INDEXSP:.INX',
'http://finance.google.com/finance/info?client=ig&q=INDEXNASDAQ:.IXIC',
'http://finance.google.com/finance/info?client=ig&q=INDEXDJX:.DJI',
'http://finance.google.com/finance/info?client=ig&q=SHA:000001',
'http://finance.google.com/finance/info?client=ig&q=INDEXNIKKEI:NI225',
'http://finance.google.com/finance/info?client=ig&q=INDEXFTSE:UKX',
'http://finance.google.com/finance/info?client=ig&q=INDEXDJSTOXX:SX5E',
'http://finance.google.com/finance/info?client=ig&q=INDEXEURO:PX1',
'http://finance.google.com/finance/info?client=ig&q=NYSE:GLD');
foreach ($quotelinks as $quotelink) {
$quote = file_get_contents($quotelink);
$json = str_replace("\n", "", $quote);
$data = substr($json, 4, strlen($json) -5);
$json_output = json_decode($data, true);
echo "\n".$json_output['t'],"\n".$json_output['l'],"\n".$json_output['cp'];echo "<br />";
}