2-1 JSON方式封装通信接口 (14:39)
先上代码:
response.class.PHP是一个最简单的返回json格式数据的类,在下面的课程中我们会进一步对其进行完善。
下面贴出接口文件代码:
returndata.php
[php] view plain copy
- <?php
- require "response.class.php"; //引入返回信息类
- //准备返回数据
- $code = 200;
- $message = "信息请求成功";
- $data = array(
- "name" => "ruanwnewu",
- "sex" => "1",
- "age" => "28",
- "exp" => array(
- "2012" => "北京瑞泰新",
- "2013" => "兄弟连",
- "2014" => "木蚂蚁科技"
- )
- );
- //实例化response类
- $response = new Response;
- //返回数据
- echo $response -> json($code,$message,$data);
请求returndata.php接口返回的数据如下图所示:
这样就完成了一个最基本的,封装好的json数据通信接口。
2-2 PHP生成XML数据 (12:02)
生成XML格式数据一般有三种方法:
在这里我只讲解第一种方法生成XML数据的方法。上代码:
producexml.php
2-3 XML方式封装通信接口 (17:50)
上代码:
[php] view plain copy
- <?php
- /**
- *description 用于返回指定数据格式的类
- *@param $code [int] 返回的状态码
- *@param $message [string] 返回的状态信息
- *@param $data [array] 需要返回的数据
- */
- class Response{
- public function json($code,$message,$data){
- $result = array(
- "code" => $code,
- "message" => $message,
- "data" => $data
- );
- return json_encode($result);
- }
- public function xml($code,$message,$data){
- $result = array(
- "code" => $code,
- "message" => $message,
- "data" => $data
- );
- header('Content-Type:text/xml');
- $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
- $xml .= "<root>";
- $xml .= self::encodeXml($result);
- $xml .= "</root>";
- return $xml;
- }
- /**
- *将数据解析为XML字符串
- */
- public static function encodeXml($data){
- $attr = $xml = "";
- foreach($data as $key => $value){
- if(is_numeric($key)){
- $attr = " id='{$key}'";
- $key = "item";
- }
- $xml .= "<{$key}{$attr}>";
- $xml .= is_array($value)?self::encodeXml($value):$value;
- $xml .= "</$key>";
- }
- return $xml;
- }
- }
同样用returndata.php调用该接口:
[php] view plain copy
- <?php
- require "response.class.php"; //引入返回信息类
- //准备返回数据
- $code = 200;
- $message = "信息请求成功";
- $data = array(
- "name" => "ruanwnewu",
- "sex" => "1",
- "age" => "28",
- "exp" => array(
- "2012" => "北京瑞泰新",
- "2013" => "兄弟连",
- "2014" => "木蚂蚁科技"
- )
- );
- //实例化response类
- $response = new Response;
- //返回数据
- echo $response -> xml($code,$message,$data);
得到如下图所示的XML文件结果
2-4 综合方式封装通信数据方法 (11:15)
将两种封装方法综合起来,并能够根据请求的参数来返回指定格式的数据,上代码:
完善后的response类
总结:[php] view plain copy
- <?php
- /**
- *description 用于返回指定数据格式的类
- *@param $code [int] 返回的状态码
- *@param $message [string] 返回的状态信息
- *@param $data [array] 需要返回的数据
- */
- class Response{
- public function show($code,$message,$data,$type="json"){
- if($type == "json"){
- $this->json($code,$message,$data);
- }else{
- $this->xml($code,$message,$data);
- }
- }
- public function json($code,$message,$data){
- $result = array(
- "code" => $code,
- "message" => $message,
- "data" => $data
- );
- return json_encode($result);
- }
- public function xml($code,$message,$data){
- $result = array(
- "code" => $code,
- "message" => $message,
- "data" => $data
- );
- header('Content-Type:text/xml');
- $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
- $xml .= "<root>";
- $xml .= self::encodeXml($result);
- $xml .= "</root>";
- return $xml;
- }
- /**
- *将数据解析为XML字符串
- */
- public static function encodeXml($data){
- $attr = $xml = "";
- foreach($data as $key => $value){
- if(is_numeric($key)){
- $attr = " id='{$key}'";
- $key = "item";
- }
- $xml .= "<{$key}{$attr}>";
- $xml .= is_array($value)?self::encodeXml($value):$value;
- $xml .= "</$key>";
- }
- return $xml;
- }
- }
联系客服