首先在阿里云注册好短信所需要的模板、签名,以及AccessKey。
直接用composer安装阿里云php的sdk,
composer require alibabacloud/client
接着在需要的地方引入
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
然后就可以执行业务逻辑了
public function send_sms()
{
$param= $this->request->param();
$sms_info=DB::name('sms_setting')->where('id',1)->find();
$TemplateParam=array(
'code'=>10,//这里根据模板的变量来设置
);
$TemplateParam=json_encode($TemplateParam);
AlibabaCloud::accessKeyClient($sms_info['accessKeyId'], $sms_info['accessKeySecret'])
->regionId('cn-hangzhou')
->asDefaultClient();
if (isset($param['phone'])) {
try {
$result = AlibabaCloud::rpc()
->product('Dysmsapi')
// ->scheme('https') // https | http
->version('2017-05-25')
->action('SendSms')
->method('POST')
->host('dysmsapi.aliyuncs.com')
->options([
'query' => [
'RegionId' => "cn-hangzhou",
'PhoneNumbers' => $param['phone'],
'SignName' => "这里是签名",
'TemplateCode' => "模板id",
'TemplateParam' => $TemplateParam,
],
])
->request();
print_r($result->toArray());
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}
}
}