Skip to main content

PHP SDK Quickstart

Following the steps described on this page, you'll create a simple PHP command-line application that interacts with the Fatture in Cloud API.

If you want to download the complete working example you can find it here.

0️⃣  Prerequisites

In this guide, we assume that these prerequisites are met:

1️⃣  Step One: Install the Fatture in Cloud SDK

In this quickstart, we'll use the Fatture in Cloud SDK, published to Packagist.

To install it using Composer:

composer require fattureincloud/fattureincloud-php-sdk

2️⃣  Step Two: Set up the OAuth access token retrieval

Create the file oauth.php and copy in the following code:

oauth.php
<?php
require_once(__DIR__ . '/vendor/autoload.php');

use FattureInCloud\OAuth2\OAuth2AuthorizationCodeManager;
use FattureInCloud\OAuth2\Scope;

session_set_cookie_params(86400);
session_start();

$oauth = new OAuth2AuthorizationCodeManager("CLIENT_ID", "CLIENT_SECRET", "http://localhost:8000/oauth.php");

if(isset($_SESSION['token'])) die('You already have an access token');

if(!isset($_GET['code'])) {
$url = $oauth->getAuthorizationUrl([Scope::ENTITY_SUPPLIERS_READ], "EXAMPLE_STATE");
header('location: '.$url);
} else {
$code = $_GET['code'];
$obj = $oauth->fetchToken($code);
if(!isset($obj->error)) {
$_SESSION['token'] = $obj->getAccessToken(); //saving the oAuth access token in a session variable
$_SESSION['refresh'] = $obj->getRefreshToken();
}

echo 'Token saved correctly in the session variable';
}

?>

To make this code work properly the only thing you need to set your client id, client secret and eventually the redirect uri at line 10.

Check the Redirect URL

Make sure your FattureInCloud app redirect URL points to your just created oauth.php file.

Keep your Client Secret safe!

Never share your Client Secret with third-party actors, or publish it to your frontend! If you did, then we suggest to delete your Fatture in Cloud app and to recreate it.

3️⃣  Step Three: Set up the sample

Retrieve your Company ID!

In this example, we'll show how to retrieve your Company ID using the PHP SDK. If you plan to manage only one company, you can insert it directly into your code as a variable. Check the Company-scoped Methods page for more info.

Create a file called quickstart.php in your working directory and copy in the following code:

quickstart.php
<?php

use FattureInCloud\Api\SuppliersApi;
use FattureInCloud\Api\UserApi;
use FattureInCloud\Configuration;
use GuzzleHttp\Client;

require __DIR__ . '/vendor/autoload.php';
session_start();

// Retrieve the access token from the session variable
$accessToken = $_SESSION['token'];

// Get the API config and construct the service object.
$config = FattureInCloud\Configuration::getDefaultConfiguration()->setAccessToken($accessToken);

$userApi = new FattureInCloud\Api\UserApi(
new GuzzleHttp\Client(),
$config
);
$suppliersApi = new FattureInCloud\Api\SuppliersApi(
new GuzzleHttp\Client(),
$config
);

try {
// Retrieve the first company id
$companies = $userApi->listUserCompanies();
$firstCompanyId = $companies->getData()->getCompanies()[0]->getId();

// Retrieve the list of first 10 Suppliers for the selected company
$suppliers = $suppliersApi->listSuppliers($firstCompanyId, null, null, null, 1, 10);
foreach ($suppliers->getData() as $supplier) {
$name = $supplier->getName();
echo("$name </br>n");
}

} catch (Exception $e) {
echo 'Exception when calling the API: ', $e->getMessage(), PHP_EOL;
}

4️⃣  Step Four: Run the sample

Now start the webserver using the following command:

php -S localhost:8000

and go to http://localhost:8000/oauth.php (or whatever your URL is), you will be redirected to the Fatture in Cloud login page where you will be asked to grant some permissions, according to what scopes you specified previously. Finally, you will see the success message, the access token will be stored in the session variable ready to be used.

You can now visit http://localhost:8000/quickstart.php (or whatever your URL is) to test the application.

  What now?

In this example, we used a limited set of the available API methods to show how to use our SDK.

If you want to access the full documentation of the available methods and models, you can check the following resources: