2017 © Pedro Peláez
 

library api-library

Mautic API Connector Library

image

mautic/api-library

Mautic API Connector Library

  • Thursday, May 31, 2018
  • by dbhurley
  • Repository
  • 27 Watchers
  • 67 Stars
  • 75,029 Installations
  • PHP
  • 11 Dependents
  • 0 Suggesters
  • 49 Forks
  • 24 Open issues
  • 11 Versions
  • 16 % Grown

The README.md

codecov Latest Stable Version Total Downloads Latest Unstable Version License All Contributors , (*1)

Using the Mautic API Library

Requirements

  • PHP 8.0 or newer

Installing the API Library

You can install the API Library with the following command:, (*2)

composer require mautic/api-library

N.B. Make sure you have installed a PSR-18 HTTP Client before you install this package or install one at the same time e.g. composer require mautic/api-library guzzlehttp/guzzle:^7.3., (*3)

HTTP Client

We are decoupled from any HTTP messaging client with the help of PSR-18 HTTP Client. This requires an extra package providing psr/http-client-implementation. To use Guzzle 7, for example, simply require guzzlehttp/guzzle:, (*4)

``` bash composer require guzzlehttp/guzzle:^7.3, (*5)


The installed HTTP Client is auto-discovered using [php-http/discovery](https://packagist.org/providers/php-http/discovery), but you can also provide your own HTTP Client if you like. ```php <?php // Bootup the Composer autoloader include __DIR__ . '/vendor/autoload.php'; use GuzzleHttp\Client; use Mautic\Auth\ApiAuth; // Initiate an HTTP Client $httpClient = new Client([ 'timeout' => 10, ]); // Initiate the auth object $initAuth = new ApiAuth($httpClient); $auth = $initAuth->newAuth($settings); // etc.

Mautic Setup

The API must be enabled in Mautic. Within Mautic, go to the Configuration page (located in the Settings menu) and under API Settings enable Mautic's API. If you intend to use Basic Authentication, ensure you enable it. You can also choose which OAuth protocol to use here. After saving the configuration, go to the API Credentials page (located in the Settings menu) and create a new client. Enter the callback/redirect URI that the request will be sent from. Click Apply, then copy the Client ID and Client Secret to the application that will be using the API., (*6)

Authorization

Obtaining an access token

The first step is to obtain authorization. Mautic supports OAuth 1.0a and OAuth 2, however it is up to the administrator to decide which is enabled. Thus it is best to have a configuration option within your project for the administrator to choose what method should be used by your code., (*7)

<?php

// Bootup the Composer autoloader
include __DIR__ . '/vendor/autoload.php';  

use Mautic\Auth\ApiAuth;

session_start();

$publicKey = '';
$secretKey = '';
$callback  = '';

// ApiAuth->newAuth() will accept an array of Auth settings
$settings = [
    'baseUrl'          => '',       // Base URL of the Mautic instance
    'version'          => 'OAuth2', // Version of the OAuth can be OAuth2 or OAuth1a. OAuth2 is the default value.
    'clientKey'        => '',       // Client/Consumer key from Mautic
    'clientSecret'     => '',       // Client/Consumer secret key from Mautic
    'callback'         => '',       // Redirect URI/Callback URI for this script
];

/*
// If you already have the access token, et al, pass them in as well to prevent the need for reauthorization
$settings['accessToken']        = $accessToken;
$settings['accessTokenSecret']  = $accessTokenSecret; //for OAuth1.0a
$settings['accessTokenExpires'] = $accessTokenExpires; //UNIX timestamp
$settings['refreshToken']       = $refreshToken;
*/

// Initiate the auth object
$initAuth = new ApiAuth();
$auth     = $initAuth->newAuth($settings);

// Initiate process for obtaining an access token; this will redirect the user to the $authorizationUrl and/or
// set the access_tokens when the user is redirected back after granting authorization

// If the access token is expired, and a refresh token is set above, then a new access token will be requested

try {
    if ($auth->validateAccessToken()) {

        // Obtain the access token returned; call accessTokenUpdated() to catch if the token was updated via a
        // refresh token

        // $accessTokenData will have the following keys:
        // For OAuth1.0a: access_token, access_token_secret, expires
        // For OAuth2: access_token, expires, token_type, refresh_token

        if ($auth->accessTokenUpdated()) {
            $accessTokenData = $auth->getAccessTokenData();

            //store access token data however you want
        }
    }
} catch (Exception $e) {
    // Do Error handling
}

Using Basic Authentication Instead

Instead of messing around with OAuth, you may simply elect to use BasicAuth instead., (*8)

Here is the BasicAuth version of the code above., (*9)

<?php

// Bootup the Composer autoloader
include __DIR__ . '/vendor/autoload.php';  

use Mautic\Auth\ApiAuth;

session_start();

// ApiAuth->newAuth() will accept an array of Auth settings
$settings = [
    'userName'   => '',             // Create a new user       
    'password'   => '',             // Make it a secure password
];

// Initiate the auth object specifying to use BasicAuth
$initAuth = new ApiAuth();
$auth     = $initAuth->newAuth($settings, 'BasicAuth');

// Nothing else to do ... It's ready to use.
// Just pass the auth object to the API context you are creating.

Note: If the credentials are incorrect an error response will be returned., (*10)

 [
    'errors' => [
        [
            'code'    => 403,
            'message' => 'access_denied: OAuth2 authentication required',
            'type'    => 'access_denied',
        ],
    ],
 ];

