PHP code example of xutl / yii2-broadcast

1. Go to this page and download the library: Download xutl/yii2-broadcast library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xutl / yii2-broadcast example snippets


'components' => [
    'broadcast' => [
        'class' => 'xutl\broadcast\Broadcast',
        'endPoint' => 'http://abcdefg.mns.cn-hangzhou.aliyuncs.com/',
        'accessId' => '1234567890',
        'accessKey' => '1234567890',
    ],
    //etc
],

/** @var \xutl\broadcast\Broadcast $broadcast */
$broadcast = Yii::$app->broadcast;

$topicName = 'CreateTopicAndPublishMessageExample';
$broadcast->create($topicName);

$topic = $broadcast->getTopicRef($topicName);
$subscriptionName = "SubscriptionExample";

try{
    $res = $topic->subscribe($subscriptionName,'https://www.baidu.com','test.test');
    echo "SubscriptionCreated! \n";
}catch (MnsException $e){
    echo "CreateSubscriptionFailed: " . $e;
    return;
}

$messageBody = "test";
$res = $topic->publishMessage($messageBody, 'test.test');
var_dump($res);


class ApiController extends \yii\web\Controller
{
    public function actions()
    {
        return [
            /**
             * Returns an access token.
             */
            'callback' => [
                'class' => \xutl\broadcast\TopicAction::classname(),
                'callback'=>[$this, 'callback'],
            ],
        ];
    }
    
    /**
     * 
     */
    public function callback($params)
    {
        print_r($params);
    }
}