alisajjad/php7_api
Composer 安装命令:
composer create-project alisajjad/php7_api
包简介
A PHP basic starter for single url API development. It provides the basic functionalities with different authorization handles as well as Mysqli Wrapper.
README 文档
README
A PHP basic starter for single url API development. It provides the basic functionalities with different authorization handles as well as Mysqli Wrapper.
Requirements
- PHP 7.2 or higher with mysqli extension
Installation
composer require alisajjad/php7_api
File Structure
- app: Contains core app files
- components: Contains your custom controllers
- connection: Mysqli Wrapper & result storage objects
- includes: Contains common functions, traits, interfaces
- tests: Test cases
- configs.php: App configurations
- routes.php: All app routes
Configurations
All the configurations can be modified/added through configs.php
- Database
- host
- user
- password
- db
- port
- socket
- Environment
- debug: true on production, false on development
- url: url of production/development server
- Authorization
- None: No Authorization
- Basic: Authorization with username & password
- Bearer: Authorization with token
- Digest: Authorization with encrypted username & password
- Header: Custom header authorization
Usage
After installing add your routes in routes.php
[
'welcome' => [
'class' => \PHP7API\Components\Welcome::class,
'method' => 'welcome'
],
'welcome_args' => [
'class' => \PHP7API\Components\Welcome::class,
'method' => 'welcomeArgs',
'requires' => [
'param_1',
'param_2',
]
],
]
Where welcome is route name and class, method is the what the app will call. The requires makes the given parameters required in current route inputs.
A Basic Component:
namespace PHP7API\Components;
class Welcome extends Component{
public function welcome($request, $route){
return [
'success' => 1,
'message' => 'success'
];
}
}
Which outputs the response:
{"success":1,"message":"success","time":"2020-03-26 21:23:18"}
The "$request" parameter contains all the given input
The "$route" parameter contains current Route information.
Call Api
Using Curl
$ch = curl_init();
$curlData = http_build_query([
'route' => 'welcome_args',
'param_1' => 'value_1',
'param_2' => 'value_2',
]);
$headers = [
'Content-Type: application/json',
'Authorization: Bearer your_token'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
$response = json_decode($server_output, true);
Mysqli Usage
Initiate the object:
$db = MySql::instance();
or
$db = MySql::init();
MySql::instance() uses the db configurations from configs.php
MySql::init() uses the given configurations from arguments or configs.php if no argument is given.
Execute Query:
$result = $db->exec($sql);
$result is true on success, false on query failure.
Execute Fetch Query:
$result = $db->fetch($sql);
$result is MysqlResultCollection object on success, null on query failure.
MysqlResultCollection
All the query results will be stored in this object.
Common Methods:
- getAll() - returns all rows in array
- getRowsNum() - returns rows count
- isEmpty() - checks if result is empty
- first() - Returns first row
- last() - Returns last row
- nth() - Returns nth index of row
The first row can be directly accessed simply by:
$result->first_row_column;
or
$result->first()->first_row_column;
Using Loop:
foreach ($user as $name => $value):
echo $value->column;
endforeach;
Limitation
- This app only uses one url for all the routes
- Only has MySQLi Wrapper yet.
- Doesn't support OAuth1 & OAuth2 yet.
alisajjad/php7_api 适用场景与选型建议
alisajjad/php7_api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 alisajjad/php7_api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alisajjad/php7_api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-26