divineomega/cachetphp

cachet.php是Cachet状态页面的PHP客户端库

v0.5 2021-04-25 13:39 UTC

This package is auto-updated.

Last update: 2024-08-25 21:26:24 UTC


README

cachet.php是Cachet状态页面的PHP客户端库 (https://cachethq.io/)。

这个库可以在其他系统中显示您的Cachet状态页面的详细信息,例如监控仪表板、客户票务系统或任何其他Web应用程序。

如果您想使用Composer获取cachet.php,请查看Packagist: https://packagist.org.cn/packages/divineomega/cachetphp

快速开始

开始之前,请通过Composer安装cachet.php库。

创建CachetInstance对象

现在,您需要创建一个表示Cachet安装的CachetInstance对象。您可以这样做

require_once 'vendor/autoload.php';

use \DivineOmega\CachetPHP\Factories\CachetInstanceFactory;

// The API token for the demo instance is 9yMHsdioQosnyVK4iCVR.
$cachetInstance = CachetInstanceFactory::create('https://demo.cachethq.io/api/v1/', '9yMHsdioQosnyVK4iCVR');

检索Cachet元素

检索Cachet实例的各个元素中的数据很容易。只需在您的$cachetInstance对象上调用适当的getter方法。Cachet安装将被联系,并返回一个适当的请求对象数组。

$components = $cachetInstance->getAllComponents();         // Components
$incidents = $cachetInstance->getAllIncidents();           // Incidents
$incidentUpdates = $incidents[0]->getAllIncidentUpdates(); // Incident Updates (Cachet 2.4.0 or above required)
$metrics = $cachetInstance->getAllMetrics();               // Metrics
$metricPoints = $metrics[0]->getAllMetricPoints();         // Metric Points
$subscribers = $cachetInstance->getAllSubscribers();       // Subscribers

要按ID检索单个Cachet元素,您可以使用类似以下方法的代码。

// Get incident by id
$incident = $cachetInstance->getIncidentById($incidentId);

排序Cachet元素

如果您想对结果进行排序,可以使用以下语法。这适用于组件、事件、指标、指标点和订阅者。

// Get components sorted by name ascending
$components = $cachetInstance->getAllComponents('name', 'asc');

从Cachet元素对象中读取

从检索到的Cachet元素对象中读取数据很容易。只需访问它们的公共成员变量。

以下是一个输出Cachet组件的id、名称、描述和状态的示例。

// Get components
$components = $cachetInstance->getAllComponents();

// Display components
foreach ($components as $component) {
    echo $component->id.' - '.$component->name.' - '.$component->description.' - '.$component->status;
    echo "<br/>";
}

有关每种类型Cachet元素的可用变量的信息,请参阅官方Cachet文档

创建Cachet元素

您可以使用cachet.php库非常容易地创建Cachet元素,例如组件或事件。这涉及创建Cachet元素的详细信息的关联数组,然后将此数组传递到相关的创建方法。

以下示例显示了如何创建一个简单的测试组件。

$componentDetails = ['name' => 'Test Component '.rand(1, 99999), 'status' => 1];

$component = $cachetInstance->createComponent($componentDetails);

echo $component->id.' - '.$component->name.' - '.$component->description.' - '.$component->status;

以下更全面的示例显示了如何创建一个事件并将其添加到事件中。请注意,如果您想使用事件更新,需要Cachet 2.4.0或更高版本。

$incidentDetails = ['name' => 'Test Incident '.rand(1, 99999), 'message' => 'Incident message '.rand(1, 99999), 'status' => 1, 'visible' => 1];

$incident = $cachetInstance->createIncident($incidentDetails);

$incidentUpdateDetails = ['status' => 2, 'message' => 'Test incident update '.rand(1, 99999)];

$incidentUpdate = $incident->createIncidentUpdate($incidentUpdateDetails);

echo $incidentUpdate->id.' - '.$incidentUpdate->incident_id.' - '.$incidentUpdate->status.' - '.$incidentUpdate->message;

更新Cachet元素

cachet.php允许您更改Cachet元素并保存这些更改回您的Cachet安装。这是通过直接更改Cachet元素的公共成员变量,然后调用对象的save()方法来完成的。

以下示例显示了如何更改组件的名称和状态,然后保存更改。

// Get components
$components = $cachetInstance->getAllComponents();

// Change component details
$component[0]->name = 'My awesome component';
$component[0]->status = 1;
$component[0]->save();

通过事件和事件更新更新组件状态

如果您创建了一个具有component_id的事件,您还可以包含一个component_status来在创建事件的同时更改组件的状态。以下是一个示例。

$incidentDetails = ['name' => 'Test Incident '.rand(1, 99999), 'message' => 'Incident message '.rand(1, 99999), 'status' => 1, 'visible' => 1,
    'component_id' => 1, 'component_status' => 1];

$incident = $cachetInstance->createIncident($incidentDetails);

在创建事件更新时,您还可以指定一个component_status来在创建事件更新时更改组件的状态。以下是一个示例。

$incidentUpdateDetails = ['status' => 2, 'message' => 'Test incident update '.rand(1, 99999), 'component_status' => 2];

$incidentUpdate = $incident->createIncidentUpdate($incidentUpdateDetails);

您还可以通过事件或事件更新来更改组件状态,方法是在相关对象上更改 component_status 并保存,如下面的示例所示。请注意,如果事件是带有相关 component_id 创建的,则此操作才会生效。

$incident->component_status = 2;
$incident->save();
$incidentUpdate->component_status = 3;
$incidentUpdate->save();

删除Cachet元素

要从您的Cachet安装中删除Cachet元素,您只需在适当的Cachet元素对象上调用 delete() 方法。

例如,要删除一个事件,您可以执行以下操作。

// Get incidents
$incidents = $cachetInstance->getAllIncidents();

// Delete the first one
$incidents[0]->delete();

联系方式

如果您发现了一个错误或新功能,请 在GitHub上报告为问题

对于其他查询,您可以在Twitter上联系我(Jordan Hall)。