PHPackages                             sdpmlab/anser-action - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. sdpmlab/anser-action

ActiveLibrary[HTTP &amp; Networking](/categories/http)

sdpmlab/anser-action
====================

PHP simple API connection library.

v0.3.10(2y ago)677431MITPHPPHP ^7.4||^8.0

Since Apr 8Pushed 2y agoCompare

[ Source](https://github.com/SDPM-lab/Anser-Action)[ Packagist](https://packagist.org/packages/sdpmlab/anser-action)[ RSS](/packages/sdpmlab-anser-action/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (10)Dependencies (2)Versions (21)Used By (1)

Anser-Action：A simple API connection library for PHP
====================================================

[](#anser-actiona-simple-api-connection-library-for-php)

 [![logo](https://camo.githubusercontent.com/a06ede6c383f13b886d1a8d6224ae2c851e559679cacccb6524e9680e37bfb22/68747470733a2f2f692e696d6775722e636f6d2f327652416349302e706e67)](https://camo.githubusercontent.com/a06ede6c383f13b886d1a8d6224ae2c851e559679cacccb6524e9680e37bfb22/68747470733a2f2f692e696d6775722e636f6d2f327652416349302e706e67)

Anser-Action is a library based on `Guzzle7`, it provides you the ability to handle HTTP responses and exceptions like using the `callback` design pattern of `jQuery Ajax`. Besides, Anser-Action is also able to achieve parallel connection swiftly, enables you to handle multiple HTTP connections and responses in a short time.

Installation
------------

[](#installation)

### Requirements

[](#requirements)

1. PHP 7.2.5↑
2. Composer
3. [Requirements](https://docs.guzzlephp.org/en/stable/overview.html#requirements) for installing Guzzle7

### Composer installation

[](#composer-installation)

Use Composer to download the needed dependencies and libraries under your project root directory.

```
composer require sdpmlab/anser-action

```

Quick Start
-----------

[](#quick-start)

### Single HTTP Connection

[](#single-http-connection)

Through the `Action` object provided by Anser, you can define your HTTP connection straightforwardly. By setting up the `doneHandler`, you can design the execution logic when the connection succeeded, and then store the needed data inside `Action` object by means of `setMeaningData`.

`Action` must call the `do()` method to execute connection, subsequently, you can take out the processed data through `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]);
```

### Error handling

[](#error-handling)

You can set up `failHandler` callback function to define the processing logic when encountering HTTP connection errors, server errors, or the client errors.

```
