/** * api返回数据 * * @param type $script_name * @param type $params * @param type $method * @param type $cookie * @param type $protocol * @return type */ public function api($script_name, $params, $method = 'get', $cookie = array(), $protocol = 'http', $timeout = 30) { $query_string = $this->getQueryString($params); $cookie_string = $this->getCookieString($cookie); if (strcmp($protocol . "://", substr($script_name, 0, strlen($protocol . "://"))) !== 0) { $url = $protocol . "://" . $this->getServerName() . $script_name; } else { $url = $script_name; } $ch = curl_init(); if ('GET' == strtoupper($method)) { curl_setopt($ch, CURLOPT_URL, "{$url}?{$query_string}"); } else { curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string); } curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // disable 100-continue curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); if (!empty($cookie_string)) { curl_setopt($ch, CURLOPT_COOKIE, $cookie_string); } if ('https' == $protocol) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } $rs = curl_exec($ch); $err = curl_error($ch); if (false === $rs || !empty($err)) { $errno = curl_errno($ch); $info = curl_getinfo($ch); curl_close($ch); $message = array('result' => false, 'errno' => $errno, 'msg' => $err, 'info' => $info); $stact_trace = Star_Debug::Trace(); //返回堆栈详细信息 $stact_trace = implode("\n", $stact_trace); Star_Log::log($url . "?" . $query_string . "\n" . $err . "\n" . $stact_trace, 'query_error'); return; } curl_close($ch); return json_decode($rs, true); }
/** * sql query * @param $sql */ public function _query($sql, $params = array()) { if ($this->slow_query_log == true) { $start_time = time(); } $stmt = $this->db->prepare($sql); $stmt->execute($params); if ($stmt->errorCode() !== '00000') { $error_info = $stmt->errorInfo(); $sql = $this->getCompeleSql($sql, $params); throw new Star_Exception("SQL: " . $sql . " \nError Message:" . $error_info[2], 500); } if ($this->slow_query_log == true) { $end_time = time(); $time = $end_time - $start_time; if ($time >= $this->slow_query_time) { $stact_trace = Star_Debug::Trace(); //返回堆栈详细信息 $stact_trace = implode("\n", $stact_trace); $sql = $this->getCompeleSql($sql, $params); Star_Log::log("Query_time: {$time}s Slow query: {$sql} \nStack trace:\n{$stact_trace}", 'slow_query'); //记录慢查询日志 } } return $stmt; }
/** * sql query * @param $sql * @return $resource */ public function _query($sql) { if ($this->slow_query_log == true) { $start_time = time(); } $resource = $this->db->query($sql); if ($resource === false) { throw new Star_Exception("SQL: " . $sql . " \nError Message:" . $this->db->error, 500); } if ($this->slow_query_log == true) { $end_time = time(); $time = $end_time - $start_time; if ($time >= $this->slow_query_time) { $stact_trace = Star_Debug::Trace(); //返回堆栈详细信息 $stact_trace = implode("\n", $stact_trace); Star_Log::log("Query_time: {$time}s Slow query: {$sql} \nStack trace:\n{$stact_trace}", 'slow_query'); //记录慢查询日志 } } return $resource; }