简体   繁体   English

如何使此php页面加载速度更快

[英]How to make this php page load faster

I am working on a php page that basically connects to Harvest using the Harvest API and fetches some Information. 我正在一个PHP页面上,该页面基本上使用Harvest API连接到Harvest,并获取一些信息。 The code basically returns all the user objects in Harvest and calculates the expenses for each user. 该代码基本上返回Harvest中的所有用户对象,并计算每个用户的费用。 It takes around 50 seconds to load. 加载大约需要50秒。 Please see my code below- 请在下面查看我的代码-

<?php require_once( 'connection.php' );
  $result= $api->getUsers();
  $users=$result->data;



  $range= Harvest_Range::lastMonth();


  foreach($users as $key=>$value){

 $user_id=$value->get("id");
 $first_name=$value->get("first-name");
 $last_name=$value->get('last-name'); 

  $result_expenses=$api->getUserExpenses($user_id, $range);
 $expenses_data=$result_expenses->data;

 $total_cost=0;

foreach($expenses_data as $key=>$value){
if($value->get("is-locked")=="true"){
$total_cost=$total_cost+$value->get("total-cost");

}} 
 ?>
 <?php if($total_cost!=0){?>
 <tbody>
 <tr>
 <td> <?php echo $first_name; echo " ".$last_name; ?> </td>
 <td> $ <?php echo $total_cost; ?> </td>
 </tr>
 </tbody>
 <?php }}?>

 </table>

Is there any way to make the page load faster? 有什么方法可以使页面加载更快?

Your page is slow because you are requesting data for each user, one at a time. 您的页面速度很慢,因为您一次请求每个用户的数据。 This means that as you get more users you are going to have to make more requests. 这意味着随着您获得更多用户,您将不得不发出更多请求。 More requests means your application will take longer and longer. 更多请求意味着您的应用程序将花费越来越长的时间。

Unfortunately it appears you are only able to request data from one user at a time: http://mdbitz.com/docs/harvest-api/ 不幸的是,您似乎一次只能向一个用户请求数据: http : //mdbitz.com/docs/harvest-api/

If you are able to store the data locally you could greatly increase the speed of your application. 如果您能够在本地存储数据,则可以大大提高应用程序的速度。 You can store it anywhere that is easily accessible such as a database, cache or even a text file. 您可以将其存储在易于访问的任何位置,例如数据库,缓存甚至是文本文件。 Then you will need to determine how often you update your local copy from the Harvest API to get the latest data. 然后,您需要确定从Harvest API更新本地副本以获取最新数据的频率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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