很久没有搞OpenCart了,昨天在群里面看到一个人反应,OpenCart 后台(Dashboard)首页打开很慢,大约9秒,其他任何页面都很快。不知道什么原因,于是我帮他解决了这个问题,这里随便记录一下问题解决的经过和思路。
首先我让它开启mysql的慢查询记录,因为后台首页有一个图表统计,以及订单和用户的等统计数据。可是结果他说并没有查询超过2秒的记录。看来不是mysql查询的问题。
然后我又让他用firebug看一下,哪个http请求的时间线最长,本来以为有外部请求的原因,结果发现就是route=common/home&token= 返回的一个ajax超时状态。于是我就去查看了admin/controller/common/home.php 的源码,也没有发现有什么外部的请求。
于是我就一个个查看ControllerCommonHome 中index方法中所有的$this->load->model,后来发现下面这段代码:
if ($this->config->get('config_currency_auto')) {
$this->load->model('localisation/currency');
$this->model_localisation_currency->updateCurrencies();
}
去admin/model/localisation/current.php看了一下源代码,果然里面有一段curl请求外部链接的代码:
public function updateCurrencies($force = false) {
if (extension_loaded('curl')) {
$data = array();
if ($force) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "'");
} else {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "' AND date_modified < '" . $this->db->escape(date('Y-m-d H:i:s', strtotime('-1 day'))) . "'");
}
foreach ($query->rows as $result) {
$data[] = $this->config->get('config_currency') . $result['code'] . '=X';
}
$curl = curl_init();
//就是这里了,看到木有
curl_setopt($curl, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=' . implode(',', $data) . '&f=sl1&e=.csv');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($curl);
curl_close($curl);
$lines = explode("\n", trim($content));
foreach ($lines as $line) {
$currency = utf8_substr($line, 4, 3);
$value = utf8_substr($line, 11, 6);
if ((float)$value) {
$this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . (float)$value . "', date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "' WHERE code = '" . $this->db->escape($currency) . "'");
}
}
$this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '1.00000', date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "' WHERE code = '" . $this->db->escape($this->config->get('config_currency')) . "'");
$this->cache->delete('currency');
}
}
看到了吧,它用curl抓取了yahoo的关于汇率的数据,结合这2段代码可以看出,其实它的功能就是打开这个页面的时候自动更新数据库中汇率数据。
到此问题找到原因了,那么解决的方法总结一下:
- 服务器PHP是否支持CURL扩展
- 服务器空间是否允许访问外部链接
- 可以关闭自动更新汇率(Dashboard>System>Setting>Your Store>edit>local>Auto Update Currency 选择No)