freshheads/harvest-api-client

An API client to read and write to Harvest via the Harvest API version 2

1.0.0 2019-01-25 14:48 UTC

This package is auto-updated.

Last update: 2024-04-18 22:03:58 UTC


README

Build Status

This library provides an API client for the Harvest V2 API. Plain PHP classes are used to map the data from the API. JMS Serializer is used for the serialization/deserializtion of this model classes.

It is also perfectly possible to use this Client without using the endpoints and models.

Requirements

Harvest API Client works with PHP 7.1.3 and up. This library depends on the HTTPPlug, see http://docs.php-http.org/en/latest/httplug/introduction.html.

Installation

Harvest API Client can easily be installed using Composer. You must have a php-http/client-implementation compatible client (+ adapter) installed to be able to make requests. You can run the following command, to install Guzzle6 and it's php-http adapter.

composer require 'freshheads/harvest-api-client' 'php-http/guzzle6-adapter'

You can replace php-http/guzzle6-adapter with any other compatible client implementation.

Usage

Instantiate the client and replace the configuration with your personal credentials:

// Use the composer autoloader to load dependencies
require_once 'vendor/autoload.php';

use FH\HarvestApiClient\Client\ClientFactory;

// API Client configuration
$clientConfiguration =
    'access_token' => 'YourAccessToken',
    // Your harvest account ID
    'account_id'    => 12345678,
    // Harvest asks you to customize the user agent header, so that they can contact you in case you're doing something wrong
    'user_agent'   => 'My Application (my@email.com)'
];

// In this example we made use of the Guzzle6 as HTTPClient in combination with an HTTPPlug compatible adapter.
$client = ClientFactory::create([], null, null, $clientConfiguration);

// Make some calls directly via the client
$response = $client->get('/clients', ['page' => 1]);

Endpoints

To use the harvest entity specific Endpoints, you need to install jms/serializer:

composer require 'jms/serializer' 'symfony/yaml'

The serializer needs to know where to find the serialization configuration of the models. The configuration is located in src/Model/configuration. An example is given below:

// Code is based on the example above.

// Creates the serializer and configures it with the serialization configuration
$serializer = SerializerBuilder::create()
      ->addMetadataDir(__DIR__ . '/vendor/freshheads/harvest-api-client/src/Model/configuration')
      ->build();

$harvestClientEndpoint = new ClientEndpoint($client, $serializer);

// List harvest clients (returns an array of Client objects).
$harvestClients = $harvestClientEndpoint->list();