{"id":15307252,"url":"https://github.com/samsonasik/apigilityconsumer","last_synced_at":"2025-04-14T23:32:22.012Z","repository":{"id":49287569,"uuid":"69756972","full_name":"samsonasik/ApigilityConsumer","owner":"samsonasik","description":":leaves: Zend Framework/Laminas and Expressive/Mezzio Apigility/Laminas API Tools Client Module to consume API Services","archived":false,"fork":false,"pushed_at":"2024-03-11T06:47:36.000Z","size":830,"stargazers_count":16,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T12:21:19.489Z","etag":null,"topics":["api","basic","digest","laminas","laminas-api","mezzio","oauth","zf2","zf3","zf3-apigility-client"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samsonasik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"samsonasik"}},"created_at":"2016-10-01T18:48:58.000Z","updated_at":"2023-02-06T19:59:45.000Z","dependencies_parsed_at":"2023-01-22T21:15:17.106Z","dependency_job_id":null,"html_url":"https://github.com/samsonasik/ApigilityConsumer","commit_stats":null,"previous_names":[],"tags_count":47,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samsonasik%2FApigilityConsumer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samsonasik%2FApigilityConsumer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samsonasik%2FApigilityConsumer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samsonasik%2FApigilityConsumer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samsonasik","download_url":"https://codeload.github.com/samsonasik/ApigilityConsumer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248979115,"owners_count":21192903,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","basic","digest","laminas","laminas-api","mezzio","oauth","zf2","zf3","zf3-apigility-client"],"created_at":"2024-10-01T08:09:37.427Z","updated_at":"2025-04-14T23:32:21.724Z","avatar_url":"https://github.com/samsonasik.png","language":"PHP","funding_links":["https://github.com/sponsors/samsonasik"],"categories":[],"sub_categories":[],"readme":"ApigilityConsumer\n=================\n\n[![Latest Version](https://img.shields.io/github/release/samsonasik/ApigilityConsumer.svg?style=flat-square)](https://github.com/samsonasik/ApigilityConsumer/releases)\n![ci build](https://github.com/samsonasik/ApigilityConsumer/workflows/ci%20build/badge.svg)\n[![Code Coverage](https://codecov.io/gh/samsonasik/ApigilityConsumer/branch/master/graph/badge.svg)](https://codecov.io/gh/samsonasik/ApigilityConsumer)\n[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat)](https://github.com/phpstan/phpstan)\n[![Downloads](https://poser.pugx.org/samsonasik/apigility-consumer/downloads)](https://packagist.org/packages/samsonasik/apigility-consumer)\n\nLaminas API Tools Client module to consume API Services.\n\n\u003e This is README for version ^4.0 which only support php ^8.0 with laminas-servicemanager v3 and laminas-json v3.\n\n\u003e For version ^3.0 you can read at [version 3 readme](https://github.com/samsonasik/ApigilityConsumer/tree/3.x.x) which only support php ^7.1 with laminas-servicemanager v3 and laminas-json v3.\n\n\u003e For version ^2.0, you can read at [version 2 readme](https://github.com/samsonasik/ApigilityConsumer/tree/2.x.x) which only support php ^7.1 with zend-servicemanager v3 and zend-json v3.\n\n\u003e For version 1, you can read at [version 1 readme](https://github.com/samsonasik/ApigilityConsumer/tree/1.x.x) which still support php ^5.6|^7.0 with zend-servicemanager v2.\n\n\u003e Consider upgrading :)\n\nInstallation\n------------\n\nInstallation of this module uses [composer](https://getcomposer.org/).\n\n```sh\ncomposer require samsonasik/apigility-consumer\n```\n\nFor its configuration, copy `vendor/samsonasik/apigility-consumer/config/apigility-consumer.local.php.dist` to `config/autoload/apigility-consumer.local.php` and configure with your api host url (required), oauth, and/or http auth settings:\n\n```php\nuse Laminas\\Http\\Client as HttpClient;\n\nreturn [\n    'apigility-consumer' =\u003e [\n        'api-host-url' =\u003e 'http://api.host.com',\n\n        // null for default or array of configuration listed at https://docs.zendframework.com/zend-http/client/intro/#configuration\n        'http_client_options' =\u003e null,\n\n        // for oauth\n        'oauth' =\u003e [\n\n            //default selected client\n            'grant_type'    =\u003e 'password', // or client_credentials\n            'client_id'     =\u003e 'foo',\n            'client_secret' =\u003e 'foo_s3cret',\n\n            // multiple clients to be selected\n            'clients' =\u003e [\n                'foo' =\u003e [ // foo is client_id\n                    'grant_type'    =\u003e 'password', // or client_credentials\n                    'client_secret' =\u003e 'foo_s3cret',\n                ],\n                'bar' =\u003e [ // bar is client_id\n                    'grant_type'    =\u003e 'password', // or client_credentials\n                    'client_secret' =\u003e 'bar_s3cret',\n                ],\n            ],\n\n        ],\n\n        // for basic and or digest\n        'auth' =\u003e [\n\n            // default client\n            HttpClient::AUTH_BASIC =\u003e [\n                'username' =\u003e 'foo',\n                'password' =\u003e 'foo_s3cret'\n            ],\n\n            HttpClient::AUTH_DIGEST =\u003e [\n                'username' =\u003e 'foo',\n                'password' =\u003e 'foo_s3cret'\n            ],\n\n            // multiple clients to be selected\n            'clients' =\u003e [\n                'foo' =\u003e [ // foo is key represent just like \"client_id\" to ease switch per-client config\n                    HttpClient::AUTH_BASIC =\u003e [\n                        'username' =\u003e 'foo',\n                        'password' =\u003e 'foo_s3cret'\n                    ],\n\n                    HttpClient::AUTH_DIGEST =\u003e [\n                        'username' =\u003e 'foo',\n                        'password' =\u003e 'foo_s3cret'\n                    ],\n                ],\n                'bar' =\u003e [ // bar is key represent just like \"client_id\" to ease switch per-client config\n                    HttpClient::AUTH_BASIC =\u003e [\n                        'username' =\u003e 'bar',\n                        'password' =\u003e 'bar_s3cret'\n                    ],\n\n                    HttpClient::AUTH_DIGEST =\u003e [\n                        'username' =\u003e 'bar',\n                        'password' =\u003e 'bar_s3cret'\n                    ],\n                ],\n            ],\n\n        ],\n    ],\n];\n```\n\nThen, enable it :\n```php\n// config/modules.config.php\nreturn [\n    'ApigilityConsumer', // \u003c-- register here\n    'Application',\n],\n```\n\nUsing at Mezzio\n------------------------\nYou can use at Mezzio, after set up local `config/autoload/apigility-consumer.local.php` like above, you can copy `config/mezzio.local.php.dist` to `config/autoload/mezzio.local.php`, and you can use it.\n\n\nServices\n--------\n\n**1. ApigilityConsumer\\Service\\ClientAuthService**\n\nIt used for `oauth`, with usage:\n\n```php\nuse ApigilityConsumer\\Service\\ClientAuthService;\n\n$client = $serviceManager-\u003eget(ClientAuthService::class);\n\n$data = [\n    'api-route-segment' =\u003e '/oauth',\n    'form-request-method' =\u003e 'POST',\n\n    'form-data' =\u003e [\n        'username' =\u003e 'foo', // not required if grant_type config = 'client_credentials'\n        'password' =\u003e '123', // not required if grant_type config = 'client_credentials'\n    ],\n];\n$timeout  = 100;\n$clientResult = $client-\u003ecallAPI($data, $timeout);\n```\n\n**Specify Oauth \"client_id\"**\n\nYou can specify what client_id to be used on Http Auth with provide `withClient()`:\n\n```php\n$clientResult = $client-\u003ewithClient('bar') // bar is \"client_id\" defined in clients in oauth config\n                       -\u003ecallAPI($data, $timeout);\n```\n\n**Reset  Oauth \"client_id\"**\n\nWe can re-use the client service and use back default \"client_id\" with `resetClient()`:\n\n```php\n$clientResult = $client-\u003ewithClient('bar') // bar is \"client_id\" defined in clients in auth config\n                       -\u003ecallAPI($data, $timeout);\n\n$clientResultDefault = $client-\u003eresetClient()\n                              -\u003ecallAPI($data, $timeout);\n```\n\n**2. ApigilityConsumer\\Service\\ClientService**\n\nFor general Api Call, with usage:\n\n**a. General RAW Json data**\n\n```php\nuse ApigilityConsumer\\Service\\ClientService;\n\n$data = [\n    'api-route-segment' =\u003e '/api',\n    'form-request-method' =\u003e 'POST',\n\n    'form-data' =\u003e [\n        // fields that will be used as raw json to be sent\n        'foo' =\u003e 'fooValue',\n    ],\n\n    // token type and access token if required\n    'token_type' =\u003e  'token type if required, for example: \"Bearer\"',\n    'access_token' =\u003e 'access token if required',\n];\n\n$client = $serviceManager-\u003eget(ClientService::class);\n\n$timeout  = 100;\n$clientResult = $client-\u003ecallAPI($data, $timeout);\n```\n\n**b. With Upload file**\n\nYou can also do upload with it to upload file to API Service. For example:\n\n```php\nuse ApigilityConsumer\\Service\\ClientService;\n\n$data['api-route-segment']   = '/api';\n$data['form-request-method'] = 'POST';\n\n$data['form-data']           = $request-\u003egetPost()-\u003etoArray();\n$data['form-data']['files']  = $request-\u003egetFiles()-\u003etoArray();\n\n/** data['form-data'] should be containst like the following\n[\n    'regular_key1' =\u003e 'regular_keyValue1',\n    'regular_key2' =\u003e 'regular_keyValue2',\n\n    'files' =\u003e [\n        'file1' =\u003e [\n            'type' =\u003e 'text/csv',\n            'name' =\u003e 'file.csv',\n            'tmp_name' =\u003e '/path/to/tmp/file',\n            'error' =\u003e 'UPLOAD_ERR_OK',\n            'size' =\u003e 123,\n        ],\n        'file2' =\u003e [\n            'type' =\u003e 'text/csv',\n            'name' =\u003e 'file2.csv',\n            'tmp_name' =\u003e '/path/to/tmp/file2',\n            'error' =\u003e 'UPLOAD_ERR_OK',\n            'size' =\u003e 123,\n        ],\n    ],\n]\n*/\n\n$client = $serviceManager-\u003eget(ClientService::class);\n\n$timeout  = 100;\n$clientResult = $client-\u003ecallAPI($data, $timeout);\n```\n\n**With include Http (basic or digest) Authentication**\n\nif api call require authentication for basic or digest, you can apply `-\u003ewithHttpAuthType()`:\n\n```php\nuse Laminas\\Http\\Client as HttpClient;\n\n$clientResult = $client-\u003ewithHttpAuthType(HttpClient::AUTH_BASIC)\n                       -\u003ecallAPI($data, $timeout);\n// OR\n$clientResult = $client-\u003ewithHttpAuthType(HttpClient::AUTH_DIGEST)\n                       -\u003ecallAPI($data, $timeout);\n```\n\nthat will read of specified basic or digest auth config we defined at `config/autoload/apigility-consumer.local.php`.\n\nIf you want to specify custom username and password for the Http Auth on `callAPI()` call, you can specify via `$data`:\n\n```php\nuse Laminas\\Http\\Client as HttpClient;\n\n$data = [\n    'api-route-segment' =\u003e '/api',\n    'form-request-method' =\u003e 'POST',\n\n    'form-data' =\u003e [\n        // fields that will be used as raw json to be sent\n        'foo' =\u003e 'fooValue',\n    ],\n\n    'auth' =\u003e [\n        HttpClient::AUTH_BASIC =\u003e [\n            'username' =\u003e 'foo',\n            'password' =\u003e 'foo_s3cret'\n        ],\n\n        HttpClient::AUTH_DIGEST =\u003e [\n            'username' =\u003e 'foo',\n            'password' =\u003e 'foo_s3cret'\n        ],\n    ],\n];\n\n$clientResult = $client-\u003ewithHttpAuthType(HttpClient::AUTH_BASIC)\n                       -\u003ecallAPI($data, $timeout);\n// OR\n$clientResult = $client-\u003ewithHttpAuthType(HttpClient::AUTH_DIGEST)\n                       -\u003ecallAPI($data, $timeout);\n```\n\n**Specify \"client_id\" on Http Auth**\n\nOn Http Auth, there is no \"client_id\" definition concept. On `ClientService`, they are keys that represent just like \"client_id\" to ease switch client specific http auth.\n\nTo allow You can specify what \"client_id\" to be used on Http Auth with provide `withClient()`:\n\n```php\n$clientResult = $client-\u003ewithClient('bar') // bar is \"client_id\" defined in clients in auth config\n                       -\u003ewithHttpAuthType(HttpClient::AUTH_BASIC)\n                       -\u003ecallAPI($data, $timeout);\n```\n\n**Reset \"client_id\" Http Auth**\n\nWe can re-use the client service and use back default \"client_id\" with `resetClient()`:\n\n```php\n$clientResult = $client-\u003ewithClient('bar') // bar is \"client_id\" defined in clients in auth config\n                       -\u003ewithHttpAuthType(HttpClient::AUTH_BASIC)\n                       -\u003ecallAPI($data, $timeout);\n\n$clientResultDefault = $client-\u003eresetClient()\n                              -\u003ecallAPI($data, $timeout);\n```\n\n**Reset Http Auth Type**\n\nAfter one or both `HttpClient::AUTH_BASIC` or `HttpClient::AUTH_DIGEST` used, we can re-use the client service and use back normal API Call without Http Authentication with apply `-\u003eresetHttpAuthType()`:\n\n```php\n$clientResultWithBasicAuth   = $client-\u003ewithHttpAuthType(HttpClient::AUTH_BASIC)\n                                      -\u003ecallAPI($data1, $timeout);\n$clientResultWithDigestAuth  = $client-\u003ewithHttpAuthType(HttpClient::AUTH_DIGEST)\n                                      -\u003ecallAPI($data2, $timeout);\n\n// RESET IT TO NORMAL WITHOUT HTTP AUTHENTICATION\n$clientResultWithoutHttpAuth = $client-\u003eresetHttpAuthType()\n                                      -\u003ecallAPI($data3, $timeout);\n```\n\nClient Result of callAPI() returned usage\n-----------------------------------------\n\nThe `$clientResult` will be a `ApigilityConsumer\\Result\\ClientResult` or `ApigilityConsumer\\Result\\ClientAuthResult` instance, with this instance, you can do:\n\n```php\n//...\n$clientResult = $client-\u003ecallAPI($data, $timeout);\n\nif (! $clientResult-\u003esuccess) {\n    var_dump($clientResult::$messages);\n} else {\n    var_dump($clientResult-\u003edata);\n}\n```\n\nContributing\n------------\nContributions are very welcome. Please read [CONTRIBUTING.md](https://github.com/samsonasik/ApigilityConsumer/blob/master/CONTRIBUTING.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamsonasik%2Fapigilityconsumer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamsonasik%2Fapigilityconsumer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamsonasik%2Fapigilityconsumer/lists"}