hyperf 阿里云短信
(此方法满足多种发送需求的短信发送组件)
阿里云短信配置如下:
第一步
$ composer require "hyperf-libraries/sms"
第二步
$ php bin/hyperf.php vendor:publish hyperf-libraries/sms
第三步
找到config下autoload文件夹下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"
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix