php阿里云短信完整代码

网站编辑2024-05-16 17:45:23218

php阿里云短信完整代码

简介

阿里云短信服务是阿里云提供的一项短信服务,可以帮助开发者向用户发送短信。本文将介绍如何使用PHP编写阿里云短信服务的完整代码,包括发送短信和查询短信记录等操作。

发送短信

要使用PHP编写阿里云短信服务的完整代码,首先需要在阿里云控制台上创建短信服务,并获取到相应的AK和SK。然后,使用以下代码实现发送短信功能:

```php

require_once 'aliyun-sdk-php-v4/Config.php';

use AlibabaCloudClientAlibabaCloud;

use AlibabaCloudSDKCloudMessagingV20190120CloudMessaging;

use AlibabaCloudTeaUtilsUtils;

use AlibabaCloudTeaUtilsUtilsRuntimeOptions;

$accessKeyId = "your access key id";

$accessKeySecret = "your access key secret";

$endpoint = "cn-shanghai.aliyuncs.com";

$accessRegionId = "cn-shanghai";

$runtimeOptions = new RuntimeOptions([]);

$cloudMessaging = new CloudMessaging($accessRegionId, $accessKeyId, $accessKeySecret);

$cloudMessaging->setAccessKeyId($accessKeyId);

$cloudMessaging->setAccessKeySecret($accessKeySecret);

$cloudMessaging->setEndpoint($endpoint);

try {

$request = new CloudMessagingRequestV20190120SmsSendRequest([    "mobile" => "13812345678",    "templateCode" => "SMS_14991054",    "templateParam" => "{"key":"value"}"]);$runtime = new RuntimeOptions([]);$response = $cloudMessaging->smsSendWithOptions($request, $runtime);print_r($response->body);

} catch (ClientException $exception) {

print_r($exception->getMessage());

} catch (ServerException $exception) {

print_r($exception->getMessage());

}

```

上述代码中,首先引入了阿里云短信服务的SDK,然后创建了一个实例并设置好相应的AK和SK。接着,定义了一个SmsSendRequest对象,设置了要发送的短信内容和模板代码等参数。最后,通过调用smsSendWithOptions方法发送短信,并打印出返回的响应结果。

查询短信记录

除了发送短信,我们还可以使用PHP编写阿里云短信服务的完整代码来查询短信记录。具体步骤如下:

```php

require_once 'aliyun-sdk-php-v4/Config.php';

use AlibabaCloudClientAlibabaCloud;

use AlibabaCloudSDKCloudMessagingV20190120CloudMessaging;

use AlibabaCloudTeaUtilsUtils;

use AlibabaCloudTeaUtilsUtilsRuntimeOptions;

$accessKeyId = "your access key id";

$accessKeySecret = "your access key secret";

$endpoint = "cn-shanghai.aliyuncs.com";

$accessRegionId = "cn-shanghai";

$runtimeOptions = new RuntimeOptions([]);

$cloudMessaging = new CloudMessaging($accessRegionId, $accessKeyId, $accessKeySecret);

$cloudMessaging->setAccessKeyId($accessKeyId);

$cloudMessaging->setAccessKeySecret($accessKeySecret);

$cloudMessaging->setEndpoint($endpoint);

try {

$request = new CloudMessagingRequestV20190120ListShortMessageLogRequest([    "messageId" => "1234567890"]);$runtime = new RuntimeOptions([]);$response = $cloudMessaging->listShortMessageLog

最新推荐

右侧广告图1
  • 短信套餐包

    通过调用API或使用控制台群发助手,即可发送带变量的验证码、通知和营销类短信;国内验证短信秒级触达,到达率最高可达99%;国际/港澳台短信覆盖200+国家和地区,安全稳定。

    40.00/年

    8折

  • 国内短信套餐包

    支持国内验证码、短信通知和推广短信,秒级触达,免运维

    35.00/2年

    7.4折

  • 数字短信套餐包

    通过调用API或使用控制台群发助手,即可发送带变量的验证码、通知和营销类短信;国内验证短信秒级触达,到达率最高可达99%;国际/港澳台短信覆盖200+国家和地区,安全稳定。

    20.00/年

    7.2折

右侧广告图2