?? ?? ???? ?????? ??? ? ??? ?? json ?? jsonp ??? ??? ??? ??? ?? ???? json ??? ??? ?? ??? ??? ??? ???? ???? ?? ?? ??? ???? ???.
1 beforeSend? ???? ?? ????? ???. beforesend? ?? ?? ??? ???? ?????. ??? ??? ?? ?????.
/** * (non-PHPdoc) * @see \yii\base\Object::init() */ public function init() { parent::init(); //綁定beforeSend事件,更改數(shù)據(jù)輸出格式 Yii::$app->getResponse()->on(Response::EVENT_BEFORE_SEND, [$this, 'beforeSend']); }#???? #
2. ?? ?? ?? ?? ???? ???. ?? ???? ??? ?? ?? ??? ????.
1>??? ?? ?? ??2>????? Json ???? ????? 3> ????? ?? ? $_GET['callback'] ????? ???? Jsonp ??? ?????
# ????#4> ??? ??? ?? ???? {"success":true,"data":{...}}
5>?? ??? ??? ?? ???? { "??":false,"???":{"??": "?? ? ??","message":"???? ?? ? ????.","code":0,"status":404}}
#?? ??#6>???? ??? ??? ????:/** * 更改數(shù)據(jù)輸出格式 * 默認(rèn)情況下輸出Json數(shù)據(jù) * 如果客戶端請(qǐng)求時(shí)有傳遞$_GET['callback']參數(shù),輸入Jsonp格式 * 請(qǐng)求正確時(shí)數(shù)據(jù)為 {"success":true,"data":{...}} * 請(qǐng)求錯(cuò)誤時(shí)數(shù)據(jù)為 {"success":false,"data":{"name":"Not Found","message":"頁(yè)面未找到。","code":0,"status":404}} * @param \yii\base\Event $event */ public function beforeSend($event) { /* @var $response \yii\web\Response */ $response = $event->sender; $isSuccessful = $response->isSuccessful; if ($response->statusCode>=400) { //異常處理 if (true && $exception = Yii::$app->getErrorHandler()->exception) { $response->data = $this->convertExceptionToArray($exception); } //Model出錯(cuò)了 if ($response->statusCode==422) { $messages=[]; foreach ($response->data as $v) { $messages[] = $v['message']; } //請(qǐng)求錯(cuò)誤時(shí)數(shù)據(jù)為 {"success":false,"data":{"name":"Not Found","message":"頁(yè)面未找到。","code":0,"status":404}} $response->data = [ 'name'=> 'valide error', 'message'=> implode(" ", $messages), 'info'=>$response->data ]; } $response->statusCode = 200; } elseif ($response->statusCode>=300) { $response->statusCode = 200; $response->data = $this->convertExceptionToArray(new ForbiddenHttpException(Yii::t('yii', 'Login Required'))); } //請(qǐng)求正確時(shí)數(shù)據(jù)為 {"success":true,"data":{...}} $response->data = [ 'success' => $isSuccessful, 'data' => $response->data, ]; $response->format = Response::FORMAT_JSON; \Yii::$app->getResponse()->getHeaders()->set('Access-Control-Allow-Origin', '*'); \Yii::$app->getResponse()->getHeaders()->set('Access-Control-Allow-Credentials', 'true'); //jsonp 格式輸出 if (isset($_GET['callback'])) { $response->format = Response::FORMAT_JSONP; $response->data = [ 'callback' => $_GET['callback'], 'data'=>$response->data, ]; } }#?? ??#3. ?? ??? ?? ???? ?? ??? ??? ? ??? ?? ?? ???? ???? ???. ???? ??? ??? ????:
/** * 將異常轉(zhuǎn)換為array輸出 * @see \yii\web\ErrorHandle * @param \Exception $exception * @return multitype:string NULL Ambigous <string, \yii\base\string> \yii\web\integer \yii\db\array multitype:string NULL Ambigous <string, \yii\base\string> \yii\web\integer \yii\db\array */ protected function convertExceptionToArray($exception) { if (!YII_DEBUG && !$exception instanceof UserException && !$exception instanceof HttpException) { $exception = new HttpException(500, Yii::t('yii', 'An internal server error occurred.')); } $array = [ 'name' => ($exception instanceof Exception || $exception instanceof ErrorException) ? $exception->getName() : 'Exception', 'message' => $exception->getMessage(), 'code' => $exception->getCode(), ]; if ($exception instanceof HttpException) { $array['status'] = $exception->statusCode; } if (YII_DEBUG) { $array['type'] = get_class($exception); if (!$exception instanceof UserException) { $array['file'] = $exception->getFile(); $array['line'] = $exception->getLine(); $array['stack-trace'] = explode("\n", $exception->getTraceAsString()); if ($exception instanceof \yii\db\Exception) { $array['error-info'] = $exception->errorInfo; } } } if (($prev = $exception->getPrevious()) !== null) { $array['previous'] = $this->convertExceptionToArray($prev); } return $array; }#???? #??, ?? ??? ??? ???? ??? ?? API ?????? ???? ?????? ???? ???? ???? ?? ??? ?? ??? ??? ????. # ????#
??: "#?? ??#Yii2.0 ????? ?? ?? ??? ????
》? ??? Yii2 API ????? ?? ?? Json ? jsonp ?? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

Oracle? ????? ??? ?????? ?? ??? ??????, Oracle? API(?? ????? ?????)? ???? Oracle ??????? ?? ?? ???? ???? ? ??? ?? ??? ?????. ? ????? Oracle API ?? ???? ??? ???? ????? ?? ???? ?? ??? ????? ??? ???? ??? ???? ???? ?? ??? ?????. 1.???

PHP ?????? API ?????? ???? ???? ????? ???? ??? ?????? 1. ?? PHP ??????? ?? ?? ?????? ???? ????? ??? ???? ???? ???. ?? ??????? API ?????? ????, ??? ??? ?????? ???? ???? ?? ? ????. ? ????? PHP? ???? API ?????? ???? ???? ????? ???? ??? ?????. 2. API ?????? URL? ????? ?????. ???? ?? ?? API ?????? URL? ?? ????? ??? ???.

?? ??: API ??? ?? ThinkPHP ?????? ???? ?? ???? ????? ????? API(?? ????? ?????)? ???? ?? ? ??? ????. API? ??? ??, ?? ?? ? ?? ??? ??? ? ??? ????? ??? ???? ?? ?? ??? ?????. ??? PHP ?? ?????? ThinkPHP ?????? ????? ?? ???? ???? ????.

Oracle API ?? ?? ??: ??? ?? ??? ??? ???? ?? ?? ??? ?????. ??? ??? ??? ?? ?? ???? ?? ???? ???? ???? ?? Oracle API? ??? ??? ?? ??? ?? ? ?????. ??? ? ??. ? ????? OracleAPI? ?? ??? ???? ???? API ?? ??? ???? ????? ??? OracleAPI? ? ? ???? ??? ? ??? ???? ?? ??? ?????. 1. ?? ??? API

??: Laravel API ?? ?? ?? ??, ???? ?? ??? ?????. Laravel? ???? ?? API ??? ?? ?????. ??? ??? ???? ?? ?? ??, ?????? ?? ??, ?? API ?? ?? ? ??? ??? ?? ??? ? ????. ??? ?? ???? ???? ??? ?? ?????. ? ????? Laravel API ?? ???? ????? ???? ??? ???? ?? ?? ?? ??? ?????. 1. Laravel? ?? ??

ReactAPI ?? ???: ??? API? ?? ???? ???? ???? ?? ??: ?? ? ????? ??? API? ?? ???? ???? ???? ?? ???? ?? ?????. ?? ???? ????? ?????? React? ? ????? ????? ? ?? ??? ??? ??? ?????. ? ????? React? ???? ?? GET ? POST ??? ???? ??? API? ???? ??? ???? ???? ?? ??? ?????. ??? ???? ??????. ?? Axi? ????? ???? ??? ??????.

PHP API ?????: Insomnia ?? ?? Insomnia? ???? API ?????? ??? ?? ????? ???? ? ??? ???. PHP? ??? ?? ????? ??? ????? ?????. ? ????? Insomnia? ???? PHPAPI ?????? ????? ??? ?????. 1??: Insomnia ?? Insomnia? Windows, MacOS ? Linux? ???? ??? ??? ?????????.

PHP? ? ??????? ? ???? ???? ? ???? ?? ???? ?? ? ???? ?????. ??? ??? API ?????? ?? ??? ? ??? ?? ???? ?? ?? ?????. ? ????? ???? PHP API ?????? ? ?? ???? ??? ?? ? ??? PHP API ?????? ?? ?? ???? ?????. API? ?????? API? "?????? ????? ?????"? ???, ?? ?? ??????? ?? ???? ??? ??? ? ??? ?? ???? ?????. ? ?? ??? W? ????? ???? ??????.
