阿里云短信服务 singleSendSms

浏览:5943 最后更新:2017-08-21 22:37 分类:类库 关键字: 短信验证码 阿里云 singleSendSms
最近项目使用阿里云短信服务,参照阿里云市场的短信服务里面的PHP示例写了个类。发送短信验证码。
use com\Sms;

$sms = new Sms();
$result = $sms->send($mobile,$verify_code);
$result = json_decode($result,true);
if($result['success']){
$this->output_data('0', (object)array(), '验证码发送成功');//这是项目的返回类,根据自己需要返回信息
}else{
$this->output_data('3', (object)array(), '验证码发送失败');
}

Sms.php 我放在了extend/com下面
<?php
namespace com;

class Sms{

//默认配置
protected $appcode;
protected $host="http://sms.market.alicloudapi.com";
protected $path = "/singleSendSms";

public function __construct() {
$this->appcode = config('sms_appcode');//读取配置的appcode
}

public function send($phoneNo,$verify_code){
$host = $this->host;
$path = $this->path;
$method = "GET";
$appcode = $this->appcode;
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$phone = $phoneNo; //目标手机号,多条记录可以英文逗号分隔
$paramstring = '{"verify_code":"'.$verify_code.'"}';//这是验证码,根据自己的短信模板来设置
$paramstring = urlencode($paramstring);
$querys = "ParamString={$paramstring}&RecNum={$phone}&SignName=XXXX&TemplateCode=SMS_67123456";
//SignName 签名信息 TemplateCode 模板code 购买阿里云的短信服务这些都在控制台得到
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILonerror, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
if (1 == strpos("$".$host, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$ret = curl_exec($curl);
curl_close($curl);
return $ret;
}

}
评论( 相关
后面还有条评论,点击查看>>