Skip to content

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
oakhope committed Jun 2, 2017
0 parents commit 28a7a7c
Show file tree
Hide file tree
Showing 188 changed files with 20,859 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Created by .ignore support plugin (hsz.mobi)
### Linux template
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### Example user template template
### Example user template

# IntelliJ project files
.idea
*.iml
out
gen
/src/test/aliyun-mns-config.ini
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
AliyunSMS
---------

阿里云短信发送

## 更新记录

* 2017-06-02 `Release v0.0.1` 测试版发布

## 安装

```bash
composer require oakhope/aliyun-sms
```

### 使用

```php
use Oakhope\AliyunSMS;

```

## License
除 “版权所有(C)阿里云计算有限公司” 的代码文件外,遵循 [MIT license](http://opensource.org/licenses/MIT) 开源
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "oakhope/aliyun-sms",
"type": "library",
"description": "阿里云短信发送",
"keywords": ["AliyunSMS", "aliyun", "SMS"],
"homepage": "https://github.com/oakhope/aliyun-sms",
"license": "MIT",
"require": {
"php": ">=7.0.0"
},
"authors": [
{
"name": "Benji Wang",
"email": "oak.hope@gmail.com"
}
],
"autoload": {
"files": [
"src/AliyunSMS.php"
]
}
}
91 changes: 91 additions & 0 deletions src/AliyunSMS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Created by wyq
* Date: 17-6-2
* Time: 下午3:37
*/

namespace Oakhope;

require_once __DIR__.'/aliyun-mns-php-sdk-1.3.4/mns-autoloader.php';

use Exception;
use AliyunMNS\Client;
use AliyunMNS\Model\BatchSmsAttributes;
use AliyunMNS\Model\MessageAttributes;
use AliyunMNS\Exception\MnsException;
use AliyunMNS\Requests\PublishMessageRequest;

class AliyunSMS
{
private $accessId;
private $accessKey;
private $endPoint;
private $client;
private $topicName;
private $signName;

public function __construct(
$accessId,
$accessKey,
$endPoint,
$topicName,
$signName
) {
/**
* Step 1. 初始化Client
*/
$this->accessId = $accessId;
$this->accessKey = $accessKey;
$this->endPoint = $endPoint; // eg. http://1234567890123456.mns.cn-shenzhen.aliyuncs.com
$this->topicName = $topicName;
$this->signName = $signName;

$this->client = new Client(
$this->endPoint,
$this->accessId,
$this->accessKey
);

}

public function sendOne($templateCode, $phone, array $templateParam = ['' => ''], $messageBody = 'empty') {

/**
* Step 2. 获取主题引用
*/
$topic = $this->client->getTopicRef($this->topicName);
/**
* Step 3. 生成SMS消息属性
*/
// 3.1 设置发送短信的签名(SMSSignName)和模板(SMSTemplateCode)
$batchSmsAttributes = new BatchSmsAttributes($this->signName, $templateCode);
// 3.2 (如果在短信模板中定义了参数)指定短信模板中对应参数的值

$batchSmsAttributes->addReceiver($phone, $templateParam);

$messageAttributes = new MessageAttributes(array($batchSmsAttributes));
/**
* Step 4. 设置SMS消息体(必须)
*
* 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
*/
// $messageBody = "smsmessage";

/**
* Step 5. 发布SMS消息
*/
$request = new PublishMessageRequest($messageBody, $messageAttributes);
try
{
$res = $topic->publishMessage($request);
return $res->isSucceed();
}
catch (MnsException $e)
{
throw new Exception($e->getMessage().' [code:'.$e->getMnsErrorCode().']');
}
}


}
29 changes: 29 additions & 0 deletions src/aliyun-mns-php-sdk-1.3.4/AliyunMNS/AsyncCallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace AliyunMNS;

use AliyunMNS\Exception\MnsException;
use AliyunMNS\Responses\BaseResponse;

class AsyncCallback
{
protected $succeedCallback;
protected $failedCallback;

public function __construct(callable $succeedCallback, callable $failedCallback)
{
$this->succeedCallback = $succeedCallback;
$this->failedCallback = $failedCallback;
}

public function onSucceed(BaseResponse $result)
{
return call_user_func($this->succeedCallback, $result);
}

public function onFailed(MnsException $e)
{
return call_user_func($this->failedCallback, $e);
}
}

?>
Loading

0 comments on commit 28a7a7c

Please sign in to comment.