ThinkPHP5开发API接口实例(life)

98 篇文章 0 订阅
97 篇文章 0 订阅

接口功能说明: 前端提交学生学号(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 = 'http://localhost/index.php/index/index/api/';

// 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、高并发、分布式等资料,现在免费分享给大家,官方群点击此处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值