PHP code example of sdpmlab / anser-action

1. Go to this page and download the library: Download sdpmlab/anser-action library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sdpmlab / anser-action example snippets




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]);


\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);


\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\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());