各语言对接短信宝API示例
温馨提示:本文最后更新于2024年4月22日 16:21,若内容或图片失效,请在下方留言或联系博主。
PHP示例:
<?php
function sendSms($config, $params) {
$apiUrl = $config['url'] . 'sendApi'; // 通道域名
$channel = $config['channel']; // 通道ID
$username = $config['username']; // 短信平台用户名
$key = md5($config['key']); // 短信平台密钥
$content = $params['content']; // 要发送的短信内容
$phone = $params['mobile']; // 要发送短信的手机号码
// 构建查询参数
$queryParams = [
'channel' => $channel,
'username' => $username,
'key' => $key,
'phone' => $phone,
'content' => $content,
];
// 构建请求URL
$requestUrl = $apiUrl . '?' . http_build_query($queryParams);
// 发送GET请求并获取响应
$result = file_get_contents($requestUrl);
// 解析JSON响应
$result = json_decode($result, true);
// 检查响应中的code字段来确定请求是否成功
if ($result['code'] == '1') {
// 请求成功,返回成功的状态和信息
return ['status' => 'success', 'msg' => $result['msg']];
} else {
// 请求失败,返回错误的状态和信息
return ['status' => 'error', 'msg' => $result['msg']];
}
}
?>