从另一个网站获取数据会导致网站速度慢吗?

时间:2017-05-12 作者:Paulo

我正在使用以下代码从另一个网站获取数据:

    <?php

  // IOCS logos
  $logos_url = \'https://www.someurl\';

  $logos_request = wp_remote_get( $logos_url );
  if(is_wp_error($logos_request)) {
    return false;
  }

  $logos_body = wp_remote_retrieve_body( $logos_request );

  $logos_data = json_decode( $logos_body );

  // OSCS logos
  $oscs_logos_url = \'https://www.someurl\';

  $oscs_logos_request = wp_remote_get( $oscs_logos_url );
  if(is_wp_error($oscs_logos_request)) {
    return false;
  }

  $oscs_logos_body = wp_remote_retrieve_body( $oscs_logos_request );

  $oscs_logos_data = json_decode( $oscs_logos_body );

 ?>

<?php if(!empty($logos_data)) : ?>
   <div class="tab-content">
      <div id="iocs-tab" class="tab-pane fade in active">
        <div class="home-carousel">
          <div class="container-fluid">
            <div class="col-sm-12 ioscs-slider">
                <div class="iocs">
                  <?php foreach ($logos_data as $value) : ?>
                    <div class="slide">
                      <img src="<?php echo esc_url( $value->Url ); ?>" alt="exxonMobil" class="img-responsive">
                    </div>
                  <?php endforeach; ?>
                </div>
              </div>
          </div>
      </div>
    </div>
    <?php endif; ?>

    <?php if(!empty($oscs_logos_data)) : ?>
      <div id="oscs-tab" class="home-carousel tab-pane fade">
        <div class="home-carousel">
          <div class="container-fluid">
            <div class="col-sm-12 oscs-slider">
                <div class="iocs">
                  <?php foreach ($oscs_logos_data as $oscs_value) : ?>
                    <div class="slide">
                      <img src="<?php echo esc_url( $oscs_value->Url ); ?>" alt="exxonMobil" class="img-responsive">
                    </div>
                  <?php endforeach; ?>
                </div>
              </div>
          </div>
      </div>
    </div>
  </div>
  <?php endif; ?>
然而,由于我已经实现了这一点,网站加载速度非常慢。提高绩效的最佳方式是什么?

EDIT

<?php

$trans_id = \'portal_logos\';
$external_logos = get_transient( $trans_id );

if ( false === $external_logos ) {
  ob_start();

  // IOCS logos
  $logos_url = \'my_url\'; //Returns array with logos

  $logos_request = wp_remote_get( $logos_url );
  if(is_wp_error($logos_request)) {
    return false;
  }

  $logos_body = wp_remote_retrieve_body( $logos_request );

  $logos_data = json_decode( $logos_body);

  // OSCS logos
  $oscs_logos_url = \'my_url\'; //Returns array with logos

  $oscs_logos_request = wp_remote_get( $oscs_logos_url );
  if(is_wp_error($oscs_logos_request)) {
    return false;
  }

  $oscs_logos_body = wp_remote_retrieve_body( $oscs_logos_request );

  $oscs_logos_data = json_decode( $oscs_logos_body);

 ?>
 <div class="logo-scroller">
  <div class="container">
      <ul class="nav nav-tabs">
         <li class="active"><a data-toggle="tab" href="#iocs-tab"><?php esc_html_e( \'IOCs operating in the Kurdistan Region\', \'mnr\' ); ?></a></li>
         <li><a data-toggle="tab" href="#oscs-tab"><?php esc_html_e( \'OSCs operating in the Kurdistan Region\', \'mnr\' ); ?></a></li>
       </ul>
  </div>

 <?php if(!empty($logos_data)) : ?>
   <div class="tab-content">
      <div id="iocs-tab" class="tab-pane fade in active">
        <div class="home-carousel">
          <div class="container-fluid">
            <div class="col-sm-12 ioscs-slider">
                <div class="iocs">
                  <?php foreach ($logos_data as $value) : ?>
                    <div class="slide">
                      <img src="<?php echo esc_url( $value->Url ); ?>" alt="exxonMobil" class="img-responsive">
                    </div>
                  <?php endforeach; ?>
                </div>
              </div>
          </div>
      </div>
    </div>
    <?php endif; ?>

    <?php if(!empty($oscs_logos_data)) : ?>
      <div id="oscs-tab" class="home-carousel tab-pane fade">
        <div class="home-carousel">
          <div class="container-fluid">
            <div class="col-sm-12 oscs-slider">
                <div class="iocs">
                  <?php foreach ($oscs_logos_data as $oscs_value) : ?>
                    <div class="slide">
                      <img src="<?php echo esc_url( $oscs_value->Url ); ?>" alt="exxonMobil" class="img-responsive">
                    </div>
                  <?php endforeach; ?>
                </div>
              </div>
          </div>
      </div>
    </div>
  </div>
  <?php endif; ?>
  </div>

  <?php
    $external_logos = ob_get_clean();
    set_transient( $trans_id, $external_logos, DAY_IN_SECONDS );
  }

  echo $external_logos;

  ?>

1 个回复
SO网友:Z. Zlatev

基本上,您得到了一段阻止PHP执行的代码,因为它依赖于外部请求wp_remote_get() 完成以继续。最糟糕的是,您可以无条件地对每个请求执行此操作:)目前最简单的解决方案可能是将基于该请求生成的所有HTML存储到transient. 查看以下内容:

<?php

$trans_id = \'my_external_logos__name_me_good\';
$external_logos = get_transient( $trans_id );

if ( false === $external_logos ) {
  ob_start();

  ... what you did before ...

  $external_logos = ob_get_clean();
  set_transient( $trans_id, $external_logos, WEEK_IN_SECONDS );
}

echo $external_logos;
您可以将WEEK\\u IN\\u SECONDS调整为以秒为单位的任何时段(例如,DAY\\u IN\\u SECONDS常量)。现在,您的代码每周只执行一次,WP将从其数据库中提供生成的HTML。

结束

相关推荐