<?php
declare(strict_types=1);
namespace Service\ALiSms;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Guzzle\ClientFactory;
class ALiSmsService
{
/**
* @var \Hyperf\Guzzle\ClientFactory
*/
private $clientFactory;
private $url = 'http://dysmsapi.aliyuncs.com/?';
public function __construct(ClientFactory $clientFactory)
{
$this->clientFactory = $clientFactory;
}
private function percentEncode($string) {
$string = urlencode ( $string );
$string = preg_replace ( '/\+/', '%20', $string );
$string = preg_replace ( '/\*/', '%2A', $string );
$string = preg_replace ( '/%7E/', '~', $string );
return $string;
}
protected function getPublicParam()
{
return $params = array (
'Version' => '2017-05-25',
'Timestamp' => gmdate ( 'Y-m-d\TH:i:s\Z' ),
'SignatureVersion' => '1.0',
'SignatureNonce' => uniqid (),
'SignatureMethod' => 'HMAC-SHA1',
'Format' => 'JSON'
);
}
protected function getSign(string $accessKeySecret,$array=array())
{
$params = $this->getPublicParam();
$newArray = array_merge($params,$array);
unset($newArray['Signature']);
ksort ( $newArray );
$canonicalizedQueryString = '';
foreach ( $newArray as $key => $value ) {
$canonicalizedQueryString .= '&' . $this->percentEncode ( $key ) . '=' . $this->percentEncode ( $value );
}
$stringToSign = 'GET&%2F&' . $this->percentencode ( substr ( $canonicalizedQueryString, 1 ) );
$signature = base64_encode ( hash_hmac ( 'sha1', $stringToSign, $accessKeySecret . '&', true ) );
$newArray ['Signature'] = $signature;
$client = $this->clientFactory->create($options=[]);
$url = $this->url . http_build_query ( $newArray );
$result = $client->request('get',$url);
return json_decode($result->getBody()->getContents(),true);
}
public function sendSms(string $accessKeySecret,$array=array())
{
$array['Action']='SendSms';
return $this->getSign($accessKeySecret,$array);
}
public function phoneNumberJson(string $accessKeySecret,$array=array())
{
$array['Action']='SendBatchSms';
return $this->getSign($accessKeySecret,$array);
}
public function getSendDetails(string $accessKeySecret,$array=array())
{
$array['Action']='QuerySendDetails';
return $this->getSign($accessKeySecret,$array);
}
}
傳送單條
$params = array (
'SignName' => '簽名',
'AccessKeyId' => 'xxx',
'TemplateCode' => '模板id',
'PhoneNumbers' => '1xxxxxxx',
'TemplateParam' => '{"code":"1234"}',
'RegionId' => 'cn-beijing',
);
phoneNumberJson(批量傳送)
$params = array (
'PhoneNumberJson'=>json_encode(array('手機號')),
'SignNameJson'=>json_encode(array('簽名')),
'TemplateCode'=>'模板id',
'AccessKeyId' => 'xxxx',
'TemplateParamJson'=>json_encode(array(array('code'=>'0000'))),
);
getSendDetails(查詢傳送記錄)
$params = array (
'CurrentPage'=>'1',
'PageSize'=>'30',
'PhoneNumber'=>'手機號',
'SendDate'=>'20191210',
'AccessKeyId' => 'xxxx',
);
呼叫
$accessKeySecret='xxxx';
$this->ALiSmsService->getSendDetails($accessKeySecret,$params);
返回結果具體引數看阿里雲.返回格式是陣列格式.
array(4) {
["Message"]=>
string(2) "OK"
["RequestId"]=>
string(36) "9FFC1339-249D-4335-9DBE-87843DA315CB"
["BizId"]=>
string(20) "785908076031923306^0"
["Code"]=>
string(2) "OK"
}