最近有项目需要验证手机,于是就找到了阿里云的短信服务,感觉用起来有些麻烦,而且限制挺多的,官方的接口有些受不了...下下来一解压,好几M,不知道里面啥玩意,没看了,于是就自己封装了一个
0x01
首先需要去申请接口,申请短信模板啦,申请短信签名啦balalalal
申请地址:https://dysms.console.aliyun.com/dysms.htm#/overview1
0x02
直接贴代码好了,233
因为我只需要发送短信,不需要其他的东西了,就只封装了发送短信,如果有其他需求可以看官方的接口:https://help.aliyun.com/document_detail/56189.html?spm=5176.doc60127.6.576.r4wrf8
需要其他的操作直接使用AliSms中的request方法,传入参数就可以了
Demo.php
<?php
$akID='xxxx';//$AccessKeyID
$akS='xxxxx';//$AccessKeySecret
$phone="15000000000";//手机号码
$SignName='爱编码的Farmer';//签名
$TemplateCode='SMS_XXXXX';//模板id
$sms=new AliSms($akID,$akS);
echo $sms->sendSms($SignName,$TemplateCode,['code'=>'test'.rand(10,99)],$phone);
AliSms.php
<?php
/**
*============================
* author:Farmer
* time:2017/12/28
* blog:blog.icodef.com
* function:阿里云短信发送
*============================
*/
namespace icf\lib\info;
class AliSms {
private $akID;
private $akSecrt;
public function __construct($AccessKeyID, $AccessKeySecret) {
$this->akID = $AccessKeyID;
$this->akSecrt = $AccessKeySecret;
}
public function sendSms($SignName, $TemplateCode, $TemplateParam, $PhoneNumbers) {
return $this->request([
'Action' => 'SendSms',
'Version' => '2017-05-25',
'RegionId' => 'cn',
'SignName' => $SignName,
'TemplateCode' => $TemplateCode,
'TemplateParam' => json_encode($TemplateParam, JSON_UNESCAPED_UNICODE),
'PhoneNumbers' => $PhoneNumbers
]);
}
public function request($param) {
$req = array_merge([
'AccessKeyId' => $this->akID,
'Timestamp' => gmdate("Y-m-d\TH:i:s\Z"),
'SignatureMethod' => 'HMAC-SHA1',
'SignatureVersion' => '1.0',
'SignatureNonce' => uniqid(mt_rand(0, 0xffff), true),
'Format' => 'JSON'
], $param);
$getParam = $this->getReqString($req);
$Signature = $this->sign($getParam);
$http = new http("https://dysmsapi.aliyuncs.com/?Signature={$Signature}{$getParam}");
$http->https();
$data = $http->get();
return $data;
}
private function getReqString($req) {
ksort($req);
$ret = '';
foreach ($req as $key => $value) {
$ret .= '&' . $this->encode($key) . '=' . $this->encode($value);
}
return $ret;
}
private function sign($param) {
$stringToSign = "GET&%2F&" . $this->encode(substr($param, 1));
$sign = base64_encode(hash_hmac("sha1", $stringToSign, $this->akSecrt . "&", true));
return $this->encode($sign);
}
private function encode($str) {
$res = urlencode($str);
$res = preg_replace("/\+/", "%20", $res);
$res = preg_replace("/\*/", "%2A", $res);
$res = preg_replace("/%7E/", "~", $res);
return $res;
}
}
/**
* Class http
* @package icf\lib\info
*/
class http {
private $curl;
public function __construct($url = '') {
$this->curl = curl_init($url);
curl_setopt($this->curl, CURLOPT_HEADER, 0); //不返回header部分
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出
curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
}
public function setopt($key, $value) {
curl_setopt($this->curl, $key, $value);
}
public function setRedirection($value = 1) {
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, $value);
}
public function __destruct() {
// TODO: Implement __destruct() method.
curl_close($this->curl);
}
public function setCookie($cookie) {
curl_setopt($this->curl, CURLOPT_COOKIE, $cookie);
}
public function setHeader($header) {
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
}
public function setUrl($url) {
curl_setopt($this->curl, CURLOPT_URL, $url);
}
public function https() {
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
}
public function get($url = '') {
if (!empty($url)) {
$this->setUrl($url);
}
curl_setopt($this->curl, CURLOPT_POST, 0);
return $this->access();
}
public function post($url = '', $data = '') {
curl_setopt($this->curl, CURLOPT_POST, 1);
if (!empty($url)) {
$this->setUrl($url);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $url);
}
return $this->access();
}
public function access() {
$response = curl_exec($this->curl);
if ($response == false) return curl_error($this->curl);
return $response;
}
}
文章评论