PHP code example of s-patompong / hmrc-api-php

1. Go to this page and download the library: Download s-patompong/hmrc-api-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

s-patompong / hmrc-api-php example snippets




$request = new \HMRC\Hello\HelloWorldRequest;

// Response is instant of HMRCResponse class
$response = $request->fire();

return $response->getBody();



// 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();



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



$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);



$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;



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

return $response->getBody();



\HMRC\Environment\Environment::getInstance()->setToLive();