GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

BranchesTags
Open more actions menu

Latest commit

 

History

116 Commits
116 Commits

Folders and files

NameName
Last commit message
Last commit date
examples
examples
 
 
src/HMRC
src/HMRC
 
 
tests
tests
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.styleci.yml
.styleci.yml
 
 
.travis.yml
.travis.yml
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE.md
LICENSE.md
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
composer.lock
composer.lock
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 

Repository files navigation

HMRC API PHP

Build Status StyleCI

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

$request = new \HMRC\Hello\HelloWorldRequest;

// Response is instant of HMRCResponse class
$response = $request->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

// ServerToken is singleton so please use getInstance() method to get an instance and then use set method on it
\HMRC\ServerToken\ServerToken::getInstance()->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

$clientId = 'clientid';
$clientSecret = 'clientsecret';
$serverToken = 'servertoken';

You can gain the access token by create HMRC Oauth2 Provider and redirect to authorize URL (see example/oauth2/create-access-token.php for example).

<?php

$callbackUri = "http://localhost:8080/examples/oauth2/callback.php" ;

$_SESSION[ 'client_id' ] = $_GET[ 'client_id' ];
$_SESSION[ 'client_secret' ] = $_GET[ 'client_secret' ];
$_SESSION[ 'callback_uri' ] = $callbackUri;
$_SESSION[ 'caller' ] = "/examples/index.php";

$provider = new \HMRC\Oauth2\Provider(
    $_GET[ 'client_id' ],
    $_GET[ 'client_secret' ],
    $callbackUri
);
$scope = [ \HMRC\Scope\Scope::VAT_READ, \HMRC\Scope\Scope::HELLO, \HMRC\Scope\Scope::VAT_WRITE ];
$provider->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

$provider = new \HMRC\Oauth2\Provider(
    $_SESSION[ 'client_id' ],
    $_SESSION[ 'client_secret' ],
    $_SESSION[ 'callback_uri' ]
);

// Try to get an access token using the authorization code grant.
$accessToken = $provider->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

$request = new \HMRC\Hello\HelloUserRequest;
$response = $request->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

\HMRC\Environment\Environment::getInstance()->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.

About

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

Topics

api-clientcomposer-packagehmrcphp-library

Resources

Readme
MIT license
Activity

Stars

10 stars

Watchers

3 watching

Forks

4 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.