hyperf 阿里云短信

 

(此方法满足多种发送需求的短信发送组件)

 

阿里云短信配置如下:

第一步

$ composer require "hyperf-libraries/sms"

第二步

$ php bin/hyperf.php vendor:publish hyperf-libraries/sms

第三步

找到configautoload文件夹下sms.php,如下图

 

第四步

配置sms文件

 

第五步

 

第六步

方法内引用

 

完成!!!

 

 

 

如果有下面报错

All the gateways have failed. You can get error details by `$exception->getExceptions()

解决方法:

阿里云短信请检查 阿里云控制台中  访问控制 管理项,是否建立具备短信权限的账号,没有请赋予AliyunDysmsFullAccess” 和 “AliyunDysmsReadOnlyAccess” 两项。

 

可参考----https://github.com/hyperf-libraries/sms

 

 

 

另一种方法

在控制器添加引用的方法

 

 

 

 

 

<?php
declare(strict_types=1);

namespace App\Utils;

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 = [
'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 = [])
{
$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['Action'] = 'SendSms';
return $this->getSign($accessKeySecret, $array);
}

public function phoneNumberJson(string $accessKeySecret, $array = [])
{
$array['Action'] = 'SendBatchSms';
return $this->getSign($accessKeySecret, $array);
}

public function getSendDetails(string $accessKeySecret, $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"
}

 

posted @ 2020-08-26 14:47  温酒书生。  阅读(1117)  评论(0编辑  收藏  举报