Installation Add the bundle via Composer:
composer require artack/mx-api-bundle
Run composer update if needed.
Enable the Bundle
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3/2):
Artack\MxApiBundle\ArtackMxApiBundle::class => ['all' => true],
First API Call Inject the service and use the fluent interface:
use Artack\MxApiBundle\Service\ArtackMxApi;
class MyController extends Controller
{
public function index(ArtackMxApi $mxapi)
{
$response = $mxapi->setPath('Contact')->get();
return response()->json($response);
}
}
Configuration
Publish the default config (if available) and update config/packages/artack_mx_api.yaml:
php bin/console config:dump-reference artack_mx_api
CRUD Operations Use the fluent interface for common API actions:
// Create
$mxapi->setPath('Contact')->setData(['email' => 'test@example.com'])->post();
// Read
$mxapi->setPath('Contact')->get();
// Update
$mxapi->setPath('Contact/{id}')->setData(['status' => 'active'])->put();
// Delete
$mxapi->setPath('Contact/{id}')->delete();
Handling Responses
Parse responses with Symfony’s Serializer:
$response = $mxapi->setPath('Campaign')->get();
$data = $this->get('serializer')->decode($response, 'json');
Authentication Pass credentials via config or dynamically:
# config/packages/artack_mx_api.yaml
artack_mx_api:
api_key: 'your_api_key_here'
api_secret: 'your_api_secret_here'
Or override in code:
$mxapi->setAuth('api_key', 'api_secret');
Error Handling Wrap API calls in try-catch:
try {
$mxapi->setPath('Newsletter')->post();
} catch (\Artack\MxApi\Exception\ApiException $e) {
$this->addFlash('error', $e->getMessage());
}
Http facade for compatibility:
$client = new \Artack\MxApiBundle\Client\BuzzClient(
new \GuzzleHttp\Client(),
$this->app['config']['artack_mx_api']
);
dispatch(new SyncContactsJob($mxapi));
$cacheKey = 'mx_contacts_' . md5('Contact');
$response = Cache::remember($cacheKey, 3600, function () use ($mxapi) {
return $mxapi->setPath('Contact')->get();
});
Deprecated API Endpoints The README warns of "recent changes to the API." Verify endpoints exist via the mailXpert API docs (if available).
Buzz Client Dependency
The bundle uses kriswallsmith/buzz (dev-master). Pin the version in composer.json to avoid breaking changes:
"kriswallsmith/buzz": "dev-master@dev"
Symfony Serializer Version
Requires symfony/serializer:>=2.0,<2.3-dev. Conflicts may arise with newer Symfony versions. Downgrade or fork the bundle if needed.
No Laravel Service Provider
The bundle is Symfony-based. Manually register the service in Laravel’s AppServiceProvider:
$this->app->bind('artack.mxapi', function ($app) {
return new \Artack\MxApi\ArtackMxApi(
new \Artack\MxApiBundle\Client\BuzzClient(
new \GuzzleHttp\Client(),
$app['config']['artack_mx_api']
)
);
});
debug: true in config to log raw API responses:
artack_mx_api:
debug: true
setHeaders() to add custom headers (e.g., Accept: application/json):
$mxapi->setHeaders(['Accept' => 'application/json']);
$mxapi->setPath('System/Info')->get();
Custom Clients
Extend \Artack\MxApiBundle\Client\AbstractClient to support Guzzle or other HTTP clients:
class GuzzleClient extends AbstractClient
{
public function __construct(\GuzzleHttp\Client $client, array $config)
{
$this->client = $client;
$this->config = $config;
}
}
Response Transformers Override response handling in a decorator:
class DecoratedMxApi implements \Artack\MxApi\ArtackMxApiInterface
{
protected $mxapi;
public function __construct(ArtackMxApi $mxapi)
{
$this->mxapi = $mxapi;
}
public function get()
{
$response = $this->mxapi->get();
return $this->transformResponse($response);
}
protected function transformResponse($response)
{
// Custom logic (e.g., normalize data)
return $response;
}
}
Event Listeners Listen for API events (if the bundle dispatches them). Example for Symfony:
# config/services.yaml
services:
App\EventListener\MxApiListener:
tags:
- { name: kernel.event_listener, event: artack.mxapi.response, method: onResponse }
How can I help you explore Laravel packages today?