PHP SDK
We’ve created an open source PHP package that allows you to communicate with the Worksome API. It provides some convenient methods to get started.
Getting started
We offer a PHP SDK package that is ready for use. It’s open source, and we accept contributions to it.
Note: The PHP SDK for Worksome requires PHP 8.1 or later.
To install via Composer, you’ll need a PSR-17 and PSR-18 implementation. We suggest using Guzzle.
composer require worksome/sdk guzzlehttp/guzzle:^7.5 http-interop/http-factory-guzzle:^1.2
Next, create an instance of the SDK, and authenticate. This takes your API token as a parameter for the authenticate()
method. If you don’t have an API token yet, you can read up on how to create one first.
$apiToken = '...';
$worksome = new \Worksome\Sdk\Client();
$worksome->authenticate($apiToken);
Once you have initialised the SDK, you can call any of the methods, or directly call the Worksome API.
User Info
You can retrieve your authenticated users’ global id using the convenient viewer()
helpers. Behind the scenes, this uses the viewer
query.
$viewerId = $worksome->viewer()->id();
You can also retrieve a list of the accounts that your API token has access to.
$accounts = $worksome->viewer()->accounts();
This will return an array of arrays with an id
and a name
value.
foreach ($accounts as $account) {
var_dump($accounts[0]['id']); // The global id of the account
var_dump($accounts['name']); // The name of the account
}
GraphQL
You can also call any GraphQL queries or mutations through the graphql()
helpers.
For example, to retrieve the same id as in the viewer()->id()
call, you could run the following:
$result = $worksome->graphql()->execute(
<<<'GQL'
{
viewer {
id
}
}
GQL
);
The SDK also supports passing variables for the GraphQL query via the second $variables
parameter. For example:
$result = $worksome->graphql()->execute('...', [
'id' => '...',
]);
The SDK also supports loading a GraphQL query from a file. This is useful for calling long queries. You can do this using the following:
$result = $worksome->graphql()->fromFile(__DIR__.'/custom-query.graphql');