sdpmlab / anser-action
PHP简单API连接库。
v0.3.10
2023-09-17 15:11 UTC
Requires
- php: ^7.4||^8.0
- guzzlehttp/guzzle: ^7.0
- psr/http-message: ^1.0
README
Anser-Action是一个基于Guzzle7
的库,它提供了使用jQuery Ajax
的callback
设计模式来处理HTTP响应和异常的能力。此外,Anser-Action还能够迅速实现并行连接,让您能够在短时间内处理多个HTTP连接和响应。
安装
要求
- PHP 7.2.5↑
- Composer
- 安装Guzzle7的要求
Composer安装
使用Composer在项目根目录下下载所需的依赖项和库。
composer require sdpmlab/anser-action
快速入门
单个HTTP连接
通过Anser提供的Action
对象,您可以简单地定义HTTP连接。通过设置doneHandler
,您可以在连接成功时设计执行逻辑,然后通过setMeaningData
在Action
对象中存储所需数据。
Action
必须调用do()
方法来执行连接,之后您可以通过getMeaningData()
获取处理后的数据。
require './vendor/autoload.php'; use \SDPMlab\Anser\Service\Action; use \Psr\Http\Message\ResponseInterface; $action = (new Action( "https://datacenter.taichung.gov.tw", "GET", "/swagger/OpenData/4d4847f5-4feb-4e9b-897c-508d2cbe1ed8" ))->doneHandler(function( ResponseInterface $response, Action $runtimeAction ){ $body = $response->getBody()->getContents(); $data = json_decode($body, true); $runtimeAction->setMeaningData($data); }); $data = $action->do()->getMeaningData(); var_dump($data[0]);
错误处理
您可以设置failHandler
回调函数来定义遇到HTTP连接错误、服务器错误或客户端错误时的处理逻辑。
<?php require './vendor/autoload.php'; use \SDPMlab\Anser\Service\Action; use \Psr\Http\Message\ResponseInterface; use \SDPMlab\Anser\Exception\ActionException; $action = (new Action( "https://error.endpoint", "GET", "/dfgdfg" ))->doneHandler(function ( ResponseInterface $response, Action $runtimeAction ) { $body = $response->getBody()->getContents(); $data = json_decode($body, true); $runtimeAction->setMeaningData($data); })->failHandler(function ( ActionException $e ) { if($e->isClientError()){ $e->getAction()->setMeaningData([ "code" => $e->getStatusCode(), "msg" => "client error" ]); }else if ($e->isServerError()){ $e->getAction()->setMeaningData([ "code" => $e->getStatusCode(), "msg" => "server error" ]); }else if($e->isConnectError()){ $e->getAction()->setMeaningData([ "msg" => $e->getMessage() ]); } }); $data = $action->do()->getMeaningData(); var_dump($data);
并发连接
您可以直接使用库提供的ConcurrentAction
类。通过传递多个Action
实体,您将能够快速实现并行连接,并统一获取处理结果。
<?php require './vendor/autoload.php'; use \SDPMlab\Anser\Service\Action; use \Psr\Http\Message\ResponseInterface; use \SDPMlab\Anser\Service\ConcurrentAction; $action1 = (new Action( "https://datacenter.taichung.gov.tw", "GET", "/swagger/OpenData/4d4847f5-4feb-4e9b-897c-508d2cbe1ed8" ))->addOption("query",[ "limit" => "1" ])->doneHandler(function( ResponseInterface $response, Action $runtimeAction ){ $body = $response->getBody()->getContents(); $data = json_decode($body, true); $runtimeAction->setMeaningData($data); }); $action2 = (new Action( "https://datacenter.taichung.gov.tw", "GET", "/swagger/OpenData/bec13df0-4648-41e9-838d-132705a45308" ))->addOption("query",[ "limit" => "1" ])->doneHandler(function( ResponseInterface $response, Action $runtimeAction ){ $body = $response->getBody()->getContents(); $data = json_decode($body, true); $runtimeAction->setMeaningData($data); }); $action3 = (new Action( "https://datacenter.taichung.gov.tw", "GET", "/swagger/OpenData/b81b1fc6-a2f0-406a-a78c-654cc0088782" ))->addOption("query",[ "limit" => "1" ])->doneHandler(function( ResponseInterface $response, Action $runtimeAction ){ $body = $response->getBody()->getContents(); $data = json_decode($body, true); $runtimeAction->setMeaningData($data); }); $concurrent = new ConcurrentAction(); $concurrent->setActions([ "action1" => $action1, "action2" => $action2, "action3" => $action3 ])->send(); var_dump($concurrent->getActionsMeaningData());
集中式连接管理
继承\SDPMlab\Anser\Service\SimpleService
类来管理和抽象类似的连接。这些连接将能够共享基本配置,通过这种设计模式,您可以最大限度地减少代码复制并提高可维护性。
<?php require './vendor/autoload.php'; use \SDPMlab\Anser\Service\Action; use \Psr\Http\Message\ResponseInterface; use \SDPMlab\Anser\Service\SimpleService; class TaichungService extends SimpleService { protected $serviceName = "https://datacenter.taichung.gov.tw"; protected $retry = 1; protected $retryDelay = 1.0; protected $timeout = 2.0; protected $options = [ "query" => [ "limit" => "1" ] ]; public function getAction1(): Action { return $this->getAction("GET", "/swagger/OpenData/4d4847f5-4feb-4e9b-897c-508d2cbe1ed8") ->doneHandler($this->sameDoneHandler()); } public function getAction2(): Action { return $this->getAction("GET", "/swagger/OpenData/bec13df0-4648-41e9-838d-132705a45308") ->doneHandler($this->sameDoneHandler()); } public function getAction3(): Action { return $this->getAction("GET", "/swagger/OpenData/b81b1fc6-a2f0-406a-a78c-654cc0088782") ->doneHandler($this->sameDoneHandler()); } protected function sameDoneHandler(): callable { return function ( ResponseInterface $response, Action $runtimeAction ) { $body = $response->getBody()->getContents(); $data = json_decode($body, true); $runtimeAction->setMeaningData($data); }; } } $taichungService = new TaichungService(); var_dump($taichungService->getAction1()->do()->getMeaningData()); var_dump($taichungService->getAction2()->do()->getMeaningData()); var_dump($taichungService->getAction2()->do()->getMeaningData());