增加docke部署
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
ini_set("display_errors", "on");
|
||||
|
||||
require_once dirname(__DIR__) . '/msg_sdk/vendor/autoload.php';
|
||||
|
||||
require_once __DIR__ . '/lib/TokenGetterForAlicom.php';
|
||||
require_once __DIR__ . '/lib/TokenForAlicom.php';
|
||||
|
||||
use Aliyun\Core\Config;
|
||||
use AliyunMNS\Exception\MnsException;
|
||||
|
||||
// 加载区域结点配置
|
||||
Config::load();
|
||||
|
||||
/**
|
||||
* Class MsgDemo
|
||||
*/
|
||||
class MsgDemo
|
||||
{
|
||||
|
||||
/**
|
||||
* @var TokenGetterForAlicom
|
||||
*/
|
||||
static $tokenGetter = null;
|
||||
|
||||
public static function getTokenGetter() {
|
||||
|
||||
$accountId = "1943695596114318"; // 此处不需要替换修改!
|
||||
|
||||
// TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
|
||||
|
||||
$accessKeyId = "yourAccessKeyId"; // AccessKeyId
|
||||
|
||||
$accessKeySecret = "yourAccessKeySecret"; // AccessKeySecret
|
||||
|
||||
if(static::$tokenGetter == null) {
|
||||
static::$tokenGetter = new TokenGetterForAlicom(
|
||||
$accountId,
|
||||
$accessKeyId,
|
||||
$accessKeySecret);
|
||||
}
|
||||
return static::$tokenGetter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取消息
|
||||
*
|
||||
* @param string $messageType 消息类型
|
||||
* @param string $queueName 在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName<br/>(e.g. Alicom-Queue-xxxxxx-xxxxxReport)
|
||||
* @param callable $callback <p>
|
||||
* 回调仅接受一个消息参数;
|
||||
* <br/>回调返回true,则工具类自动删除已拉取的消息;
|
||||
* <br/>回调返回false,消息不删除可以下次获取.
|
||||
* <br/>(e.g. function ($message) { return true; }
|
||||
* </p>
|
||||
*/
|
||||
public static function receiveMsg($messageType, $queueName, callable $callback)
|
||||
{
|
||||
$i = 0;
|
||||
// 取回执消息失败3次则停止循环拉取
|
||||
while ( $i < 3)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 取临时token
|
||||
$tokenForAlicom = static::getTokenGetter()->getTokenByMessageType($messageType, $queueName);
|
||||
|
||||
// 使用MNSClient得到Queue
|
||||
$queue = $tokenForAlicom->getClient()->getQueueRef($queueName);
|
||||
|
||||
// 接收消息,并根据实际情况设置超时时间
|
||||
$res = $queue->receiveMessage(2);
|
||||
|
||||
// 计算消息体的摘要用作校验
|
||||
$bodyMD5 = strtoupper(md5(base64_encode($res->getMessageBody())));
|
||||
|
||||
// 比对摘要,防止消息被截断或发生错误
|
||||
if ($bodyMD5 == $res->getMessageBodyMD5())
|
||||
{
|
||||
// 执行回调
|
||||
if(call_user_func($callback, json_decode($res->getMessageBody())))
|
||||
{
|
||||
// 当回调返回真值时,删除已接收的信息
|
||||
$receiptHandle = $res->getReceiptHandle();
|
||||
$queue->deleteMessage($receiptHandle);
|
||||
}
|
||||
}
|
||||
|
||||
return; // 整个取回执消息流程完成后退出
|
||||
}
|
||||
catch (MnsException $e)
|
||||
{
|
||||
$i++;
|
||||
echo "ex:{$e->getMnsErrorCode()}\n";
|
||||
echo "ReceiveMessage Failed: {$e}\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 调用示例:
|
||||
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
|
||||
echo "消息接口查阅短信状态报告返回结果:\n";
|
||||
MsgDemo::receiveMsg(
|
||||
// 消息类型,SmsReport: 短信状态报告
|
||||
"SmsReport",
|
||||
|
||||
// 在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName
|
||||
"Alicom-Queue-xxxxxxxx-SmsReport",
|
||||
|
||||
/**
|
||||
* 回调
|
||||
* @param stdClass $message 消息数据
|
||||
* @return bool 返回true,则工具类自动删除已拉取的消息。返回false,消息不删除可以下次获取
|
||||
*/
|
||||
function ($message) {
|
||||
print_r($message);
|
||||
return false;
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
echo "消息接口查阅短信服务上行返回结果:\n";
|
||||
MsgDemo::receiveMsg(
|
||||
// 消息类型,SmsUp: 短信服务上行
|
||||
"SmsUp",
|
||||
|
||||
// 在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName
|
||||
"Alicom-Queue-xxxxxxxx-SmsUp",
|
||||
|
||||
/**
|
||||
* 回调
|
||||
* @param stdClass $message 消息数据
|
||||
* @return bool 返回true,则工具类自动删除已拉取的消息。返回false,消息不删除可以下次获取
|
||||
*/
|
||||
function ($message) {
|
||||
print_r($message);
|
||||
return false;
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 用于接收云通信消息的临时token
|
||||
*
|
||||
* @property string messageType
|
||||
* @property string token
|
||||
* @property int expireTime
|
||||
* @property string tempAccessKey
|
||||
* @property string tempSecret
|
||||
* @property \AliyunMNS\Client client
|
||||
* @property string queue
|
||||
*/
|
||||
|
||||
class TokenForAlicom
|
||||
{
|
||||
|
||||
/**
|
||||
* 设置消息类型
|
||||
* @param string $messageType
|
||||
*/
|
||||
public function setMessageType($messageType)
|
||||
{
|
||||
$this->messageType = $messageType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得消息类型
|
||||
* @return string
|
||||
*/
|
||||
public function getMessageType()
|
||||
{
|
||||
return $this->messageType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置临时token
|
||||
* @param string $token
|
||||
*/
|
||||
public function setToken($token)
|
||||
{
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得临时token
|
||||
* @return string
|
||||
*/
|
||||
public function getToken()
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置过期时间 (unix timestamp)
|
||||
* @param int $expireTime
|
||||
*/
|
||||
public function setExpireTime($expireTime)
|
||||
{
|
||||
$this->expireTime = $expireTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得过期时间 (unix timestamp)
|
||||
* @return int
|
||||
* @return int
|
||||
*/
|
||||
public function getExpireTime()
|
||||
{
|
||||
return $this->expireTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置临时AccessKeyId
|
||||
* @param $tempAccessKey
|
||||
*/
|
||||
public function setTempAccessKey($tempAccessKey)
|
||||
{
|
||||
$this->tempAccessKey = $tempAccessKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得临时AccessKeyId
|
||||
* @return string
|
||||
*/
|
||||
public function getTempAccessKey()
|
||||
{
|
||||
return $this->tempAccessKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置临时AccessKeySecret
|
||||
* @param string $tempSecret
|
||||
*/
|
||||
public function setTempSecret($tempSecret)
|
||||
{
|
||||
$this->tempSecret = $tempSecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得临时AccessKeySecret
|
||||
* @return string
|
||||
*/
|
||||
public function getTempSecret()
|
||||
{
|
||||
return $this->tempSecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置MNS Client
|
||||
* @param \AliyunMNS\Client $client
|
||||
*/
|
||||
public function setClient($client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得MNS Client
|
||||
* @return \AliyunMNS\Client
|
||||
*/
|
||||
public function getClient()
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Queue Name
|
||||
* @param string $queue
|
||||
*/
|
||||
public function setQueue($queue)
|
||||
{
|
||||
$this->queue = $queue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得Queue Name
|
||||
* @return string
|
||||
*/
|
||||
public function getQueue()
|
||||
{
|
||||
return $this->queue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
use Aliyun\Api\Msg\Request\V20170525\QueryTokenForMnsQueueRequest;
|
||||
use Aliyun\Core\Config;
|
||||
use AliyunMNS\Client;
|
||||
use Aliyun\Core\Profile\DefaultProfile;
|
||||
use Aliyun\Core\DefaultAcsClient;
|
||||
use Aliyun\Core\Exception\ClientException;
|
||||
use Aliyun\Core\Exception\ServerException;
|
||||
|
||||
// 加载区域结点配置
|
||||
Config::load();
|
||||
|
||||
/**
|
||||
* 接收云通信消息的临时token
|
||||
*
|
||||
* @property array tokenMap
|
||||
* @property int bufferTime 过期时间小于2分钟则重新获取,防止服务器时间误差
|
||||
* @property string mnsAccountEndpoint
|
||||
* @property \Aliyun\Core\DefaultAcsClient acsClient
|
||||
*/
|
||||
class TokenGetterForAlicom
|
||||
{
|
||||
/**
|
||||
* TokenGetterForAlicom constructor.
|
||||
*
|
||||
* @param string $accountId AccountId
|
||||
* @param string $accessKeyId AccessKeyId
|
||||
* @param string $accessKeySecret AccessKeySecret
|
||||
*/
|
||||
public function __construct($accountId, $accessKeyId, $accessKeySecret)
|
||||
{
|
||||
$endpointName = "cn-hangzhou";
|
||||
$regionId = "cn-hangzhou";
|
||||
$productName = "Dybaseapi";
|
||||
$domain = "dybaseapi.aliyuncs.com";
|
||||
|
||||
$this->tokenMap = [];
|
||||
$this->bufferTime = 2 * 60;
|
||||
DefaultProfile::addEndpoint($endpointName, $regionId, $productName, $domain);
|
||||
$profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
|
||||
$this->acsClient = new DefaultAcsClient($profile);
|
||||
$this->mnsAccountEndpoint = $this->getAccountEndpoint($accountId, $regionId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 配置获取AccountEndPoint
|
||||
*
|
||||
* @param string $accountId AccountId
|
||||
* @param string $region Region
|
||||
* @param bool $secure 是否启用https
|
||||
* @param bool $internal
|
||||
* @param bool $vpc
|
||||
* @return string <ul>
|
||||
* <li>http(s)://{AccountId}.mns.cn-beijing.aliyuncs.com</li>
|
||||
* <li>http(s)://{AccountId}.mns.cn-beijing-internal.aliyuncs.com</li>
|
||||
* <li>http(s)://{AccountId}.mns.cn-beijing-internal-vpc.aliyuncs.com</li>
|
||||
* </ul>
|
||||
*/
|
||||
private function getAccountEndpoint($accountId, $region, $secure=false, $internal=false, $vpc=false)
|
||||
{
|
||||
$protocol = $secure ? 'https' : 'http';
|
||||
$realRegion = $region;
|
||||
if ($internal) {
|
||||
$realRegion .= '-internal';
|
||||
}
|
||||
|
||||
if ($vpc) {
|
||||
$realRegion .= '-vpc';
|
||||
}
|
||||
|
||||
return "$protocol://$accountId.mns.$realRegion.aliyuncs.com";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 远程取Token
|
||||
*
|
||||
* @param string $messageType 消息订阅类型 SmsReport | SmsUp
|
||||
* @return TokenForAlicom|bool
|
||||
*/
|
||||
public function getTokenFromRemote($messageType)
|
||||
{
|
||||
$request = new QueryTokenForMnsQueueRequest();
|
||||
$request->setMessageType($messageType);
|
||||
|
||||
try {
|
||||
$response = $this->acsClient->getAcsResponse($request);
|
||||
// print_r($response);
|
||||
$tokenForAlicom = new TokenForAlicom();
|
||||
$tokenForAlicom->setMessageType($messageType);
|
||||
$tokenForAlicom->setToken($response->MessageTokenDTO->SecurityToken);
|
||||
$tokenForAlicom->setTempAccessKey($response->MessageTokenDTO->AccessKeyId);
|
||||
$tokenForAlicom->setTempSecret($response->MessageTokenDTO->AccessKeySecret);
|
||||
$tokenForAlicom->setExpireTime($response->MessageTokenDTO->ExpireTime);
|
||||
// print_r($tokenForAlicom);
|
||||
return $tokenForAlicom;
|
||||
}
|
||||
catch (ServerException $e) {
|
||||
print_r($e->getErrorCode());
|
||||
print_r($e->getErrorMessage());
|
||||
}
|
||||
catch (ClientException $e) {
|
||||
print_r($e->getErrorCode());
|
||||
print_r($e->getErrorMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 先从tokenMap中取,如果不存在则远程取Token并存入tokenMap
|
||||
*
|
||||
* @param string $messageType 消息订阅类型 SmsReport | SmsUp
|
||||
* @param string $queueName 在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName<br/>(e.g. Alicom-Queue-xxxxxx-SmsReport)
|
||||
* @return TokenForAlicom|bool
|
||||
*/
|
||||
public function getTokenByMessageType($messageType, $queueName)
|
||||
{
|
||||
$tokenForAlicom = null;
|
||||
if(isset($this->tokenMap[$messageType])) {
|
||||
$tokenForAlicom = $this->tokenMap[$messageType];
|
||||
}
|
||||
|
||||
if(null == $tokenForAlicom || strtotime($tokenForAlicom->getExpireTime()) - time() > $this->bufferTime)
|
||||
{
|
||||
$tokenForAlicom =$this->getTokenFromRemote($messageType);
|
||||
|
||||
$client = new Client(
|
||||
$this->mnsAccountEndpoint,
|
||||
$tokenForAlicom->getTempAccessKey(),
|
||||
$tokenForAlicom->getTempSecret(),
|
||||
$tokenForAlicom->getToken()
|
||||
);
|
||||
|
||||
$tokenForAlicom->setClient($client);
|
||||
$tokenForAlicom->setQueue($queueName);
|
||||
|
||||
$this->tokenMap[$messageType] = $tokenForAlicom;
|
||||
}
|
||||
|
||||
return $tokenForAlicom;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user