Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aliyum SMS API #583

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/sms_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,29 @@ https://github.com/AsamK/signal-cli#readme
$signal_user = '+18881234567';
$signal_config = '<path to signal-cli config folder>';
$signal_cli = '<path to signal-cli binaray>';

Aliyun
------

Provider website: https://www.aliyun.com

Code sample provided in SSP sources:
https://raw.githubusercontent.com/ltb-project/self-service-password/master/lib/smsapi-aliyun.inc.php

Go to lib/aliyun-dysms-php-sdk/api_demo and type:

.. code:: sh

composer install

Then you can enable it in configuration:

.. code:: php

$sms_api_lib = "lib/aliyun-dysms-php-sdk/api_demo/SmsDemo.php";
$accessKeyId = "xxxxx";
$accessKeySecret = "xxxxxx";
$region = "xxxxx"
$endPointName = "xxxxx";
$request->setSignName("xxxxxx");
$request->setTemplateCode("xxxxxxx");
191 changes: 191 additions & 0 deletions lib/aliyun-dysms-php-sdk/api_demo/SmsDemo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php

ini_set("display_errors", "on");

require_once dirname(__DIR__) . '/api_sdk/vendor/autoload.php';

use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use Aliyun\Api\Sms\Request\V20170525\SendBatchSmsRequest;
use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;

// 加载区域结点配置
Config::load();

/**
* Class SmsDemo
*
* 这是短信服务API产品的DEMO程序,直接执行此文件即可体验短信服务产品API功能
* (只需要将AK替换成开通了云通信-短信服务产品功能的AK即可)
* 备注:Demo工程编码采用UTF-8
*/
class SmsDemo
{

static $acsClient = null;

/**
* 取得AcsClient
*
* @return DefaultAcsClient
*/
public static function getAcsClient() {
//产品名称:云通信短信服务API产品,开发者无需替换
$product = "Dysmsapi";

//产品域名,开发者无需替换
$domain = "dysmsapi.aliyuncs.com";

// TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
$accessKeyId = "xxxxx"; // AccessKeyId

$accessKeySecret = "xxxxxx"; // AccessKeySecret

// 暂时不支持多Region
$region = "cn-hangzhou";

// 服务结点
$endPointName = "cn-hangzhou";


if(static::$acsClient == null) {

//初始化acsClient,暂不支持region化
$profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);

// 增加服务结点
DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);

// 初始化AcsClient用于发起请求
static::$acsClient = new DefaultAcsClient($profile);
}
return static::$acsClient;
}

/**
* 发送短信
* @return stdClass
*/
public static function sendSms($mobile,$message) {
// 初始化SendSmsRequest实例用于设置发送短信的参数
$request = new SendSmsRequest();
$message = substr($message,-6);
//可选-启用https协议
//$request->setProtocol("https");

// 必填,设置短信接收号码
$request->setPhoneNumbers("$mobile");

// 必填,设置签名名称,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
$request->setSignName("xxxxxx");

// 必填,设置模板CODE,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
$request->setTemplateCode("xxxxxxx");

// 可选,设置模板参数, 假如模板中存在变量需要替换则为必填项
$request->setTemplateParam(json_encode(array( // 短信模板中字段的值
"code"=>"$message",
), JSON_UNESCAPED_UNICODE));

// 可选,设置流水号
$request->setOutId("yourOutId");

// 选填,上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段)
$request->setSmsUpExtendCode("1234567");

// 发起访问请求
$acsResponse = static::getAcsClient()->getAcsResponse($request);

return $acsResponse;
}

