龙空技术网

ThinkPHP5开发API接口实例

程序猿的生活1 343

前言:

目前看官们对“thinkphp写app接口”大约比较关怀,各位老铁们都想要分析一些“thinkphp写app接口”的相关资讯。那么小编在网摘上汇集了一些对于“thinkphp写app接口””的相关资讯,希望兄弟们能喜欢,你们一起来了解一下吧!

接口功能说明: 前端提交学生学号(sno)给Api,Api接口返回此学生的基本信息

API接口端

namespace app\index\controller;use think\Controller;use app\index\model\Student;class User{public function index() {return $this->fetch();}// 客户端提交学生学号(sno)给api,api返回此学生的基本信息public function api($sno='0001') {// 查询 并把数据赋值给 $data$data = Student::getBysno($sno);// 返回数据return json($data);}}

(请求端) HTML

TP5通过API查询数据

(请求端) C层控制器

namespace app\index\controller;use think\Controller;class User extends Controller {public function index() {return $this->fetch();}public function capi() {// http协议请求$url = ';;// input('sno') 是前端的from传过来的name值$ch = curl_init($url.'?sno='.input('sno'));curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 执行 并把执行后的数据赋值给 $data$data = curl_exec($ch);// 关闭curl_close($ch);// 返回数据return $data;}}

你是否有这样的烦恼,想学习高级技术,缺乏好的高级学习资料,收集了部分12年网站架构师授课的TP5、laravel、swoole、swoft、高并发、分布式等资料,现在免费分享给大家。

获取地址:

标签: #thinkphp写app接口