简化一段php代码,重复太多

发布于 2024-12-07 10:02:53 字数 4276 浏览 0 评论 0原文

无法真正弄清楚如何简化它。我想要的是获取这几页的内容并将它们显示在我的网站上。正确的方法是为我想要的每个页面重复 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'];

Cant really figure out how to simplify this. What i want is to get contents of these few pages and display them on my site. Is the right way just to repeat the file_get_contents for every page i want or can i do this in another way (maybe all the web sites in one file_get_contents function ?). Also, the rest of the code just repeats its self. I this the whole code just slows down my web site a lot. If somebody is willing to lend a hand :) in this, thank you so much.

<?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'];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧人九事 2024-12-14 10:02:53

只需使用一个数组和一个 for 每个循环。

 $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 />"; 

 }

希望这有帮助,
菲尔

Just use an array and a for each loop.

 $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 />"; 

 }

Hope this helps,
Phil

怕倦 2024-12-14 10:02:53

一些想法:

1) 如果主要问题是它会减慢您的网站速度,请使用 Memcached 或类似工具缓存该值,这样您就不必为不更改的信息每秒触发 5000 个请求。

2)您能否让每个用户的浏览器使用 javascript 异步加载此内容,以便每个块在可用时加载,从而允许立即加载导航等?

3) 如果你确实需要对从 google 的 api 返回的数据进行一些数据处理,你可以有一个单独的 php 页面,就像用户的浏览器使用 javascript 异步点击的 handle_google_data.php 文件。

4)您可能会在代码中抛出一些 microtime(true) 语句,以查看什么花费了最长的时间(我相当肯定这将是 api 调用)。

祝你好运!
-Z。

A few thoughts:

1) IF the main problem is it slowing down your site, cache the value using Memcached or similar, so you don't have to fire 5000 requests/sec for information that doesn't change.

2) Could you have each user's browser load this asynchronously with javascript so each block loads as it becomes available and therefore allows the navigation, etc. to load immediately?

3) If you really need to do some data massaging of this data coming back from google's api, you could have a separate php page that's like a handle_google_data.php file that your users' browser hits asynchronously with javascript.

4) You might throw some microtime(true) statements throughout your code to see what's taking the longest (I'm fairly positive it will be the api calls).

Good luck!
-Z.

眸中客 2024-12-14 10:02:53

尝试这个并修改 $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;

Try this one and modify the $json_var to get the output that you need.
$file_arr is containing the file paths that you need to call.

$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;
且行且努力 2024-12-14 10:02:53

您可以将链接放入这样的数组中,然后对它们运行 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 />"; 
}

You can put your links in an array like this then run a foreach through them.

$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 />"; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文