价格不是我们的优势,品质永远是我们的追求!
当前位置:首页行业快讯 > 短信接口对接流程
短信接口对接流程
来源:动力思维乐信  时间:2019-07-24 17:36
    在开发网站、app等项目中,经常需要接入短信接口来实现各种短信发送功能,常见的应用场景如用户注册登录短信验证、会员通知提醒等,一般短信接口对接到项目中的流程如下:
 
    短信接口对接具体流程
 
    这里就乐信短信接口为例为大家介绍一下:
 
    ①注册乐信短信平台账户,地址:https://www.lx598.com/acc/x5?i=110792;
 
    ②登录乐信短信平台,完善账户企业信息,设置“短信签名”;
 
    ③联系客户经理,申请开通“短信接口”;
 
    ④查看乐信短信api接口文档(https://www.lx598.com/apitext.html),根据文档相关说明,将短信接口接入到项目中;
 
    下面是php开发语言短信接口接入到项目中的demo示例:

<?php    
// ① 该代码仅供接入动力思维乐信短信接口参考使用,客户可根据实际需要自行编写; 
// ② 支持发送验证码短信、触发通知短信等; 
// ③ 测试验证码短信、通知短信,请用默认的测试模板,默认模板详见接口文档。
 
class SendUtility {
private $_config = array();
 
/**
* 获取相关配置
* Config.php文件中的乐信用户名、密码、签名
*/
public __construct($config) {
$this->_config  =  $config;
}
 
/**
* 拼接请求参数
*/
function BuildContent($AimMobiles, $Content) {
$str  = "accName=" . urlencode($this->_config["UserName"]);
$str .= "&accPwd=" . urlencode(strtoupper(md5($this->_config["Password"])));
$str .= "&aimcodes=" . urlencode($AimMobiles);
$str .= "&content=" . urlencode($Content . $this->_config["Signature"]);
return $str;
}
 
/**
* 短信发送
* @param $AimMobiles 下行手机号
* @param $Content 短信内容
*/
function Send($AimMobiles, $Content) {
$content = $this->BuildContent($AimMobiles, $Content);
$counter = 0;
while ($counter < count($this->_config["Addresses"])) {
$opts = array('http' =>  array("method" => "POST", "timeout" => $this->_config["HttpTimeout"],
"header" => "Content-type: application/x-www-form-urlencoded", "content" => $content));
$message = file_get_contents($this->_config["Addresses"][$counter] . "/send", false,
stream_context_create($opts));
if ($message == false) $counter++;
else break;
}
if ($message == false) return "发送失败";
$RtnString = explode(";", $message);
if ($RtnString[0] != "1") return $RtnString[1];
return $RtnString[0];
}
 
/**
* 余额查询
* @param $accName 用户名
* @param $accPwd 密码
*/
function Query() {
$content  = "accName=" . urlencode($this->_config["UserName"]);
$content .= "&accPwd=" . urlencode(strtoupper(md5($this->_config["Password"])));
$opts = array('http' =>  array("method" => "POST", 
"header" => "Content-type: application/x-www-form-urlencoded", 
"content" => $content));
$message = file_get_contents($this->_config["Addresses"][0] . "/qryBalance", false,
stream_context_create($opts));
if ($message == false) return "查询失败";
$RtnString = explode(";", $message);
if ($RtnString[0] != "1") return $RtnString[1];
return $RtnString[2];
}
 
}
 
php>
 
    了解更多开发语言接入到项目中的demo示例: