組件配置添加:php
'request' => [ 'parsers' => [ 'application/json' => 'yii\web\JsonParser', 'application/xml' => 'common\components\XmlParser', 'text/xml' => 'common\components\XmlParser' ] ], 'response' => [ 'formatters' => [ \yii\web\Response::FORMAT_XML => [ 'class' => 'yii\web\XmlResponseFormatter', 'rootTag' => 'xml' ] ] ]
common\components\XmlParserweb
<?php namespace common\components; use yii\base\InvalidParamException; use yii\web\BadRequestHttpException; use yii\web\RequestParserInterface; class XmlParser implements RequestParserInterface { public $asArray = true; public $throwException = true; /** * Parses a HTTP request body. * @param string $rawBody the raw HTTP request body. * @param string $contentType the content type specified for the request body. * @return array parameters parsed from the request body * @throws BadRequestHttpException if the body contains invalid json and [[throwException]] is `true`. */ public function parse($rawBody, $contentType) { try { $parameters = simplexml_load_string($rawBody, 'SimpleXMLElement', LIBXML_NOCDATA); $parameters = $this->asArray ? (array) $parameters : $parameters; return $parameters === null ? [] : $parameters; } catch (InvalidParamException $e) { if ($this->throwException) { throw new BadRequestHttpException('Invalid XML data in request body: ' . $e->getMessage()); } return []; } } }
而後就能夠在控制器裏直接取$params = Yii::$app->bodyParams
json
xml格式適合微信公衆號開發。微信