国际版阿里云短信对接

  • 首先贴一下阿里云国际版登陆网址
https://account.alibabacloud.com/login/login.htm?spm=a212t0.3047821.0.0.166a1b05yDApSQ&lang=zh_CN
  • 文档地址
https://www.alibabacloud.com/help/zh/doc-detail/164745.htm?spm=a2c63.p38356.b99.36.7a0b2cafsr44zk
  • 首先安装阿里云提供的SDK
composer require alibabacloud/client
  • PHP代码
<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
// Download:https://github.com/aliyun/openapi-sdk-php-client
// Usage:https://github.com/aliyun/openapi-sdk-php-client/blob/master/README-CN.md
AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
                        ->regionId('ap-southeast-1') 
                        ->asGlobalClient();
try {
    $result = AlibabaCloud::rpcRequest()
                          ->product('Dysmsapi')
                          ->host('dysmsapi.ap-southeast-1.aliyuncs.com')
                          ->version('2018-05-01')
                          ->action('SendMessageToGlobe')
                          ->method('POST')
                          ->options([
                                        'query' => [
                                            "To" => "62123****8901",
                                            // "From" => "1234567890",
                                            "Message" => "have a test.",
                                        ],
                                    ])
                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}

在这里插入图片描述

阿里云短信对接需要以下步骤: 1. 在阿里云短信控制台中创建签名和模板,获取对应的签名和模板CODE。 2. 在阿里云控制台中获取 AccessKey ID 和 AccessKey Secret。 3. 在代码中调用阿里云短信 API,传入必要的参数,例如短信模板、签名、接收手机号等。 这里提供一个简单的 PHP 代码示例: ```php <?php require_once './aliyun-php-sdk-core/Config.php'; use Aliyun\Core\DefaultAcsClient; use Aliyun\Core\Profile\DefaultProfile; use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest; // 替换成自己的AccessKey信息 $accessKeyId = "your_accessKeyId"; $accessKeySecret = "your_accessKeySecret"; // 设置阿里云短信服务所在的Region,如华东1 $region = "cn-hangzhou"; // 替换成自己的短信签名和模板CODE $signName = "your_signName"; $templateCode = "your_templateCode"; // 发送短信的手机号码,支持多个手机号码,用英文逗号分隔 $phoneNumbers = "your_phoneNumbers"; // 短信模板中的变量替换JSON串,如验证码为:{"code":"123456"} $templateParam = "{\"code\":\"123456\"}"; // 初始化DefaultAcsClient实例并设置参数 $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret); DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com"); $client = new DefaultAcsClient($profile); $request = new SendSmsRequest(); // 设置请求参数 $request->setPhoneNumbers($phoneNumbers); $request->setSignName($signName); $request->setTemplateCode($templateCode); $request->setTemplateParam($templateParam); // 发送短信 $response = $client->getAcsResponse($request); // 输出结果 print_r($response); ?> ``` 注意,以上代码中需要替换成自己的 AccessKey ID、AccessKey Secret、短信签名、模板 CODE、手机号码、短信模板变量等信息。另外,阿里云短信服务需要收取一定的费用,请确保账户余额充足。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值