PHP - 如何优化此脚本以使其执行得更快?已经需要20秒才能加载

时间:2014-03-17 16:23:17

标签: javascript php ajax zend-framework

这是一个非常简单的脚本,为一些使用Ajax / Javascript / jQuery的Affiliate网站提供一个按钮,通过以下代码从我的服务器获取所需的插件。但它占用了大部分网站+ / _ 20秒,其他插件在30ms内被加载。

我无法理解导致这种缓慢加载的部分是什么?我在这做错了什么?你怎么能改进它?

<?php
header('Access-Control-Allow-Origin: *');  
$customerid = $_GET['id'];
$what = $_GET['button'];
$id = substr('3' . rand(0,9999), 0-$length);

switch ($customerid) {
  case 'customer1':    
    break;
  case 'customer2':
    break;  
  default:
    $da = "https://server0/1.php?r={$id}&endof=end";
    $db = "https://server1?r={$id}";    
    $imagelink = "http://server2.com:8080/images/button.png";

    $a = "<a href={$da} id={$} class={$customerid}abutton onclick=\"typeof(_gaq)=='undefined'?'':_gaq.push(['_trackEvent', 'Mycompany', 'ButtonClick']);typeof(_gat)=='undefined'?'':_gat._getTrackerByName()._setAllowLinker(true); window.open(typeof(_gat)=='undefined'?this.href+'?referrer='+escape(window.location.href):_gat._getTrackerByName()._getLinkerUrl(this.href+'?referrer='+escape(window.location.href)), '_blank', 'width=480,height=360,resizable=no,toolbar=no,menubar=no,location=no,status=no'); return false\"><img src=\"{$imagelink}\" /></a>";

    $b = "<a href={$db} id={$} class={$customerid}aabutton onclick=\"typeof(_gaq)=='undefined'?'':_gaq.push(['_trackEvent', 'Mycompany', 'ButtonClick']);typeof(_gat)=='undefined'?'':_gat._getTrackerByName()._setAllowLinker(true); window.open(typeof(_gat)=='undefined'?this.href+'?referrer='+escape(window.location.href):_gat._getTrackerByName()._getLinkerUrl(this.href+'?referrer='+escape(window.location.href)), '_blank', 'width=480,height=360,resizable=no,toolbar=no,menubar=no,location=no,status=no'); return false\"><img src=\"{$imagelink}\" /></a>";

    $section = file_get_contents("http://server2/ajax/getstatus");
    $section = json_decode($section);
    if ($section->result){
      switch($what) {
        case 'v':
          echo $b;
          break;
        case 'a':
          echo $a;
          break;
        case 'b':
          echo $a . $b;
          break;
      }

    } else {
      switch($what) {
        case 'v':
          break;
        case 'a':
          echo $a;
          break;
        case 'b':
          echo $a;
          break;
      }
    }   
    break;
}

?>

获取状态:持续时间最长,我该怎么做才能优化它?花了将近10秒钟。

  public function getstatusAction() {
    $this->_response->setHeader('Access-Control-Allow-Origin', '*');       
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender();   
    $post = $this->getRequest()->getQuery();
    $data = (object) $post;
    $this->db = Application_Model_Db::db_load();

    $sql = "select *from sh_av_users where
              status='online'
        limit 1";
    $result = $this->db->fetchAll($sql);
    if (count($result) > 0) {
      $json = array(
          'result' =>true,
      );      
    }   else {
      $json = array(
          'result' =>false,
      );        
    } 

    $content = Zend_Json::encode($json);
    $this->getResponse()
            ->setHeader('Content-Type', 'application/json')
            ->setBody($content)
            ->sendResponse();

    exit;
  }

0 个答案:

没有答案