以下是一个使用阿里云短信服务的PHP示例代码:
<?php
// 引入SDK
require_once 'aliyun-php-sdk-core/Config.php';
use DysmsapiRequestV20170525SendSmsRequest;
// 读取AccessKey配置
$accessKeyId = "your_access_key_id";
$accessKeySecret = "your_access_key_secret";
// 短信API产品名
$product = "Dysmsapi";
// 短信API产品域名
$domain = "dysmsapi.aliyuncs.com";
// 暂时不支持多Region
$region = "cn-hangzhou";
// 初始化client
$iClientProfile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
$client = new DefaultAcsClient($iClientProfile);
// 创建请求并设置参数
$request = new SendSmsRequest();
$request->setPhoneNumbers("18888888888"); // 接收短信的手机号码
$request->setSignName("阿里云短信测试专用"); // 短信签名
$request->setTemplateCode("SMS_123456789"); // 短信模板ID
$request->setTemplateParam("{"code":"123456"}"); // 短信模板中的变量替换JSON串
// 发起请求并处理响应
try {
$response = $client->getAcsResponse($request);
if ($response->Code == 'OK') { // 判断短信是否发送成功
echo "短信发送成功!";
} else {
echo "短信发送失败,原因:" . $response->Message;
}
} catch (Exception $e) {
echo "短信发送异常,原因:" . $e->getMessage();
}
?>
请注意将示例代码中的”your_access_key_id”和”your_access_key_secret”替换为自己的阿里云AccessKey值。同时,将”18888888888″替换为接收短信的手机号码,”阿里云短信测试专用”替换为自己的短信签名,”SMS_123456789″替换为自己的短信模板ID。如果有变量替换的需求,可以修改”setTemplateParam”方法中的JSON串。
此代码使用阿里云短信服务提供的SDK发送短信,需要先下载并导入aliyun-php-sdk-core的库。代码中的API请求参数可以根据自己的需求进行修改。
以下是一个示例的苏州阿里云代理商可用的阿里云短信的PHP类:
<?php
class AliyunSms {
private $accessKeyId;
private $accessKeySecret;
private $signName;
private $templateCode;
public function __construct($accessKeyId, $accessKeySecret, $signName, $templateCode) {
$this->accessKeyId = $accessKeyId;
$this->accessKeySecret = $accessKeySecret;
$this->signName = $signName;
$this->templateCode = $templateCode;
}
public function sendSms($mobile, $templateParam) {
$params = array (
'PhoneNumbers' => $mobile,
'SignName' => $this->signName,
'TemplateCode' => $this->templateCode,
'TemplateParam' => json_encode($templateParam),
'OutId' => time()
);
$params['RegionId'] = 'cn-hangzhou';
$params['Action'] = 'SendSms';
$params['Version'] = '2017-05-25';
$params['AccessKeyId'] = $this->accessKeyId;
$params['Format'] = 'JSON';
$params['SignatureMethod'] = 'HMAC-SHA1';
$params['Timestamp'] = gmdate('Y-m-dTH:i:sZ');
$params['SignatureVersion'] = '1.0';
$params['SignatureNonce'] = md5(uniqid(mt_rand(), true));
ksort($params);
$canonicalizedQueryString = '';
foreach ($params 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, $this->accessKeySecret . '&', true));
$params['Signature'] = $signature;
$requestUrl = 'http://dysmsapi.aliyuncs.com/?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
private function percentEncode($value) {
$percentEncode = urlencode($value);
$percentEncode = str_replace('+', '%20', $percentEncode);
$percentEncode = str_replace('*', '%2A', $percentEncode);
$percentEncode = str_replace('%7E', '~', $percentEncode);
return $percentEncode;
}
}
使用示例:
// 实例化阿里云短信类
$sms = new AliyunSms('your_access_key_id', 'your_access_key_secret', 'your_sign_name', 'your_template_code');
// 发送短信
$mobile = '132xxxxxxxx';
$templateParam = array(
'code' => '123456'
);
$response = $sms->sendSms($mobile, $templateParam);
// 打印发送结果
if ($response['Code'] == 'OK') {
echo '短信发送成功';
} else {
echo '短信发送失败:' . $response['Message'];
}
注意替换your_access_key_id
、your_access_key_secret
、your_sign_name
和your_template_code
为自己的阿里云访问密钥、短信签名和短信模板编码。另外,若有需要,可以根据实际情况调整代码中的其他参数和配置。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/147212.html