This topic describes how to install the SDK for PHP provided by EventBridge and how to use the SDK for PHP to publish events. Sample code is also provided in this topic.
Overview
EventBridge SDKs are classified into management API SDKs and data API SDKs. Sample code that is used by different types of SDKs varies.
Management API SDKs: the SDKs that are used to perform operations on the EventBridge console.
Data API SDKs: the channel for event data. Only the PutEvents API operation is called by using this type of SDK.
Before you begin
Perform the following operations:
Environment preparations
Environment requirements
PHP 5.6 or later is installed. For more information, visit the download page of PHP.
Install Composer. For more information, visit the download page of Composer.
Check the PHP version
Run the
php -v
command to check the PHP version.
Management API SDKs
Install the SDK for PHP
Run the following command to install the SDK for PHP:
composer require alibabacloud/eventbridge-20200401 1.0.0
Sample code
You can call the corresponding operation in OpenAPI Explorer to automatically generate sample code. For more information, see Automatic generation of SDK examples
Data API SDKs
Install the SDK for PHP
Run the following command to install the SDK for PHP:
composer require alibabacloud/eventbridge
Run the following command to install the output library for PHP:
composer require alibabacloud/tea-console
Sample code
Data API SDKs support only the PutEvents API operation. If you want to use the SDK for PHP to publish one or more events, refer to the following sample code:
<?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([]);