在php+laravel项目中使用Saloon扩展包编写API集成
Laravel  /  管理员 发布于 2年前   519
Saloon 是一个 php语言中Laravel框架软件包,它允许你用漂亮、标准化的语法编写 API 集成。
该软件包的核心是请求对象,它定义了如何从 API 获取特定请求。
例如,要获取 Laravel Forge 服务器,你可以定义一个 GetForgeServerRequest 类:
use App\Http\Saloon\Requests\GetForgeServerRequest;
$request = new GetForgeServerRequest(serverId: '123456');
$request->addHeader('Accept', 'application/json');
$request->addConfig('debug', true);
// This will overwrite all default headers.
$request->setHeaders($array);
// This will overwrite all default configration options.
$request->setConfig($array);
// Send the request and get the response body
$response = $request->send();
$data = $response->json();下面是请求类的样子:
use App\Http\Saloon\Connectors\ForgeConnector;
use Sammyjo20\Saloon\Constants\Saloon;
use Sammyjo20\Saloon\Http\SaloonRequest;
class GetForgeServerRequest extends SaloonRequest
{
protected ?string $method = Saloon::GET;
protected ?string $connector = ForgeConnector::class;
public function defineEndpoint(): string
{
return '/servers/' . $this->serverId;
}
public function __construct(
public string $serverId
){}
}您还可以通过在请求类上定义的 defaultHeaders() 方法为请求定义默认头信息,
并将其与基础连接器头信息合并。
这个软件包为你提供了一个面向对象的流畅系统,
为你更好地组织与外部 API 交互的类提供了约定。
Saloon 可通过 composer 与 Laravel 和任何 PHP 项目(PHP 8 以上)协同工作,
具有以下特点:
1.语法简单,规范了与 API 的交互方式
2.无需担心 Guzzle/Http Facade/cURL
3.在一个地方组织所有 API 集成
4.使用插件轻松添加自己的功能
5.强大的拦截逻辑可自定义响应
6.支持无限定制的 Guzzle 处理程序
7.用于测试的模拟请求(即将推出)
8.与框架无关
您可以在 GitHub 上了解有关此软件包的更多信息,获得完整的安装说明,并查看源代码。
https://github.com/Sammyjo20/Saloontest1 在
opencode + Oh-my-openagent,我的第一个免费的ai编程智能体管家:Sisyphus中评论 test..122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..Zita 在
Google AI Studio升级全栈 vibe coding体验,可直接构建带登录和数据库的应用中评论 111222..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号
