nascom / teamleader-api-client
PHP client to connect to the Teamleader API
Installs: 54 171
Dependents: 0
Suggesters: 0
Security: 0
Stars: 9
Watchers: 7
Forks: 7
Open Issues: 2
Requires
- php: >=5.6
- league/oauth2-client: ^2.3
- nascom/oauth2-teamleader: ^0.1
- php-http/client-common: ^1.0|^2.0
- php-http/client-implementation: ^1.0|^2.0
- php-http/discovery: ^1.0
- php-http/httplug: ^1.1|^2.0
- php-http/message-factory: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- guzzlehttp/psr7: ^1.0
- php-http/message: ^1.0
- php-http/mock-client: ^1.0
- phpspec/phpspec: ^3.2
- symfony/var-dumper: ^4.2
Suggests
- php-http/guzzle6-adapter: ^1.0
- symfony/serializer: Lets you use the Teamleader repository classes
This package is auto-updated.
Last update: 2025-03-16 18:01:11 UTC
README
The updated API client is currently under development at the v2 branch
TeamleaderApiClient
PHP client to connect to the Teamleader API.
Installation
The package is available via composer:
$ composer require nascom/teamleader-api-client
Basic usage
Setup
First, you'll need to provide a client that can make HTTP requests.
It has to implement the HttpClientInterface
. A client using Guzzle
is already available in the package. This requires installing
guzzlehttp/guzzle.
<?php use Nascom\TeamleaderApiClient\Http\HttpClient\GuzzleHttpClient; $guzzle = new \GuzzleHttp\Client(['base_uri' => 'https://www.teamleader.be/api/']); $httpClient = new GuzzleHttpClient($guzzle);
You can use this HttpClient to instantiate the actual API client. You'll have to provide your Teamleader API credentials as well.
<?php use Nascom\TeamleaderApiClient\Http\ApiClient\ApiClient; $teamleaderParameters = [ 'api_group' => '12345', 'api_secret' => 'XXXXXXXXXXXXXXX' ]; $client = new ApiClient( $httpClient, // A client implementing HttpClientInterface. $teamleaderParameters // An array containing the Teamleader credentials. );
Making requests
Every API endpoint has a corresponding Request class. These classes have to
be passed to the client's handle()
method, which will return a Response object.
All available requests can be found here.
For example, here is how you could fetch the details of a project:
<?php use Nascom\TeamleaderApiClient\Request\Project\RetrieveProjectRequest; $projectRequest = new RetrieveProjectRequest(23); $response = $client->handle($projectRequest); echo $response->getData(); // Returns the Teamleader JSON response as a string.
Advanced features
Providing extra options
Extra options can be passed to the ApiClient. These will be merged with some default options, and passed to the HttpClient on making a request.
<?php $client = new ApiClient( $httpClient, $teamleaderParameters, ['connect_timeout' => 5.0] // This will override the default timeout. );
Creating a custom HttpClient
You can create a custom HttpClient to handle requests. All it has to do is
implement the HttpClientInterface
. You could, for example, implement a client
using curl.
<?php use Nascom\TeamleaderApiClient\Http\HttpClient\HttpClientInterface; class CurlHttpClient implements HttpClientInterface { public function request($method, $uri, array $options = []) { $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => 'https://www.teamleader.be/api/' . $uri, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $options['form_params'] )); $response = curl_exec($curl); curl_close($curl); return $response; } } $client = new ApiClient( new CurlHttpCLient(), $teamleaderParameters );