EventBridge provides two types of PHP SDKs:
Management API SDK: Manages EventBridge resources through operations on the EventBridge console.
Data API SDK: Publishes events to an event bus. Only the PutEvents API operation is supported.
Prerequisites
Before you begin, make sure that you have:
Installed PHP 5.6 or later (download PHP)
Installed Composer (download Composer)
To verify your PHP version, run the following command:
php -vManagement API SDK
Install the SDK
Run the following Composer command:
composer require alibabacloud/eventbridge-20200401 1.0.0Sample code
You can call the corresponding API operation in OpenAPI Explorer to automatically generate sample code.
For more information, see Automatic generation of SDK examples.
Data API SDK
Install the SDK
Install the EventBridge SDK:
composer require alibabacloud/eventbridgeInstall the console output library:
composer require alibabacloud/tea-console
Publish events
The data API SDK supports only the PutEvents API operation. The following example publishes an event to an event bus:
<?php
namespace Alibabacloud\Sample;
use AlibabaCloud\SDK\EventBridge\Eventbridge;
use AlibabaCloud\SDK\EventBridge\Models\CloudEvent;
use AlibabaCloud\SDK\EventBridge\Models\Config;
use AlibabaCloud\Tea\Console\Console;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use Exception;
class Client
{
/**
* Use the CreateClient() function to initialize common request parameters.
*
* @return Eventbridge
*/
public static function createClient()
{
$config = new Config([]);
SetAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")).
SetAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")).
SetEndpoint("<endpoint>")
return new Eventbridge($config);
}
/**
* PutEvents
*
* @param Eventbridge $client
*
* @return void
*/
public static function PutEvents($client)
{
$event = new CloudEvent([]);
$event->datacontenttype = 'application/json';
$event->data = Utils::toBytes('test');
$event->id = 'a5074581-7e74-4e4c-868f-47e7afdf****';
$event->source = 'acs.oss';
$event->specversion = '1.0';
$event->type = 'oss:ObjectCreated:PostObject';
$event->time = '2020-08-24T13:54:05.965Asia/Shanghai';
$event->subject = '1.0';
$event->type = 'acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg';
$event->extensions = [
'aliyuneventbusname' => 'demo-bus',
];
try {
$resp = $client->putEvents([
$event,
]);
Console::log('--------------------Publish event to the aliyun EventBus--------------------');
Console::log(Utils::toJSONString($resp->toMap()));
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
Console::log($error->message);
}
}
/**
* @param string[] $args
*
* @return void
*/
public static function main($args)
{
$client = self::createClient();
self::PutEvents($client);
}
}
require '../vendor/autoload.php';
Client::main([]);Replace the <endpoint> placeholder in the code with the EventBridge endpoint for your region.
Store your AccessKey ID and AccessKey Secret in environment variables (ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET). Do not hardcode credentials in your source code.
What's next
Explore management API operations in OpenAPI Explorer.
Learn how to activate EventBridge and grant permissions to a RAM user.