用interface關鍵字定義,示例:php
interface video(){web
public function getVideos();json
public function getCount();//這都是虛擬的方法api
}數組
接口的實現:【接口中給的全部方法都必須在示例中實現】服務器
class movie implements video{app
public function getVideo(){ide
//do somethingui
}編碼
public function getCount(){
//do something
}
}
接口地址——返回接口數據——解析數據——客戶端
APP(通訊)接口:
1、接口地址(http://app.com/api.php?format=xml);
2、接口文件(api.php,處理一些業務邏輯);
3、接口數據
APP如何進行通訊:
客戶端App觸發——》發送http請求(接口地址)——》服務器——》返回客戶端
返回的數據格式通常爲xml或者json
地址被封裝在app裏面用戶不可見,與通常的web不一樣
XML:擴展標記語言,節點能夠自定義(而HTML內標籤是不可自定義的),格式統一,可跨平臺使用,適合通訊和傳輸。
示例:
<?xml version=」1.0」 encoding=」UTF-8」?>
<item>
<title>singwa</title>
<test id=」1」/>
<description>singwa1</description>
<address>beijing</address>
</item>
XML可讀性強;JSON生成數據方面、傳輸速度方面強。
接口做用:獲取數據、提交數據。
封裝通訊接口數據方法
JSON方式封裝通訊接口:json_encode()【必須是UTF-8的形式】
示例:
<?php
$arr = {
‘id’ => 1;
‘username => Tom’
}
echo json_encode($arr);
iconv(‘UTF-8’,’GBK’,$data)//用於進行編碼轉化,本例將$data由UTF-8轉化爲GBK
通訊數據標準格式
一、code:200 //狀態碼
二、message //提示信息
三、data //返回數據
JSON封裝數據方法示例(response.php):
<?php
class Respomse{
public static function json($code,$message = ‘’,$data = array()){
if(!is_numeric($code)){
return ‘’;//若是傳過來的$code不是數字返回空
}
$result = array(
‘code’ => $code,
‘message’ => $message,
‘data’ => $data
);
echo json_encode($result);
exit;
}
}
在其餘文件調用這個接口:
<?php
require_once(‘./response.php’);
$arr = array(
‘id’ => 1,
‘name’ => ‘fareise’
);
Response::json(200,’數據返回成功’,’$arr’);//這樣就會返回一個json數據
XML方式封裝接口數據方法
PHP生成XML數據的方法
一、組裝成字符串
二、使用系統類(DomDocument;XMLWriter;SimpleXML)
示例:
<?php
class Response{
public static function xml(){
header(「Content-Type:text/xml」);//將類型轉化爲xml的類型
$xml = 「<?xml version=’1.0’ encoding=’UTF-8’?>\n」
$xml.=」<root>\n」;
$xml.=」<code>200</code>\n」;
$xml.=」<message>200</message>\n」;
......
$xml.=」</root>」;
echo $xml;
}
}
XML方式封裝通訊接口
<?php
class Response{
public static function xmlEncode($code,$message,$data){
if(!is_numeric($code)){
return ‘’;
}
$result = array(
‘code’ => $code,
‘message’ => $message,
‘data’ => $data,
);
header(「Content-Type:text/xml」);
$xml = 「<?xml version = ‘1.0’ encoding = ‘UTF-8’?>」
$xml .= 「<root>」;
$xml .= self::xmlToEncode($result) //經過定義的這個方法對數據進行xm解析
$xml .= 「</root>」;
}
publlc static function amlToEncode($data){ // 經過這樣的遍歷將每一條數據寫成標籤
$xml = $attr = 「」;
foreach($data as $key => $value){//若是key是數字則轉化爲<item id=」1」>的形式
if(is_numeric($key)){
$attr = 「id = ‘{$key}’」;
$key = 「item」;
}
$xml .= 「<{$key}{$attr}>」;
//若是是數組,採用遞歸的方式把數組內每個元素都用標籤輸出
$xml .= is_array($value)?self::xmlToEncode($value):$value;
$xml .= 「</{key}>」
}
}
}
調用示例:
$data = array(
‘id’ => 1,
‘name’ => fareise
‘type => array(4,5,6)’
)
Response::xmlEncode(200,’sucess’,$data);
綜合通訊封裝方法(兩種都支持)
在前兩個方法都寫好的基礎上:
const JSON = ‘json’//設定默認值
public static function show($code,$message = ‘’,$data = array(),$type=self::JSON){
if(!is_numeric($code)){
return ‘’;
}
$type = $_GET[‘format’]?$_GET[‘format’]:self」」JSON;
$result = array(
‘code’ => $code,
‘message’ => $message,
‘data’ => $data
);
if($type == ’json’){
self::json($code,$message,$data);
exit;
}elseif($type == ‘array’){
var dump($result)
}elseif($type == ‘xml’){
self::xmlEncode($code,$message,$data);
exit;
}else{
//TODO
}
}