gentor/mautic-api-laravel

Laravel 4/5.x 上 Mautic API 的包装器

dev-master 2022-11-11 13:39 UTC

This package is auto-updated.

Last update: 2024-09-11 17:25:19 UTC


README

Laravel 4/5.x 上 Mautic API 的包装器

安装

使用 composer 进行安装

composer require gentor/mautic-api-laravel

config/app.php 中添加服务提供者

Gentor\Mautic\MauticServiceProvider::class,

config/app.php 中添加外观别名

Gentor\Mautic\Facades\Mautic::class,

配置

app/config/mautic.php 中更改默认设置

return [
    'baseUrl' => env('MAUTIC_API_URL'),
    'userName' => env('MAUTIC_API_USERNAME'),
    'password' => env('MAUTIC_API_PASSWORD'),
];

用法

  • 创建项目
// Create contact
$fields = Mautic::contacts()->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 = Mautic::contacts()->create($data);
$contact = $response[Mautic::contacts()->itemName()];
// Create company
$fields = Mautic::companies()->getFieldList();

$data = array();

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

// Create the company
$response = Mautic::companies()->create($data);
$contact = $response[Mautic::companies()->itemName()];
// Create contact with companies
$contact = Mautic::contacts()->createWithCompanies([
    'firstname' => 'Mautic',
    'lasttname' => 'Contact',
    'email' => 'contact@email.com',
    'companies' => [
        [
            'companyname' => 'Company 1',
        ],
        [
            'companyname' => 'Company 2',
        ],
    ],
]);
  • 编辑项目
$updatedData = array(
    'firstname' => 'Updated Name'
);

$response = Mautic::contacts()->edit($contactId, $updatedData);
$contact = $response[Mautic::contacts()->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 = Mautic::contacts()->edit($contactId, $updatedData, true);
$contact = $response[Mautic::contacts()->itemName()];
  • 删除项目
$response = Mautic::contacts()->delete($contactId);
$contact = $response[Mautic::contacts()->itemName()];
  • 错误处理
// $response returned by an API call should be checked for errors
$response = Mautic::contacts()->delete($contactId);

if (isset($response['error'])) {
    echo $response['error']['code'] . ": " . $response['error']['message'];
} else {
    // do whatever with the info
}

文档

Mautic API 库

Mautic API 文档