How to encapsulate app in php: 1. Create json.php file and demo.php file; 2. Use array to represent JSON format data in php; 3. Enclose the array into JSON data.
The operating environment of this article: windows7 system, PHP7.4 version, DELL G3 computer
Use php to encapsulate the APP interface
php encapsulation APP interface
Let’s first introduce the Json encapsulation method
If the json_encode function passes Chinese, the output will be garbled. , I think it is necessary to make an explanation for this problem:
In fact, json_encode is not garbled for Chinese, but json_encode will convert Chinese into unicode encoding, so the output will be this encoding, but it does not affect our use. We After json_decode, it will be a normal value, but if you want to output json_encode in Chinese, there is actually a way, as follows:
echo urldecode(json_encode(urlencode("JSON Chinese output solution")) ; Many only have this function;
But in terms of readability, XML looks more intuitive, while Json looks messy and time-consuming;
The above is Json below Let’s get an XML one! Method: PHP generates XML data;
How to generate XML data?
The following two methods can be achieved--> 1. Assemble into an XML string 2. Use system classes such as: DomDocument, XMLWriter, SimpleXML
Go to the Demo directly first!<?php //服务端 json.php //php中用数组表示JSON格式数据 header("Content-type:text/html;charset=utf-8"); $arr = array( 'code' => 200, 'message' => '数据返回成功', 'data' => array( 'name' =>'bawei', 'email' =>'www.bawei@qq,com', ), ); echo json_encode($arr); //将数组封闭成JSON数据 主要函数json_encode; ?>
The above is the detailed content of How to encapsulate app in php. For more information, please follow other related articles on the PHP Chinese website!