设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 站长学院 > PHP教程 > 正文

api接口返回为反常是html情况的问题怎么处理

发布时间:2022-06-30 13:41 所属栏目:121 来源:互联网
导读:关于api接口返回为异常是html情况的问题怎么处理的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。 现在谁不开发接口的呢?但是在接
  关于“api接口返回为异常是html情况的问题怎么处理”的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。
 
     现在谁不开发接口的呢?但是在接口开发过程中,报错误异常后居然返回错误的信息依然是html信息!TP官方也不知道为啥不添加,说好的为接口而生,我的解决方案也很简单,把系统的异常处理类复制出来,去掉模板相关,直接以json方式输出
 
  下面是解决方案:
 
  1:按照TP扩展异常的方式引用这个文件
 
  https://www.kancloud.cn/manual/thinkphp5_1/354092
 
  // 判断默认输出类型
  // $app 是配置数组
  if ($app['default_return_type'] == 'json') {
   // 异常处理handle类 留空使用 \think\exception\Handle
   $app['exception_handle'] = '\\app\\common\\exception\\JsonException';
  }
  return $app;
  异常处理类:
 
  <?php
 
   namespace app\common\exception;
 
   use Exception;
   use think\exception\ErrorException;
   use think\exception\Handle;
   use think\exception\HttpException;
   use think\console\Output;
   use think\Container;
   use think\Response;
 
   class JsonException extends Handle
   {
    protected $render;
    protected $ignoreReport = [
     '\\think\\exception\\HttpException',
    ];
 
    public function setRender($render)
    {

     if (!$this->isIgnoreReport($exception)) {
     // 收集异常数据
     if (Container::get('app')->isDebug()) {
      $data = [
       'file' => $exception->getFile(),
       'line' => $exception->getLine(),
       'message' => $this->getMessage($exception),
       'code' => $this->getCode($exception),
      ];
      $log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
     } else {
      $data = [
       'code' => $this->getCode($exception),
       'message' => $this->getMessage($exception),
      ];
      $log = "[{$data['code']}]{$data['message']}";
     }
 
     if (Container::get('app')->config('log.record_trace')) {
      $log .= "\r\n" . $exception->getTraceAsString();
     }
 
     Container::get('log')->record($log, 'error');
     }
    }
 
    protected function isIgnoreReport(Exception $exception)
    {
     foreach ($this->ignoreReport as $class) {
     if ($exception instanceof $class) {
      return true;
     }
     }
 
     return false;
    }
 
    /**
    * Render an exception into an HTTP response.
    *
    * @access public
    * @param \Exception $e
    * @return Response
    */
    public function render(Exception $e)
    {
     if ($this->render && $this->render instanceof \Closure) {
     $result = call_user_func_array($this->render, [$e]);
 
     if ($result) {
      return $result;
     }
     }
 
     if ($e instanceof HttpException) {
     return $this->renderHttpException($e);
     } else {
     return $this->convertExceptionToResponse($e);
     }

    /**
    * @access protected
    * @param HttpException $e
    * @return Response
    */
    protected function renderHttpException(HttpException $e)
    {
     $status = $e->getStatusCode();
     $template = Container::get('app')->config('http_exception_template');
 
     if (!Container::get('app')->isDebug() && !empty($template[$status])) {
     return Response::create($e, 'json', $status);
     } else {
     return $this->convertExceptionToResponse($e);
     }
    }
 
    /**
    * @access protected
    * @param Exception $exception
    * @return Response
    */
    protected function convertExceptionToResponse(Exception $exception)
    {
     // 收集异常数据
     if (Container::get('app')->isDebug()) {
     // 调试模式,获取详细的错误信息
     $data = [
      'name' => get_class($exception),
      'file' => $exception->getFile(),
      'line' => $exception->getLine(),
      'message' => $this->getMessage($exception),
      'trace' => $exception->getTrace(),
      'code' => $this->getCode($exception),
      'source' => $this->getSourceCode($exception),
      'datas' => $this->getExtendData($exception),
      'tables' => [
       'GET Data'    => $_GET,
       'POST Data'    => $_POST,
       'Files'     => $_FILES,
       'Cookies'    => $_COOKIE,
 
     //保留一层
     while (ob_get_level() > 1) {
     ob_end_clean();
     }
 
     $data['echo'] = ob_get_clean();
 
     $response = Response::create($data, 'json');
 
     if ($exception instanceof HttpException) {
     $statusCode = $exception->getStatusCode();
     $response->header($exception->getHeaders());
     }
 
     if (!isset($statusCode)) {
     $statusCode = 500;
     }
     $response->code($statusCode);
 
     return $response;
    }
 
    /**

    * 获取错误信息
    * ErrorException则使用错误级别作为错误编码
    * @access protected
    * @param \Exception $exception
    * @return string    错误信息
    */
    protected function getMessage(Exception $exception)
    {
     $message = $exception->getMessage();
 
     if (PHP_SAPI == 'cli') {
     return $message;
     }
 
     $lang = Container::get('lang');
 
     if (strpos($message, ':')) {
     $name = strstr($message, ':', true);
     $message = $lang->has($name) ? $lang->get($name) . strstr($message, ':') : $message;
     } elseif (strpos($message, ',')) {
     $name = strstr($message, ',', true);
     $message = $lang->has($name) ? $lang->get($name) . ':' . substr(strstr($message, ','), 1) : $message;
     } elseif ($lang->has($message)) {
     $message = $lang->get($message);
     }
 
     return $message;
    }
 
    /**

    * 获取异常扩展信息
    * 用于非调试模式html返回类型显示
    * @access protected
    * @param \Exception $exception
    * @return array     异常类定义的扩展数据
    */
    protected function getExtendData(Exception $exception)
    {
     $data = [];
 
     if ($exception instanceof \think\Exception) {
     $data = $exception->getData();
     }
 
     return $data;
    }
 
    /**
    * 获取常量列表
    * @access private
    * @return array 常量列表
    */
    private static function getConst()
    {
     $const = get_defined_constants(true);
 
     return isset($const['user']) ? $const['user'] : [];
    }
 
   }
 
  以上就是关于“api接口返回为异常是html情况的问题怎么处理”的介绍了,感谢各位的阅读,希望这篇文章能帮助大家解决问题。

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读