php-app介面實作(json和xml)

WBOY
發布: 2016-07-30 13:31:44
原創
1269 人瀏覽過

1.回顧

   上篇學習了封裝mysql的實例化物件類別!

2.這篇將封裝一個app介面類,用來產生json資料和xml資料

3.了解並掌握

xml:擴充標記語言:可以標記數據,定義數據類型;數據格式清晰明了, 可讀性高;           json:一種輕量級的數據交換格式;生成數據簡單;傳輸速度快;


  3.資料

                  取得資料:從資料庫或快取中取得資料(可為快取中的資料)     資料端   

      

    3.3 通訊資料標準格式

              狀態碼:code    回傳資料:  data

    3.4 定時任務與快取(這裡不封裝)


         快取:與Redis 快取技術
          定時任務:corntab 

4.封裝

    json方式封裝介面資料方法
                函數json_encode();
               只接收utf-8

     4.2 xml封裝

                       4.3 實作類別


 

<?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 調用資料型別type可以為xml ,json ,array !


http://localhost:8081/appInterface/test.php?type=json
登入後複製

          test.php 實作如下:

sql

3篇資料庫組合資料庫

require_once 'appUtil.php';
登入後複製

6. appUtil.php下載 

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

版權聲明。不得轉載。

以上就介紹了php-app介面實作(json和xml),包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!