API接口实例

API就是操作系统留给应用程序的一个调用接口,应用程序通过调用操作系统的 API 而使操作系统去执行应用程序的命令(动作)。本文主要和大家分享PHP关于API接口实例,希望能帮助到大家。


PHP生成JSON数据

json_encode($value) 方法

(response.php和testapi.php)

通信标准格式:

code 状态码   message 提示信息   data返回数据

json如何封装通信数据方法

Response类

  1. 1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    <?php

    classResponse{

    /**

    *按json方式输出通信数据

    *@param integer $code 状态码

    *@param string $message 提示信息

    *@param array $data 数据

    *return string

    */

    publicstaticfunction json($code,$message='',$data=array()){

    if(!is_numeric($code)){

    return'';

    }

    $result=array(

    'code'=>$code,

    'message'=>$message,

    'data'=>$data

    );

    echo json_encode($result);

    exit;

    }

    }

实例使用

  1. 1

    2

    3

    4

    5

    6

    7

    <?php

    require_once('./apitest.php');

    $arr=array(

    'id'=>1,

    'name'=>'huwei',

    );

    Response::json(200,'数据返回成功',$arr);


PHP生成XML数据

1.组装字符串

2.使用系统类

DomDocument

XMLWriter

SimpleXML

封装XML通信接口


封装方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

/**

*按XML方式输出通信数据

*@param integer $code 状态码

*@param string $message 提示信息

*@param array $data 数据

*return string

*/

publicstaticfunction xml($code,$message='',$data=array()){

if(!is_numeric($code)){

return'';

}

$result=array(

'code'=>$code,

'message'=>$message,

'data'=>$data

);

header("Content-Type:text/xml");//将头信息转换为XML格式

$xml="<?xml version='1.0' encoding='UTF-8'?>\n";

$xml.="<root>\n";

$xml.=self::xmlToEncode($result);

$xml.="</root>";

return $xml;

}

publicstaticfunction xmlToEncode($data){

$xml=$attr="";

foreach($data as $k=>$v){

if(is_numeric($k)){

$attr=" id='{$k}'";

$k="item";

}

$xml.="<{$k}{$attr}>";

$xml.=is_array($v)?self::xmlToEncode($v):$v;

$xml.="</{$k}>";

}

return $xml;

}

实现:

<?php

require_once('./apitest.php');

$arr=array(

'id'=>1,

'name'=>'huwei',

'type'=>array(1,2,3)

);

//echo Response::json(200,'数据返回成功',$arr);

echo Response::xml(200,'数据返回成功',$arr);

综合通信方法封装

  

封装方法

const JSON="json";

/**

*按综合方式输出通信数据

*@param integer $code 状态码

*@param string $message 提示信息

*@param array $data 数据

*@param string $type 类型

*return string

*/

publicstaticfunction show($code,$message='',$data=array(),$type==self::JSON){

if(!is_numeric($code)){

return'';

}

$result=array(

'code'=>$code,

'message'=>$message,

'data'=>$data,

);

if($type=='json'){

returnself::json($code,$message,$data);

}elseif($type=='array'){

var_dump($result);

}elseif($type=='xml'){

returnself::xml($code,$message,$data);

}else{

//TODO

}

}

调用方式:

<?php

require_once('./apitest.php');

$arr=array(

'id'=>1,

'name'=>'huwei',

'type'=>array(1,2,3)

);

//echo Response::json(200,'数据返回成功',$arr);

//echo Response::xml(200,'数据返回成功',$arr);

echo Response::show(200,'数据返回成功',$arr,'array');

缓存技术:

1.静态缓存

保存在磁盘上的静态文件,用PHP生成的数据放入静态缓存文件中

PHP操作缓存(file.php)

生成缓存、获取缓存、删除缓存

封装类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

<?php

classFile{

private $_dir;// 文件路径

const EXT='.txt';//文件后缀

publicfunction __construct(){

$this->_dir=dirname(__FILE__).'\files\/';//获取该项目同级目录

}

publicfunction cacheData($key,$value='',$path=''){

$filename=$this->_dir.$path.$key.self::EXT;

if($value!==''){//将value值写入缓存

if(is_null($value)){

return@unlink($filename);

}

$dir=dirname($filename);

if(!is_dir($dir)){

mkdir($dir,0777);

}

return file_put_contents($filename,json_encode($value));//若成功返回字节数,不然为false

}

if(!is_file($filename)){

returnFalse;

}else{

return json_decode(file_get_contents($filename),true);

}

}

}

调用类

<?php

//require_once('./apitest.php');

require_once('./file.php');

$arr=array(

&#

评论 (0)

发表评论