本文介绍如何使用PHP SDK创建一个事件规则。

示例代码如下:

<?php

namespace Alibabacloud\Sample;

use AlibabaCloud\SDK\EventBridge\Eventbridge;
use AlibabaCloud\SDK\EventBridge\Models\Config;
use AlibabaCloud\SDK\EventBridge\Models\CreateRuleRequest;
use AlibabaCloud\SDK\EventBridge\Models\TargetEntry;
use AlibabaCloud\Tea\Console\Console;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use Exception;

class Client
{
    /**
     * Create client  初始化公共请求参数。
     *
     * @return Eventbridge
     */
    public static function createClient()
    {
        $config = new Config([]);
        $config->accessKeyId = ('accessKeyId');
        $config->accessKeySecret = ('ACCESS_KEY_SECRET');
        $config->endpoint = ('EVENT_BRIDGE_ENDPOINT');

        return new Eventbridge($config);
    }

    /**
     * @param Eventbridge $client
     *
     * @return void
     */
    public static function createEventRuleSample($client)
    {
        try {
            $createEventRuleRequest = new CreateRuleRequest([]);
            $targetEntry            = new TargetEntry([]);
            $targetEntry->id        = '1234';
            $targetEntry->endpoint  = 'http://www.baidu.com';
            $targetEntryList        = [
                $targetEntry,
            ];
            // targetEntryList[0] =targetEntry;
            $createEventRuleRequest->ruleName      = 'myRule';
            $createEventRuleRequest->eventBusName  = 'demo-bus';
            $createEventRuleRequest->filterPattern = '{"source":["acs.oss"],"type":["oss:ObjectCreated:UploadPart"]}';
            $createEventRuleRequest->status        = 'enable';
            $createEventRuleRequest->targets       = $targetEntryList;
            $response                              = $client->createRule($createEventRuleRequest);
            Console::log('--------------------create rule success--------------------');
            Console::log(Utils::toJSONString($response->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::createEventRuleSample($client);
    }
}
require '../vendor/autoload.php';
Client::main([]);