mailcampaigns / api-client
MailCampaigns API client
Requires
- php: ^8.2
- ext-curl: *
- ext-json: *
- symfony/http-client: ^5.1
Requires (Dev)
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.7
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-master
- 3.0.0.x-dev
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.0
- 1.x-dev
- 1.12.4
- 1.12.3
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.1
- 1.11.0
- 1.10.0
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.1
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.3
- 1.0.1
- 1.0.0
- 0.0.1
- dev-codex/avoid-recursive-order-quote-cloning-2.x
- dev-codex/avoid-recursive-order-quote-cloning
- dev-codex/refresh-rejected-access-tokens
- dev-codex/refresh-rejected-access-tokens-1.x
This package is auto-updated.
Last update: 2026-10-26 07:45:56 UTC
README
MailCampaigns PHP API client
The MailCampaigns PHP Api client provides an easy way to communicate with the MailCampaigns RESTful API (v3).
Installation
Install with Composer:
composer require mailcampaigns/api-client
Usage
You'll need the Api url, client key and secret which can be obtained by logging in to your MailCampaigns account (on the Custom Connector page).
Note: This section will be updated soon!
use MailCampaigns\ApiClient\ApiClient; use MailCampaigns\ApiClient\Entity\Customer; $apiClient = ApiClient::create('<url>', '<client_key>', '<client_secret>'); $customerApi = $apiClient->getCustomerApi(); // Example 1 - Retrieve a customer by id: $customer = $customerApi->getById(1234); // Example 2 - Retrieve first three pages of customers: for ($page = 1; $page <= 3; $page++) { $customers = $customerApi->getCollection($page); /** @var Customer $customer */ foreach ($customers as $customer) { // Use customer as an entity (object): print $customer->getEmail() . PHP_EOL; // Output customer as an array. print_r($customer->toArray()); } } // Example 3 - Create a new customer: $customerApi->create( (new Customer) ->setCustomerRef('CUST-8539') ->setEmail('johndoe@hotmail.com') ->setFirstName('John') ->setLastName('Doe') ->setGender('male') ); // Example 4 - Update a customer: $customerApi->update($customer); // Example 5 - Get customer by customer reference: $customer = $customerApi->getByCustomerRef('CUST-8539'); // Example 6 - Get customer by email address: $customer = $customerApi->getByEmail('bob@hotmail.com'); // Example 7 - Delete a customer by id. $customerApi->deleteById(1234);
Using custom fields:
use MailCampaigns\ApiClient\ApiClient; use MailCampaigns\ApiClient\Entity\Customer; use MailCampaigns\ApiClient\Entity\CustomerCustomField; $apiClient = ApiClient::create('<url>', '<client_key>', '<client_secret>'); $customerCustomFieldApi = $apiClient->getCustomerCustomFieldApi(); // Example 1: Add a new custom field to an existing customer. $customer = (new Customer)->setCustomerId(6); // Or load an actual customer first. $customField = (new CustomerCustomField) ->setName('een_extra_veld_2') ->setValue('Waarde voor extra veld 2.') ->setCustomer($customer); $createdCustomField = $customerCustomFieldApi->create($customField); // The created custom field will contain the generated id. print $createdCustomField->getCustomFieldId(); // Example 2: Update an existing custom field. $customField ->setUpdatedAt(new DateTime()) ->setValue('Een aangepaste waarde.'); $updatedCustomField = $customerCustomFieldApi->update($customField); // Example 4: Delete a custom field by its id. $customerCustomFieldApi->deleteById(123);
Token renewal
The client renews access tokens 60 seconds before their expected expiry. If the API nevertheless responds with HTTP 401, it obtains a new token and repeats the same request once, preserving the method, query, headers and JSON body. A second 401 or a token endpoint failure remains an exception. Other HTTP errors and transport failures are not retried.
Run the regression suite with vendor/bin/phpunit. Authentication tests use a
local PHP server and fake tokens; no real API credentials are required.