Author: Bailang Source: http://www.manks.top/yii2_xml_response.html.html The copyright of this article belongs to the author, and you are welcome to reprint it. However, this statement must be retained without the author's consent, and the original text must be given in a prominent position on the article page. connection, otherwise we reserve the right to pursue legal liability.
Xml processing in PHP is rarely used in actual development, but it will inevitably be used. When it is used, in summary, it is still a little troublesome.
Let’s take a look at how xml is processed in yii2. It will be easier than you think.
We take outputting data in xml format as an example.
Since it is output, it must involve web requests and responses. If you are not familiar with it, you can first understand the HTTP protocol.
yii2 supports the following return formats, all of which can be customized.
HTML: implemented by yiiwebHtmlResponseFormatter.
XML: implemented by yiiwebXmlResponseFormatter.
JSON: implemented by yiiwebJsonResponseFormatter.
JSONP: implemented by yiiwebJsonResponseFormatter.
RAW: use this format if you want to send the response directly without applying any formatting.
We are here for XML.
Let’s first look at a simple output of xml format data
<span>public</span><span>function</span><span> actionTest () { \Yii</span>::<span>$app</span>->response->format = \yii\web\Response::<span>FORMAT_XML; </span><span>return</span><span> [ </span>'message' => 'hello world', 'code' => 100,<span> ]; }</span>
Here we specify the response format FORMAT_XML, and then access this test method to see that xml type data is output on the page
<span><</span><span>response</span><span>></span><span><</span><span>message</span><span>></span>hello world<span></</span><span>message</span><span>></span><span><</span><span>code</span><span>></span>100<span></</span><span>code</span><span>></span><span></</span><span>response</span><span>></span>
Above The mentioned method is a bit troublesome. It is not so convenient when configuring multiple items. Let’s try to create the response object ourselves
<span>public</span><span>function</span><span> actionTest () { </span><span>return</span> \Yii::<span>createObject([ </span>'class' => 'yii\web\Response', 'format' => \yii\web\Response::FORMAT_XML, 'formatters' =><span> [ \yii\web\Response</span>::FORMAT_XML =><span> [ </span>'class' => 'yii\web\XmlResponseFormatter', 'rootTag' => 'urlset', <span>//</span><span>根节点 </span> 'itemTag' => 'url', <span>//</span><span>单元 </span> ],<span> ]</span>, 'data' => [ <span>//</span><span>要输出的数据 </span><span> [ </span>'loc' => 'http://********',<span> ]</span>,<span> ]</span>,<span> ]); }</span>
In order to facilitate the following explanation, the above configurations are also done. You can see that we have configured the response format and made some separate configurations, including configuring the root node rootTag, unit itemTag and data type. Some students have noticed that here we have actually implemented a site map output in xml format very simply. Yes, it's that simple.
The above introduces the xml format data output by yii2, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.