API Requests

Now that you have an access token and the auth object, you can make API requests. The API is broken down into contexts., (*11)

Get a context object

<?php

use Mautic\MauticApi;

// Create an api context by passing in the desired context (Contacts, Forms, Pages, etc), the $auth object from above
// and the base URL to the Mautic server (i.e. http://my-mautic-server.com/api/)

$api        = new MauticApi();
$contactApi = $api->newApi('contacts', $auth, $apiUrl);

Supported contexts are currently:, (*12)

See the developer documentation., (*13)

Retrieving items

All of the above contexts support the following functions for retrieving items:, (*14)

<?php

$response = $contactApi->get($id);
$contact  = $response[$contactApi->itemName()];

// getList accepts optional parameters for filtering, limiting, and ordering
$response      = $contactApi->getList($filter, $start, $limit, $orderBy, $orderByDir);
$totalContacts = $response['total'];
$contact       = $response[$contactApi->listName()];

Creating an item

<?php

$fields = $contactApi->getFieldList();

$data = array();

foreach ($fields as $field) {
    $data[$field['alias']] = $_POST[$field['alias']];
}

// Set the IP address the contact originated from if it is different than that of the server making the request
$data['ipAddress'] = $ipAddress;

// Create the contact
$response = $contactApi->create($data);
$contact  = $response[$contactApi->itemName()];

Editing an item

<?php

$updatedData = [
    'firstname' => 'Updated Name'
];

$response = $contactApi->edit($contactId, $updatedData);
$contact  = $response[$contactApi->itemName()];

// If you want to create a new contact in the case that $contactId no longer exists
// $response will be populated with the new contact item
$response = $contactApi->edit($contactId, $updatedData, true);
$contact  = $response[$contactApi->itemName()];

Deleting an item

<?php

$response = $contactApi->delete($contactId);
$contact  = $response[$contactApi->itemName()];

Error handling

<?php

// $response returned by an API call should be checked for errors
$response = $contactApi->delete($contactId);

if (isset($response['errors'])) {
    foreach ($response['errors'] as $error) {
        echo $error['code'] . ": " . $error['message'];
    }
}

Contributing

Setting up your environment (automatically)

In order to get started quickly, we recommend that you use DDEV which sets things up automatically for you. It clones https://github.com/mautic/mautic, sets up a local instance for you, and connects the API library tests to that instance., (*15)

To get started, run ddev start! Our first-run experience will guide you through the setup., (*16)

Setting up your environment (manually)

If you want to set up your local environment manually, ensure that you copy /tests/local.config.php.dist to /tests/local.config.php, and fill in the required settings. We recommend using the Basic Authentication method to get up and running quickly., (*17)

Unit tests

Configure the unit tests config before running the unit tests. The tests fire real API requests to a Mautic instance., (*18)

  1. Ensure you have set up your local environment using the steps above.
  2. Run composer test to run the tests.

Modify this command to run a specific test: composer test -- --filter testCreateGetAndDelete tests/Api/NotesTest.php, (*19)

Modify this command to run all tests in one class: composer test -- --filter test tests/Api/NotesTest.php, (*20)

Contributors ✨

Thanks goes to these wonderful people (emoji key):, (*21)

, (*22)

Zdeno Kuzmany
Zdeno Kuzmany

💻
dlopez-akalam
dlopez-akalam

💻
mollux
mollux

💻
Martina  Scholz
Martina Scholz

💻
John Linhart
John Linhart

👀
Marinus van Velzen
Marinus van Velzen

💻
Pierre Ammeloot
Pierre Ammeloot

📓
Martin Vooremäe
Martin Vooremäe

💻 ⚠️

, (*23)

This project follows the all-contributors specification. Contributions of any kind welcome!, (*24)

The Versions

31/05 2018

dev-master

9999999-dev

Mautic API Connector Library

  Sources   Download

MIT

The Requires

 

The Development Requires

28/03 2018

dev-send-sms-endpoint

dev-send-sms-endpoint

Mautic API Connector Library

  Sources   Download

MIT

The Requires

 

The Development Requires

27/02 2018

2.12.0

2.12.0.0

Mautic API Connector Library

  Sources   Download

MIT

The Requires

 

The Development Requires

28/08 2017

2.10.0

2.10.0.0

Mautic API Connector Library

  Sources   Download

MIT

The Requires

 

The Development Requires

15/02 2017

2.6.0

2.6.0.0

Mautic API Connector Library

  Sources   Download

MIT

The Requires

 

The Development Requires

12/12 2016

2.4.0

2.4.0.0

Mautic API Connector Library

  Sources   Download

MIT

The Requires

 

The Development Requires

01/12 2016

2.3.0

2.3.0.0

Mautic API Connector Library

  Sources   Download

MIT

The Requires

 

The Development Requires

25/08 2016

2.1.1

2.1.1.0

Mautic API Connector Library

  Sources   Download

MIT

The Requires

 

The Development Requires

22/07 2016

2.0.1

2.0.1.0

Mautic API Connector Library

  Sources   Download

MIT

The Requires

 

The Development Requires

15/07 2016

2.0.0

2.0.0.0

Mautic API Connector Library

  Sources   Download

MIT

The Requires

 

The Development Requires

23/06 2016

1.0.0

1.0.0.0

Mautic API Connector Library

  Sources   Download

MIT

The Requires

 

The Development Requires