Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/s-patompong/hmrc-api-php

PHP library to talk to https://developer.service.hmrc.gov.uk/api-documentation
https://github.com/s-patompong/hmrc-api-php

api-client composer-package hmrc php-library

Last synced: about 2 months ago
JSON representation

PHP library to talk to https://developer.service.hmrc.gov.uk/api-documentation

Awesome Lists containing this project

README

        

# HMRC API PHP
[![Build Status](https://travis-ci.org/s-patompong/hmrc-api-php.svg?branch=master)](https://travis-ci.org/s-patompong/hmrc-api-php)
[![StyleCI](https://github.styleci.io/repos/167135773/shield?branch=master)](https://github.styleci.io/repos/167135773)

This library can be used to connect and does operations on HMRC API https://developer.service.hmrc.gov.uk/api-documentation.

## How to use
For global API such as Hello World, you can use HelloWorldRequest class to deal with the API call.

```php
fire();

return $response->getBody();
```

For application-restricted API call such as Hello Application. First set the server token using ServerToken class and then you can use HelloApplicationRequest class to call the API.

```php
set($_GET['server_token']);

$request = new \HMRC\Hello\HelloApplicationRequest;
$response = $request->fire();

return $response->getBody();
```

For user-restricted API call, please see the next section.

## User-Restricted API call
The easiest way to learn about this is by running the local server using `php -S localhost:8080` command at the root of this library. And then navigate to http://localhost:8080/examples/index.php on your browser. Don't forget to setup the credentials inside examples/config.php file.
```php
redirectToAuthorizationURL($scope);
```
After user grant authorize on HMRC authorization page, it will redirect back to `$callbackUri`, which in the example above, the callback.php file.

Content of callback.php
```php
getAccessToken('authorization_code', [
'code' => $_GET['code']
]);

\HMRC\Oauth2\AccessToken::set($accessToken);

header("Location: /examples/index.php");
exit;
```
You need to use `\HMRC\Oauth2\AccessToken` class to get and set access token. The class that do the request will get Access Token from this class.

After get the access token and save it inside `\HMRC\Oauth2\AccessToken`, we can start calling user-restricted API. For example, here is the request to hello user endpoint.
```php
fire();

return $response->getBody();
```
## Change between sandbox and live environment
In default mode, this library will talk with `sandbox` environment of HMRC. If you want to use live environment, you can call it via `Environment` singleton.
```php
setToLive();
```
## Development & Contribution
Contributor is more than welcome to help develop this library, all the important methods should have unit test.

To run test, simply call `composer test` command on terminal.