在thinkphp5.1中如何写接口及如何调用接口
对于php不熟悉的人来说,解除thinkphp还是挺有难度的。
下面记录如何编写接口。
及如何对编写的接口进行调用。
<?php
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
namespace app\api\controller;
use controller\BasicApi;
use service\DataService;
use service\NodeService;
use service\ToolsService;
use think\Db;
use think\exception\HttpResponseException;
/**
* 兼容适配接口
* Class Member
* @package app\api\controller
*/
class Ecodemo extends BasicApi {
protected $table = 'Compatible';
/**
* 初始化
* @access protected
*/
protected function initialize() {
parent::initialize();
}
/**
* 获取适配信息
* @access public
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function index() {
$param = $this->request->param(); //获取请求的参数
if (empty($_SERVER['HTTP_AUTHORIZATION']))
throw new HttpResponseException(json(['code' => 0, 'msg' => 'error!']));
$sign = $_SERVER['HTTP_AUTHORIZATION'];
$company = Db::name('authentication')->where('token', $sign)->value('company');
if (empty($company))
throw new HttpResponseException(json(['code' => 0, 'msg' => 'error!']));
$db = Db::name($this->table)
->order(['sort' => 'asc', 'id' => 'desc'])
->where('is_deleted', 0)
->where('id', $param['list_id'])->field(['name','update_time','desc']); //数据库查询
// dump($db);
return parent::_list($db);
// throw new HttpResponseException(json(['code' => 1, 'list' => $db]));
}
/**
* 修改适配信息
* @access public
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function update() {
$param = $this->request->param();
if (empty($_SERVER['HTTP_AUTHORIZATION']))
throw new HttpResponseException(json(['code' => 0, 'msg' => 'error!']));
$sign = $_SERVER['HTTP_AUTHORIZATION'];
$company = Db::name('authentication')->where('token', $sign)->value('company');
if (empty($company))
throw new HttpResponseException(json(['code' => 1, 'msg' => 'error!']));
if ($this->request->isPost()) {
Db::name($this->table)
->where('id',$param['list_id'])
->data(['name' => $param['content']])
->update(); //数据库修改
throw new HttpResponseException(json(['code' => 0, 'msg' => '修改成功']));
} else {
throw new HttpResponseException(json(['code' => 1, 'msg' => 'error!']));
}
}
/**
* 获取请求header
*
*/
function getallheaders($param = null) {
$headers = array();
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
if($param != null){
return $headers[$param];
}
return $headers;
}
}
<?php
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
namespace app\api\controller;
use controller\BasicApi;
use service\DataService;
use service\NodeService;
use service\ToolsService;
use think\Db;
use think\exception\HttpResponseException;
/**
* 兼容适配接口
* Class Member
* @package app\api\controller
*/
class Ecodemo extends BasicApi {
protected $table = 'Compatible';
/**
* 初始化
* @access protected
*/
protected function initialize() {
parent::initialize();
}
/**
* 获取适配信息
* @access public
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function index() {
$param = $this->request->param(); //获取请求的参数
if (empty($_SERVER['HTTP_AUTHORIZATION']))
throw new HttpResponseException(json(['code' => 0, 'msg' => 'error!']));
$sign = $_SERVER['HTTP_AUTHORIZATION'];
$company = Db::name('authentication')->where('token', $sign)->value('company');
if (empty($company))
throw new HttpResponseException(json(['code' => 0, 'msg' => 'error!']));
$db = Db::name($this->table)
->order(['sort' => 'asc', 'id' => 'desc'])
->where('is_deleted', 0)
->where('id', $param['list_id'])->field(['name','update_time','desc']); //数据库查询
// dump($db);
return parent::_list($db);
// throw new HttpResponseException(json(['code' => 1, 'list' => $db]));
}
/**
* 修改适配信息
* @access public
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function update() {
$param = $this->request->param();
if (empty($_SERVER['HTTP_AUTHORIZATION']))
throw new HttpResponseException(json(['code' => 0, 'msg' => 'error!']));
$sign = $_SERVER['HTTP_AUTHORIZATION'];
$company = Db::name('authentication')->where('token', $sign)->value('company');
if (empty($company))
throw new HttpResponseException(json(['code' => 1, 'msg' => 'error!']));
if ($this->request->isPost()) {
Db::name($this->table)
->where('id',$param['list_id'])
->data(['name' => $param['content']])
->update(); //数据库修改
throw new HttpResponseException(json(['code' => 0, 'msg' => '修改成功']));
} else {
throw new HttpResponseException(json(['code' => 1, 'msg' => 'error!']));
}
}
/**
* 获取请求header
*
*/
function getallheaders($param = null) {
$headers = array();
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
if($param != null){
return $headers[$param];
}
return $headers;
}
}
最后我发现无论是get、post、put请求,我们都可以通过$this->request->param();方法获取到参数信息。

发表评论 取消回复