Assets


Note

此页面的内容需要进行重大更新。旧页面包含过时且可能不准确的信息。您仍然可以通过 Mautic Developer Documentation archived repository 访问它。

如果您有兴趣帮助开发此页面和其他页面的新内容,请考虑加入文档编写工作。

请阅读 Contributing GuidelinesContributing to Mautic’s documentation 以开始您的贡献。

使用此端点获取有关 Mautic 的 Assets 的详细信息。

使用 Mautic API 库

您可以通过以下方式使用 Mautic API Library 与此 API 进行交互,或者可以使用本文档中描述的各种 HTTP 端点。

<?php
use Mautic\MauticApi;
use Mautic\Auth\ApiAuth;

// ...
$initAuth = new ApiAuth();
$auth     = $initAuth->newAuth($settings);
$apiUrl   = "https://example.com";
$api      = new MauticApi();
$assetApi = $api->newApi("Assets", $auth, $apiUrl);

获取 Asset

<?php

//...
$asset = $assetApi->get($id);

通过 ID 获取单个 Asset。

HTTP 请求

GET /assets/ID

响应

预期响应代码:200

{
    "asset": {
        "id": 1,
        "title": "Product Whitepaper",
        "description": "Some description",
        "alias": "whitepaper",
        "language": "en",
        "isPublished": true,
        "publishUp": "2015-06-07T06:28:27+00:00",
        "publishDown": "2015-06-30T06:28:27+00:00",
        "dateAdded": "2015-06-07T06:28:27+00:00",
        "createdBy": 1,
        "createdByUser": "Rahel Herschel",
        "dateModified": "2015-06-010T09:30:47+00:00",
        "modifiedBy": 1,
        "modifiedByUser": "Rahel Herschel",
        "downloadCount": 10,
        "uniqueDownloadCount": 8,
        "revision": 1,
        "category": {
            "createdByUser": "Yoav Andrysiak",
            "modifiedByUser": "Yoav Andrysiak",
            "id": 19,
            "title": "test",
            "alias": "test",
            "description": null,
            "color": null,
            "bundle": "asset"
        },
        "extension": "pdf",
        "mime": "application/pdf",
        "size": 269128,
        "downloadUrl": "https://example.com/asset/1:whitepaper"
    }
}

Asset 属性

    • Name
      • Type

      • Description

      • id

      • int

      • ID of the Asset

      • title

      • string

      • Title/name of the Asset

      • description

      • string/null

      • Description of the Asset

      • alias

      • string

      • Used to generate the URL for the Asset

      • language

      • string

      • Locale of the Asset

      • isPublished

      • boolean

      • Published state

      • publishUp

      • datetime/null

      • Asset publish date/time

      • publishDown

      • datetime/null

      • Asset unpublish date/time

      • dateAdded

      • datetime

      • Asset creation date/time

      • createdBy

      • int

      • ID of the User that created the Asset

      • createdByUser

      • string

      • Name of the User that created the Asset

      • dateModified

      • datetime/null

      • Asset modified date/time

      • modifiedBy

      • int

      • ID of the User that last modified the Asset

      • modifiedByUser

      • string

      • Name of the User that last modified the Asset

      • downloadCount

      • int

      • Total number of downloads

      • uniqueDownloadCount

      • int

      • Unique number of downloads

      • revision

      • int

      • Revision version

      • category

      • object/null

      • Object with the Category details

      • extension

      • string

      • Extension of the Asset

      • mime

      • string

      • Mime type of the Asset

      • size

      • int

      • File size of the Asset in bytes

      • downloadUrl

      • string

      • Public download URL for the Asset

List assets

<?php
// ...

$assets = $assetApi->getList($searchFilter, $start, $limit, $orderBy, $orderByDir, $publishedOnly, $minimal);

HTTP Request

GET /assets

Query Parameters

Name

Description

search

String or search command to filter entities by

start

Starting row for the entities returned, defaults to 0

limit

Limit number of entities to return, defaults to the system configuration for pagination - default of 30

orderBy

Column to sort by, can use any column listed in the response

orderByDir

Sort direction: asc or desc

publishedOnly

Only return currently published entities

minimal

Return only array of entities without additional lists in it

Response

Expected Response Code: 200


{

“total”: 1, “assets”: [

{

“id”: 1, “title”: “Product Whitepaper”, “description”: “Some description”, “alias”: “whitepaper”, “language”: “en”, “isPublished”: true, “publishUp”: “2015-06-07T06:28:27+00:00”, “publishDown”: “2015-06-30T06:28:27+00:00”, “dateAdded”: “2015-06-07T06:28:27+00:00”, “createdBy”: 1, “createdByUser”: “Wayne Costa”, “dateModified”: “2015-06-010T09:30:47+00:00”, “modifiedBy”: 1, “modifiedByUser”: “Wayne Costa”, “downloadCount”: 10, “uniqueDownloadCount”: 8, “revision”: 1, “category”: null, “extension”: “pdf”, “mime”: “application/pdf”, “size”: 269128, “downloadUrl”: “https://example.com/asset/1:whitepaper

}

]

}

Properties

Same as Get Asset.

Create Asset

<?php

/**
 * Local Asset example
 */
// Upload a local file first
$apiContextFiles = $this->getContext('files');
$apiContextFiles->setFolder('assets');
$fileRequest = array(
    'file' => dirname(__DIR__).'/'.'mauticlogo.png'
);
$response = $apiContextFiles->create($fileRequest);

$data = array(
    'title' => 'Mautic Logo sent as a API request',
    'storageLocation' => 'local',
    'file' => $response['file']['name']
);

$asset = $assetApi->create($data);


/**
 * Remote Asset example
 */
$data = array(
    'title' => 'PDF sent as a API request',
    'storageLocation' => 'remote',
    'file' => 'https://www.mautic.org/media/logos/logo/Mautic_Logo_DB.pdf'
);

$asset = $assetApi->create($data);

Create a new Asset. There are 2 options: local or remote Asset.

HTTP Request

POST /assets/new

POST Parameters

Name

Type

Description

title

string

Asset title

storageLocation

string

Storage location can be local or remote

file

string

Either URL for remote file or filename for local file

Response

Expected Response Code: 201

Properties

Same as Get Asset.

Edit Asset

<?php

$id   = 1;
$data = array(
    'type' => 'general',
);

// Create new a Asset if ID 1 isn't found?
$createIfNotFound = true;

$asset = $assetApi->edit($id, $data, $createIfNotFound);

Edit a new Asset. This supports PUT or PATCH depending on the desired behavior.

PUT 方法会在给定 ID 不存在时创建一个资源,并清除所有现有资源信息,然后添加来自请求的信息。 PATCH 方法如果找不到具有给定 ID 的资源则会失败,并且使用来自请求的值更新资源的字段值。

HTTP 请求

要编辑一个资源,并在未找到该资源时返回 404 错误:

PATCH /assets/ID/edit

要编辑一个资源,如果未找到该资源则创建一个新资源:

PUT /assets/ID/edit

POST 参数

Name

Type

Description

title

string

资源的标题

storageLocation

string

存储位置,可以是本地或远程

file

string

如果是远程文件,则为 URL;如果是本地文件,则为文件名

响应

如果使用 PUT 方法,编辑资源时预期的响应代码为 200`201`(如果已创建)。

如果使用 PATCH 方法,预期的响应代码为 200

属性

Get Asset 相同。

删除资源

<?php

$asset = $assetApi->delete($id);

删除一个资源。如果存储位置是本地,则也会删除本地文件。

HTTP 请求

DELETE /assets/ID/delete

响应

Expected Response Code: 200

属性

Get Asset 相同。