/**
* 批量发送短信
* @return stdClass
*/
public static function sendBatchSms() {

// 初始化SendSmsRequest实例用于设置发送短信的参数
$request = new SendBatchSmsRequest();

//可选-启用https协议
//$request->setProtocol("https");

// 必填:待发送手机号。支持JSON格式的批量调用,批量上限为100个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
$request->setPhoneNumberJson(json_encode(array(
"1500000000",
"1500000001",
), JSON_UNESCAPED_UNICODE));

// 必填:短信签名-支持不同的号码发送不同的短信签名
$request->setSignNameJson(json_encode(array(
"云通信",
"云通信"
), JSON_UNESCAPED_UNICODE));

// 必填:短信模板-可在短信控制台中找到
$request->setTemplateCode("SMS_1000000");

// 必填:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
// 友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
$request->setTemplateParamJson(json_encode(array(
array(
"name" => "Tom",
"code" => "123",
),
array(
"name" => "Jack",
"code" => "456",
),
), JSON_UNESCAPED_UNICODE));

// 可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段)
// $request->setSmsUpExtendCodeJson("[\"90997\",\"90998\"]");

// 发起访问请求
$acsResponse = static::getAcsClient()->getAcsResponse($request);

return $acsResponse;
}

/**
* 短信发送记录查询
* @return stdClass
*/
public static function querySendDetails() {

// 初始化QuerySendDetailsRequest实例用于设置短信查询的参数
$request = new QuerySendDetailsRequest();

//可选-启用https协议
//$request->setProtocol("https");

// 必填,短信接收号码
$request->setPhoneNumber("12345678901");

// 必填,短信发送日期,格式Ymd,支持近30天记录查询
$request->setSendDate("20170718");

// 必填,分页大小
$request->setPageSize(10);

// 必填,当前页码
$request->setCurrentPage(1);

// 选填,短信发送流水号
$request->setBizId("yourBizId");

// 发起访问请求
$acsResponse = static::getAcsClient()->getAcsResponse($request);

return $acsResponse;
}

}


#$response = SmsDemo::sendSms("xxxxx","123456");
#echo "发送短信(sendSms)接口返回的结果:\n";
#print_r($response);
16 changes: 16 additions & 0 deletions lib/aliyun-dysms-php-sdk/api_sdk/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
24 changes: 24 additions & 0 deletions lib/aliyun-dysms-php-sdk/api_sdk/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "aliyuncs/aliyun-dysms-php-sdk",
"description": "Aliyun SMS SDK for PHP",
"keywords": [ "aliyun", "SMS", "sdk" ],
"type": "library",
"license": "Apache-2.0",
"homepage": "https://www.aliyun.com/product/sms",
"authors": [
{
"name": "Aliyuncs",
"homepage": "https://www.aliyun.com"
}
],
"require": {
"php": ">=5.5.0"
},
"require-dev": {},
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"Aliyun\\": "lib/"
}
}
}
19 changes: 19 additions & 0 deletions lib/aliyun-dysms-php-sdk/api_sdk/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Aliyun\Api\Sms\Request\V20170525;

use Aliyun\Core\RpcAcsRequest;

class QueryInterSmsIsoInfoRequest extends RpcAcsRequest
{
function __construct()
{
parent::__construct("Dysmsapi", "2017-05-25", "QueryInterSmsIsoInfo");
$this->setMethod("POST");
}

private $resourceOwnerAccount;

private $countryName;

private $resourceOwnerId;

private $ownerId;

public function getResourceOwnerAccount() {
return $this->resourceOwnerAccount;
}

public function setResourceOwnerAccount($resourceOwnerAccount) {
$this->resourceOwnerAccount = $resourceOwnerAccount;
$this->queryParameters["ResourceOwnerAccount"]=$resourceOwnerAccount;
}

public function getCountryName() {
return $this->countryName;
}

public function setCountryName($countryName) {
$this->countryName = $countryName;
$this->queryParameters["CountryName"]=$countryName;
}

public function getResourceOwnerId() {
return $this->resourceOwnerId;
}

public function setResourceOwnerId($resourceOwnerId) {
$this->resourceOwnerId = $resourceOwnerId;
$this->queryParameters["ResourceOwnerId"]=$resourceOwnerId;
}

public function getOwnerId() {
return $this->ownerId;
}

public function setOwnerId($ownerId) {
$this->ownerId = $ownerId;
$this->queryParameters["OwnerId"]=$ownerId;
}

}
Loading