piggly / php-api-client
A client starter-kit used to interact with any REST API.
Requires
- php: >=7.2
- ext-curl: >=7.2
- ext-json: >=7.2
- monolog/monolog: ^2.2
Requires (Dev)
- php: >=7.3
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-12-03 09:28:37 UTC
README
This open-source library is provided for cases where you need to do APIs calls with large flexibility that officials SDK may not provide out-of-the-box. When using this Starter-Kit, you will be able to make better cURL requests with a smart managing of requests and responses.
Installation
Composer
- At you console, in your project folder, type
composer require piggly/php-api-client
; - Don't forget to add Composer's autoload file at your code base
require_once('vendor/autoload.php);
.
Manual install
- Download or clone with repository with
git clone https://github.com/piggly-dev/php-api-client.git
; - After, goes to
cd /path/to/piggly/php-api-client
; - Install all Composer's dependencies with
composer install
; - Add project's autoload file at your code base
require_once('/path/to/piggly/php-api-client/vendor/autoload.php);
.
Dependencies
The library has the following external dependencies:
- PHP cURL extension;
- PHP JSON extension;
- PHP 7.2+.
How to?
Configuration
This library provides, first, a Configuration
object. You must create it before send any request. You may create many Configuration
objects to provide different environments or you could use .env
files to manages it.
See below all configurations:
Development configurations:
With debug()
method you will be able to enable requests debugging. You also may set a Logger
to log messages across code lifetime. The logger()
method is expecting a Logger
from monolog/monolog
package. Only when a Logger
is set the log()
method will output messages into Logger
.
NOTE: Debug messages will only be output to
Logger
whendebug()
mode is active.
Requests
After configuration is done, you are ready to use Request
object. You may set a Configuration
object to Request
constructor or it will get the default configuration.
Then, you must start with request's methods: delete()
, head()
, get()
, options()
, patch()
,post()
and put()
. After you will be able to manipulate request with methods below:
When your request is done, you need to use call()
method. The call()
method will:
- Throw an
ApiRequestException
, if response fails; - Throw an
ApiResponseException
, if response has a non 2xx response; - Return a
Response
object with all response data and request reference.
Headers
To manage headers, there is a HeaderBag
object that will have all needed methods to better handle with headers.
Samples
You can see very lightweight samples at /samples folder.
Changelog
See the CHANGELOG file for information about all code changes.
Testing the code
This library uses the PHPUnit. We carry out tests of all the main classes of this application.
vendor/bin/phpunit
You must always run tests with PHP 7.2 and greater.
Contributions
See the CONTRIBUTING file for information before submitting your contribution.
Credits
Support the project
Piggly Studio is an agency located in Rio de Janeiro, Brazil. If you like this library and want to support this job, be free to donate any value to BTC wallet 3DNssbspq7dURaVQH6yBoYwW3PhsNs8dnK
❤.
License
MIT License (MIT). See LICENSE.