Home  >  Article  >  Backend Development  >  php-app interface implementation (json and xml)

php-app interface implementation (json and xml)

WBOY
WBOYOriginal
2016-07-30 13:31:441303browse

1. Review

In the previous article, we learned about the instantiated object class that encapsulates mysql!

2. This article will encapsulate an app interface class to generate json data and xml data

3. Understand and master

3.1 The difference between xml and json

xml: extended tag Language: You can mark data and define data types; the data format is clear and readable;
                  json: a lightweight data exchange format; simple to generate data, fast transmission speed;

3.2 app interface and Data

                                                           .

3.3 Communication data standard format

              Status code: code

                  Prompt information: message                                                      off 3.4 Scheduled tasks and cache (not encapsulated here)


Cache: static cache, Memcache And Redis cache technology i timing task: Corntab

4. Packaging

4.1 JSON packaging

JSON method packaging interface data method

function json_encode ();

4.2 xml encapsulation

Assembling string (simple)

4.3 Implementation class

<?php

//header("Content-type:text/html;charset=utf-8");

class Response{
	
	const JSON=&#39;json&#39;;
	/**
	 * 01.综合通信入口
	 * @param int $code
	 * @param string $msg
	 * @param array $data
	 * @param string $type
	 */
	public static function show($code,$msg=&#39;&#39;,$data=array()){
		
		if(!is_numeric($code)){
			return &#39;&#39;;
		}
		//如果url上传参了,去参数的类型,否则取得默认值!
		$type=isset($_GET[&#39;type&#39;])?$_GET[&#39;type&#39;]:self::JSON;
		
		
		$result=array(
				&#39;code&#39;=>$code,
				'msg'=>$msg,
				'data'=>$data
		);
		if($type=='json'){
			self::jsonEncode($code,$msg,$data);
			exit();
		}elseif ($type=='xml'){
			self::xmlEncode($code,$msg,$data);
			exit();
		}elseif ($type='array'){
			var_dump($result);
			exit();
		}
		
	}
	
	/**
	 * 02.按json方式输出 通信数据
	 * @param int $code 状态码
	 * @param string $msg 提示信息
	 * @param array $data 数据
	 * retrun string 
	 */
	public static function jsonEncode($code,$msg='',$data=array()) {
		header("Content-Type:text/json");
		#判断状态码
		if(!is_numeric($code)){
			return '';
		}
		$result=array(
				'code'=>$code,
		        'msg'=>$msg,
				'data'=>$data
		);
		
		echo json_encode($result);
		exit();
	}
	
	
    /**
     * 03.封装xml 输出通信数据
     * @param unknown $code
     * @param unknown $msg
     * @param unknown $data
     */
	public static function xmlEncode($code,$msg='',$data=array()){
		
		
		if(!is_numeric($code)){
           return '';
		}

		$result=array(
				'code'=>$code,
				'msg'=>$msg,
				'data'=>$data
		);
		
		header("Content-Type:text/xml");
		$xml="<?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?>";
		$xml.="<root>";
		$xml.=self::xmlToEncode($result);
		$xml.="</root>";
		echo $xml;
		
		exit();
	}
	
	/**
	 *04. 拼装 xml数据
	 * @param array $data
	 * @return string
	 * 使用递归,判断是不是数组,是数组继续调用循环
	 * xml的 节点不能为 数字,用item代替
	 */
	public  static function xmlToEncode($data){
		$xml=$attr='';
		foreach ($data as $key=>$value){
			if(is_numeric($key)){
				$attr="id='{$key}'";
				$key="item";
			}
			
			
			$xml.="<{$key} {$attr}>";
			$xml.=is_array($value)?self::xmlToEncode($value):$value;
			$xml.="</{$key}>";
		}
		return $xml;
	}
	
}

?>

4.4 Call



Implemented through url, display The data type can be xml, json, array!

http://localhost:8081/appInterface/test.php?type=json
Test.php is implemented as follows:


require_once 'appUtil.php';
$arr=array(
		'id'=>1,
		'name'=>'yuan',
		'age'=>23,
		'location'=>'hpu'
);

$arr1=array(1,4,5,2,6,3);

Response::jsonEncode(200,'success',$arr);

5. In summary, the previous article is connected to mysql database implementation: data encapsulation

//调用
$con=Db::getInstance()->connect();

//查询语句
$sql='select * from user_info';
//执行,返回结果集
$result=mysql_query($sql,$con);
//添加的新数组
$arr3=array();
while ($row=mysql_fetch_row($result)){
	array_push($arr3,$row);
}
Response::show('200','success',$arr3);

6. appUtil.php download

http://download.csdn.net/detail/lablenet/8995987

Copyright statement: This article is an original article by the blogger, without the blogger's permission No reproduction allowed.

The above introduces the php-app interface implementation (json and xml), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn