<?php
//简单形式
header('Content-Type:text/html;charset=utf-8'); //避免输出乱码
$output = array();
$a = @$_GET['a'] ? $_GET['a'] : '';
$uid = @$_GET['uid'] ? $_GET['uid'] : 0;
if (empty($a)) {
$output = array('data'=>NULL, 'info'=>'坑爹啊!', 'code'=>-201);
exit(json_encode($output));
}
//
//走接口
if ($a == 'get_users') {
//检查用户
if ($uid == 0) {
$output = array('data'=>NULL, 'info'=>'uid是空的', 'code'=>-401);
exit(json_encode($output));
}
//假设 $mysql 是数据库
$mysql = array(
10001 => array(
'uid'=>10001,
'vip'=>5,
'nickname' => 'Shine X',
'email'=>'979137@qq.com',
'qq'=>979137,
'gold'=>1500,
'powerplay'=> array('2xp'=>12,'gem'=>12,'bingo'=>5,'keys'=>5,'chest'=>8),
'gems'=> array('red'=>13,'green'=>3,'blue'=>8,'yellow'=>17),
'ctime'=>1376523234,
'lastLogin'=>1377123144,
'level'=>19,
'exp'=>16758,
),
10002 => array(
'uid'=>10002,
'vip'=>50,
'nickname' => 'elva',
'email'=>'elva@ezhi.net',
'qq'=>NULL,
'gold'=>14320,
'powerplay'=> array('2xp'=>1,'gem'=>120,'bingo'=>51,'keys'=>5,'chest'=>8),
'gems'=> array('red'=>13,'green'=>3,'blue'=>8,'yellow'=>17),
'ctime'=>1376523234,
'lastLogin'=>1377123144,
'level'=>112,
'exp'=>167588,
),
10003 => array(
'uid' => 10003,
'vip' => 5,
'nickname' => 'Lily',
'email' => 'Lily@ezhi.net',
'qq' => NULL,
'gold' => 1541,
'powerplay'=> array('2xp'=>2,'gem'=>112,'bingo'=>4,'keys'=>7,'chest'=>8),
'gems' => array('red'=>13,'green'=>3,'blue'=>9,'yellow'=>7),
'ctime' => 1376523234,
'lastLogin'=> 1377123144,
'level' => 10,
'exp' => 1758,
),
);
$uidArr = array(10001,10002,10003);
if (in_array($uid, $uidArr, true)) {
$output = array('data' => NULL, 'info'=>'The user does not exist!', 'code' => -402);
exit(json_encode($output));
}
//查询数据库
$userInfo = $mysql[$uid];
//输出数据
$output = array(
'data' => array(
'userInfo' => $userInfo,
'isLogin' => true,//是否首次登陆
'unread' => 4,//未读消息数量
'untask' => 3,//未完成任务
),
'info' => 'Here is the message which, commonly used in popup window', //消息提示,客户端常会用此作为给弹窗信息。
'code' => 200, //成功与失败的代码,一般都是正数或者负数
);
exit(json_encode($output));
} elseif ($a == 'get_games_result') {
//...
die('您正在调 get_games_result 接口!');
} elseif ($a == 'upload_avatars') {
//....
die('您正在调 upload_avatars 接口!');
}
//调用测试http://localhost:63342/HelloPHP/API.php?a=get_users&uid=10001
//返回结果
{
"data": {
"userInfo": {
"uid": 10001,
"vip": 5,
"nickname": "Shine X",
"email": "979137@qq.com",
"qq": 979137,
"gold": 1500,
"powerplay": {
"2xp": 12,
"gem": 12,
"bingo": 5,
"keys": 5,
"chest": 8
},
"gems": {
"red": 13,
"green": 3,
"blue": 8,
"yellow": 17
},
"ctime": 1376523234,
"lastLogin": 1377123144,
"level": 19,
"exp": 16758
},
"isLogin": true,
"unread": 4,
"untask": 3
},
"info": "Here is the message which, commonly used in popup window",
"code": 200
}
//例子二:
//开始GET接口
//我们的空项目在首次运行后,会自动生成 index.php 这个文件,我们删除里面的内容,然后修改如下:?
$data = array(
'tid' => 100,
'name' => '标哥的技术博客',
'site' => 'www.huangyibiao.com');
$response = array(
'code' => 200,
'message' => 'success for request',
'data' => $data,
);
echojson_encode($response);
//我们这里的结构是日常App开发中最常见的返回数据结构了吧?通常都是状态码、状态信息和客户端业务数据。
//PHP其实也好给力,声明数组后,通过 json_encode 函数就可以输出json格式数据了!
//PHP POST接口
//假设我们要求传参数type过来,而且要求是数值型,用于返回不同的数据,当我们做接口测试时,就可以通过这么来干,就不需要等后台给接口了!
//
$type = $_POST['type'];
$data = '';
if (isset($type) && is_numeric($type) && $type >= 0) {
if ($type == 1) {
$data = array(
'type' => $type,
'name' => '标哥的技术博客',
'site' => 'www.huangyibiao.com');
} else if ($type == 2) {
$data = array(
'type' => $type,
'name' => '公众号:标哥的技术博客',
'site' => 'weixin search: biaogedejishuboke');
}
$response = array(
'code' => 200,
'message' => 'success for request',
'data' => $data,
);
echojson_encode($response);
return;
}
$response = array(
'code' => 999,
'message' => 'argument error for request',
'data' => $data,
);
echojson_encode($response);
//本篇就到此为止吧,相信大家若想学习它,一定会认真去操作一遍的!