public function send($to, $title, $content, $taskId) { $mail = common\loadClass::getPhpMail(); $mail->isSMTP(); $mail->Host = ZConfig::getField('mail', 'smtp_host'); $mail->Port = ZConfig::getField('mail', 'smtp_port', 25); $mail->SMTPAuth = true; $mail->Username = ZConfig::getField('mail', 'username'); $mail->Password = ZConfig::getField('mail', 'password'); $mail->setFrom(ZConfig::getField('mail', 'from', $mail->Username), ZConfig::getField('mail', 'sendname', 'zmail_server')); $mail->addAddress($to); $mail->Subject = $title; $mail->Body = $content; if (!$mail->send()) { common\Log::info([$taskId, $to, $title, $content, $mail->ErrorInfo], 'error'); return false; } common\Log::info([$taskId, $to, $title, $content, $mail->ErrorInfo], 'success'); return true; }
/** * @return mixed * @throws \Exception * @desc fatal error处理 */ public static function fatalHandler() { $error = \error_get_last(); if (empty($error)) { return; } if (!in_array($error['type'], array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR))) { return; } $config = ZConfig::get('project'); $model = ZFormater::fatal($error); $info['data'] = null; if ($config['debug_mode']) { $info['debug'] = $model; } Log::info([\var_export($model, true)], 'fatal'); $info['msg'] = $model['message']; $info['code'] = $model['code']; Response::status('200'); if ('Php' == Request::getViewMode()) { if ($config['debug_mode']) { Request::setTplFile('public/exception.php'); } else { Request::setTplFile('public/error.php'); } } Response::display($info); }
/** * 并发请求:开始执行 * @return array */ public function exec() { if (!$this->_curlMultiEnabled) { throw new Exception('CurlMultiNotEnabled'); } $this->_curlMultiEnabled = false; $this->_curlMultiLastError = array(); $results = array(); // 空请求 if (empty($this->_curlMultiList)) { return $results; } $mh = curl_multi_init(); $chList = array(); $infoList = array(); $count = count($this->_curlMultiList); foreach ($this->_curlMultiList as $args) { $options = $args['options']; $jsonDecode = $args['jsonDecode']; $url = $options[CURLOPT_URL]; // 判断 http method $method = 'GET'; if (isset($options[CURLOPT_CUSTOMREQUEST])) { $method = $options[CURLOPT_CUSTOMREQUEST]; } elseif (isset($options[CURLOPT_POSTFIELDS])) { $method = 'POST'; } $infoList[] = compact('jsonDecode', 'url', 'method'); $ch = curl_init(); curl_setopt_array($ch, $options); $chList[] = $ch; curl_multi_add_handle($mh, $ch); } $active = null; $start = microtime(true); // 执行批处理句柄 do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active && $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } $totalCost = round((microtime(true) - $start) * 1000); $totalLength = 0; foreach ($chList as $i => $ch) { $result = curl_multi_getcontent($ch); if ($result === null) { $result = false; } // $url, $method, $jsonDecode extract($infoList[$i]); $cost = round(curl_getinfo($ch, CURLINFO_TOTAL_TIME) * 1000); $length = $result === false ? -1 : strlen($result); $totalLength += $length; Log::notice('curl', "{$method} \"{$url}\" Cost: {$cost}ms, Length: {$length}"); $failed = false; $message = ''; $code = 0; $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($result !== false) { // HTTP响应状态码为 `2xx`, 才认为成功 if ($httpStatus >= 200 && $httpStatus < 300) { // 默认会进行 json_decode() if ($jsonDecode) { $message = $result; $result = json_decode($result, true); // JSON 解析出错 if ($result === null && strtolower($message) !== 'null') { $code = 1000; $failed = true; } } } else { $message = $result; $result = null; $failed = true; } } else { // 请求出错 $code = curl_errno($ch); $message = curl_error($ch); $failed = true; } if ($failed) { $this->_curlMultiLastError[] = new CurlError($message, $code, $httpStatus, $options); Log::error('curl', "{$method} \"{$url}\" {$code} [{$httpStatus}] {$message}"); Log::error('curl.options', var_export($options, true)); } else { $this->_curlMultiLastError[] = null; } $results[] = $result; curl_multi_remove_handle($mh, $ch); curl_close($ch); } curl_multi_close($mh); Log::notice('curl', "Multi Request * {$count}, Total Cost: {$totalCost}ms, Total Length: {$totalLength}"); return $results; }