发布时间:2026-09-30 05:33:18 浏览次数:2
http://localhost/openUser.php?act=get_user_list&type=json 在这里openUser.php相当于一个接口,其中get_user_list 是一个API(获取用户列表),讲求返回的数据类型为JSON格式。
你只需要在你PHP代码中执行这条链接他就会返回。GET方式的直接使用
$file_contents = file_get_content('http://localhost/openUser.php?act=get_user_list&type=json')POST方式得用下面的(需要开启PHP curl支持)。
$url = 'http://localhost/openUser.php?act=get_user_list&type=json';$ch = curl_init ();curl_setopt ( $ch, CURLOPT_URL, $url );curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );curl_setopt ( $ch, CURLOPT_POST, 1 ); //启用POST提交$file_contents = curl_exec ( $ch );curl_close ( $ch );一、先简单回答两个问题,更多认识可以拉到底部获取视频教程
1、PHP 可以开发客户端?
答:不可以,因为PHP是脚本语言,是负责完成 B/S架构 或 C/S架构 的S部分,即:服务端的开发。(别去纠结 GTK、WinBinder)
2、为什么选择 PHP 作为开发服务端的首选?
答:跨平台(可以运行在UNIX、Linux、WINDOWS、Mac OS下)、低消耗(PHP消耗相当少的系统资源)、运行效率高(相对而言)、MySQL的完美搭档,本身是免费开源的,……
二、如何使用 PHP 开发 API(Application Programming Interface,应用程序编程接口) 呢?
做过 API 的人应该了解,其实开发 API 比开发 WEB 更简洁,但可能逻辑更复杂,因为 API 其实就是数据输出,不用呈现页面,所以也就不存在 MVC(API 只有 M 和 C)
<?php$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'=>'The uid is null!', '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://www.mano100.cn/api/test/index.php
http://www.mano100.cn/test/index.php?a=get_users
http://www.mano100.cn/test/index.php?a=get_users&uid=10001
http://www.mano100.cn/api/test/index.php?a=get_users&uid=10002
http://www.mano100.cn/api/test/index.php?a=get_users&uid=10003
三、实际项目中,我们在开发 API 应该注意的几个事项(仅供参考):
1、单文件实现多接口的形式有很多种,例如:if..elseif.. 或 switch 或 动态方法 (也就是TP的这种访问函数体的形式)