互联网时代短信的应用无处不在,如APP的注册,平时的短信通知等。
下面就由小编系统的讲解一下阿里云短信的购买和使用流程
1,无论怎么样首先你需要注册个阿里云账号
- 点击下面链接注册:
输入账户名和密码和手机号
注册完成以后,请到个人中心做实名。 切记:如果是企业行为一定是要做企业实名,不要个人实名。https://account.console.aliyun.com/v2/#/authc/types
2,购买短信套餐包
点击:立即购买 一般项目刚刚上线,选择1.5万条短信就够了。
3,设置短信签名
填写:签名,签名用途,签名来源,场景说明
4,设置短信模板。
填写:模型类型,关联签名,模板名称,模板内容,场景链接,场景说明
5,接入短信SDK
首先获取到AccessKey
AccessKey ID:用于标识用户。
AccessKey Secret:用于验证用户的密钥。AccessKey Secret必须保密。
点击下面链接:
https://ram.console.aliyun.com/manage/ak?spm=5176.12818093.top-nav.dak.5adc16d0Ixl28b
创建AccessKey
以下是发送短信和批量发送短信的接口地址:
https://help.aliyun.com/document_detail/419273.html?spm=a2c4g.419274.0.0.29cb4198AtA3Qx
调试发送短信的demo
下面是Java代码实力
// This file is auto-generated, don't edit it. Thanks. package demo; import com.aliyun.auth.credentials.Credential; import com.aliyun.auth.credentials.provider.StaticCredentialProvider; import com.aliyun.core.http.HttpClient; import com.aliyun.core.http.HttpMethod; import com.aliyun.core.http.ProxyOptions; import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder; import com.aliyun.sdk.service.dysmsapi20170525.models.*; import com.aliyun.sdk.service.dysmsapi20170525.*; import com.google.gson.Gson; import darabonba.core.RequestConfiguration; import darabonba.core.client.ClientOverrideConfiguration; import darabonba.core.utils.CommonUtil; import darabonba.core.TeaPair; //import javax.net.ssl.KeyManager; //import javax.net.ssl.X509TrustManager; import java.net.InetSocketAddress; import java.time.Duration; import java.util.*; import java.util.concurrent.CompletableFuture; public class SendSms { public static void main(String[] args) throws Exception { // HttpClient Configuration /*HttpClient httpClient = new ApacheAsyncHttpClientBuilder() .connectionTimeout(Duration.ofSeconds(10)) // Set the connection timeout time, the default is 10 seconds .responseTimeout(Duration.ofSeconds(10)) // Set the response timeout time, the default is 20 seconds .maxConnections(128) // Set the connection pool size .maxIdleTimeOut(Duration.ofSeconds(50)) // Set the connection pool timeout, the default is 30 seconds // Configure the proxy .proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<your-proxy-hostname>", 9001)) .setCredentials("<your-proxy-username>", "<your-proxy-password>")) // If it is an https connection, you need to configure the certificate, or ignore the certificate(.ignoreSSL(true)) .x509TrustManagers(new X509TrustManager[]{}) .keyManagers(new KeyManager[]{}) .ignoreSSL(false) .build();*/ // Configure Credentials authentication information, including ak, secret, token StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder() .accessKeyId("<your-accessKeyId>") .accessKeySecret("<your-accessKeySecret>") //.securityToken("<your-token>") // use STS token .build()); // Configure the Client AsyncClient client = AsyncClient.builder() .region("cn-hangzhou") // Region ID //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient) .credentialsProvider(provider) //.serviceConfiguration(Configuration.create()) // Service-level configuration // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc. .overrideConfiguration( ClientOverrideConfiguration.create() .setEndpointOverride("dysmsapi.aliyuncs.com") //.setConnectTimeout(Duration.ofSeconds(30)) ) .build(); // Parameter settings for API request SendSmsRequest sendSmsRequest = SendSmsRequest.builder() // Request-level configuration rewrite, can set Http request parameters, etc. // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders())) .build(); // Asynchronously get the return value of the API request CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest); // Synchronously get the return value of the API request SendSmsResponse resp = response.get(); System.out.println(new Gson().toJson(resp)); // Asynchronous processing of return values /*response.thenAccept(resp -> { System.out.println(new Gson().toJson(resp)); }).exceptionally(throwable -> { // Handling exceptions System.out.println(throwable.getMessage()); return null; });*/ // Finally, close the client client.close(); } }
下面是PHP代码示例
<?php // This file is auto-generated, don't edit it. Thanks. namespace AlibabaCloud\SDK\Sample; use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi; use \Exception; use AlibabaCloud\Tea\Exception\TeaError; use AlibabaCloud\Tea\Utils\Utils; use Darabonba\OpenApi\Models\Config; use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest; use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; class Sample { /** * 使用AK&SK初始化账号Client * @param string $accessKeyId * @param string $accessKeySecret * @return Dysmsapi Client */ public static function createClient($accessKeyId, $accessKeySecret){ $config = new Config([ // 必填,您的 AccessKey ID "accessKeyId" => $accessKeyId, // 必填,您的 AccessKey Secret "accessKeySecret" => $accessKeySecret ]); // 访问的域名 $config->endpoint = "dysmsapi.aliyuncs.com"; return new Dysmsapi($config); } /** * @param string[] $args * @return void */ public static function main($args){ // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html $client = self::createClient("accessKeyId", "accessKeySecret"); $sendSmsRequest = new SendSmsRequest([]); $runtime = new RuntimeOptions([]); try { // 复制代码运行请自行打印 API 的返回值 $client->sendSmsWithOptions($sendSmsRequest, $runtime); } catch (Exception $error) { if (!($error instanceof TeaError)) { $error = new TeaError([], $error->getMessage(), $error->getCode(), $error); } // 如有需要,请打印 error Utils::assertAsString($error->message); } } } $path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php'; if (file_exists($path)) { require_once $path; } Sample::main(array_slice($argv, 1));