qualiretraite/teamleader-api-client

用于连接Teamleader API的PHP客户端

v1.0.1 2021-11-30 14:10 UTC

This package is auto-updated.

Last update: 2024-09-29 06:25:50 UTC


README

PHP客户端,用于连接到Teamleader API

安装

此包可通过composer获取

$ composer require nascom/teamleader-api-client

基本用法

依赖项

此包需要支持PHP-HTTP的HTTP客户端。如果您使用Guzzle(guzzlehttp/guzzle),已提供默认实现。

身份验证

Teamleader API使用OAuth2.0进行请求身份验证。我们依赖于PHP league的OAuth2客户端,使用第三方Teamleader提供者来处理。

<?php

use Nascom\OAuth2\Client\Provider\Teamleader;

$provider = new Teamleader([
    'clientId' => 'your-client-id',
    'clientSecret' => 'your-client-secret',
    'redirectUri' => 'http://example.com/your/redirect/uri'
]);

您可以使用此提供者获取访问令牌。请参阅League文档中的示例。

设置客户端

从上一步获取的访问令牌是实例化API客户端所必需的。您还需要一个HTTP客户端。如果您使用guzzle,已提供默认工厂。

<?php

use QR\TeamleaderApiClient\Http\Guzzle\GuzzleApiClientFactory;
use QR\TeamleaderApiClient\Http\ApiClient\ApiClient;

// Instantiate the client using the default Guzzle implementation.
$apiClient = GuzzleApiClientFactory::create(
    $provider,
    $accessToken,
    [ // Optional extra configuration.
        'timeout' => 3.0, 
        'callback' => function($newActionToken) {
            // Do stuff with the new token
            // ...
        }
    ] 
);

// Alternatively, you can use any other PHP-HTTP compatible client of your
// choosing to instantiate the API client.
$apiClient = new ApiClient(
    $provider,
    $yourAlternativeHttpClient,
    $accessToken
);

发送请求

一旦您设置了API客户端,就可以开始发送请求。这是通过将一个Request对象传递给客户端的handle方法来完成的。将返回一个PSR-7 ResponseInterface

所有可用的请求都可以在这里找到这里

<?php

use QR\TeamleaderApiClient\Request\CRM\Companies\CompaniesAddRequest;
use QR\TeamleaderApiClient\Request\CRM\Companies\CompaniesDeleteRequest;
use QR\TeamleaderApiClient\Request\CRM\Companies\CompaniesInfoRequest;
use QR\TeamleaderApiClient\Request\CRM\Companies\CompaniesListRequest;
use QR\TeamleaderApiClient\Request\CRM\Companies\CompaniesUpdateRequest;

// Performing a companies.add request
$company = [
    'name' => 'TeamLeader',
    'emails' => [
        ['type' => 'primary', 'email' => 'sales@teamleader.eu'],
        ['type' => 'invoicing', 'email' => 'invoicing@teamleader.eu'],
    ],
    'language' => 'en',
    'website' => 'https://teamleader.eu/',
];
$request = new CompaniesAddRequest($company);
$response = $apiClient->handle($request);
$linkedCompany = json_decode($response->getBody()->getContents(), true);

// Performing a companies.info request
$request = new CompaniesInfoRequest($linkedCompany['data']['id']);
$response = $apiClient->handle($request);
$company = json_decode($response->getBody()->getContents(), true)['data'];

// Performing a companies.update request
$company['custom_fields'][0]['value'] = 'New Custom Value';
$request = new CompaniesUpdateRequest($company);
$apiClient->handle($request);

// Performing a companies.delete request
$request = new CompaniesDeleteRequest($linkedCompany['data']['id']);
$apiClient->handle($request);

// Performing a companies.list request
$request = new CompaniesListRequest();
$response = $apiClient->handle($request);
$companies = json_decode($response->getBody()->getContents());

// Performing a companies.list request with filters, pagination and sorting
$filters = [
    'email' => [
        'type' => 'primary',
        'email' => 'info@example.org',
    ],
];
$pagination = [
    'size' => 5,
    'number' => 1,
];
$sorting = [
    'name' => 'asc',
    'added_at' => 'desc',
];
$request = new CompaniesListRequest();
$request->setPage($pagination);
$request->setSort($sorting);
$response = $apiClient->handle($request);
$companies = json_decode($response->getBody()->getContents());

使用仓库类

如果您想使用反序列化的模型,可以使用Teamleader类。这充当API的包装器。您需要安装Symfony的Serializer组件(symfony/serializer)。

如以下所示实例化teamleader以使用仓库

$teamleader = \QR\TeamleaderApiClient\Teamleader::withDefaultSerializer($apiClient);
<?php

use QR\TeamleaderApiClient\Model\Company\Company;
use QR\TeamleaderApiClient\Model\Aggregate\Email;

// Performing a companies.add request
$company = new Company();
$company->create('TeamLeader');
$emails = [
    new Email('sales@teamleader.eu', 'primary'),
    new Email('invoicing@teamleader.eu', 'invoicing'),
];
$company->setEmails($emails);
$company->setLanguage('en');
$company->setWebsite('https://teamleader.eu/');
$linkedCompany = $teamleader->companies()->addCompany($company);

// Performing a companies.info request
$company = $teamleader->companies()->getCompany($linkedCompany->getId());

// Performing a companies.update request
$customFields = $company->getCustomFields();
$customFields[0]->setValue('New custom value');
$company->setCustomFields($customFields);
$teamleader->companies()->updateCompany($company);

// Performing a companies.delete request
$teamleader->companies()->deleteCompany($linkedCompany->getId());

// Performing a companies.list request
$companies = $teamleader->companies()->listCompanies();

// Performing a companies.list request with filters, pagination and sorting
$filters = [
    'email' => [
        'type' => 'primary',
        'email' => 'info@example.org',
    ],
];
$pagination = [
    'size' => 5,
    'number' => 1,
];
$sorting = [
    'name' => 'asc',
    'added_at' => 'desc',
];
$companies = $teamleader->companies()->listCompanies($filters, $pagination, $sorting);

已知问题

timeTracking.info请求导致'400 Bad Request'响应:{"errors":[{"code":0,"title":"Key id must be present","status":400,"meta":{"field":"id"}}]}