anik/apiz
Apiz is a boilerplate for REST API manager
Requires
- php: >=5.5.9
- ext-curl: *
- guzzlehttp/guzzle: ^6.3
- ext-xmlwriter: *
- lib-libxml: >=2.6.20
- nahid/jsonq: ^5.1
Requires (Dev)
- phpunit/phpunit: ^6.2
- symfony/var-dumper: ^3.4
README
APIZ is a PHP API Client Development Kit. You can easily handle all kind of JSON api response by using this package.
Requirements
- PHP >= 5.5.9
Installations
composer require anik/apiz
Configurations
There are no extra configuration for this package.
Usage
Lets make a api service service for https://reqres.in.
Suppose you have to make several api service for your package. Your service directory is
app/Services
. Now we are develop a service for https://reqres.in and make a class file ReqResApiService.php
which is extend by \Apiz\AbstractApi
class.
namespace App\Services; use Apiz\AbstractApi; class ReqResApiService extends AbstractApi { protected function setBaseUrl() { return 'https://reqres.in'; } }
AbstractApi
is a abstract class where setBaseUrl()
is a abstract method.
To get API response from this url they've a prefix 'api' so first we set it with protected property $prefix
namespace App\Services; use Apiz\AbstractApi; class ReqResApiService extends AbstractApi { protected $prefix = 'api'; protected function setBaseUrl() { return 'https://reqres.in'; } }
Now we make a method for get all users info
namespace App\Services; use Apiz\AbstractApi; class ReqResApiService extends AbstractApi { protected $prefix = 'api'; protected function setBaseUrl() { return 'https://reqres.in'; } public function allUsers() { $users = $this->get('/users'); if ($users->getStatusCode() == 200) { return $users; } return false; } }
We use GuzzleHttp for this package. So you can easily use all HTTP verbs
as a magic method. Its totally hassle free. with our all response we return three objects response
, request
and contents
.
You can access all Guzzle response method from this response. We are using magic method to access it from response.
Output
Post Request with Form Params
public function createUser(array $data) { $user = $this->formParams($data) ->post('/create'); if ($user->getStatusCode() == 201) { return $user; } return false; }
List of Parameter Options
formParams(array $params)
headers(array $params)
query(array $params)
allowRedirects(array $params)
auth(string $username, string $password [, array $options])
body(string $contents)
json(array $params)
file(string $name, string $file_path, string $filename [, array $headers])
params(array $params)
List of HTTP verbs
get(string $uri)
post(string $uri)
put(string $uri)
delete(string $uri)
head(string $uri)
options(string $uri)
Extra Methods
getGuzzleClient()
Log request data
Override method logRequest
which receives the array of data that will be sent to the next request. Log will have the sensetive data as well. So, be careful with what you're doing with that method.