简体   繁体   中英

Simplify a piece of php code, too many repetitions

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

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

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.

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM