GithubHelp home page GithubHelp logo

matthiasm11 / teamleaderapiclient Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nascom/teamleaderapiclient

0.0 0.0 0.0 395 KB

PHP Client to connect to the Teamleader API

License: MIT License

PHP 100.00%

teamleaderapiclient's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.