发送短信和 WhatsApp API
SMSMobile API 支持两种身份验证方法:使用简单的 API 密钥或带有客户端 ID 和客户端密钥的 OAuth2 协议。
1. 发送短信的 API 密钥认证
此方法需要 API 密钥,可将其作为参数包含在 GET 或 POST 请求中。这是验证 API 请求的一种简单方法。
https://api.smsmobileapi.com/sendsms?apikey=YOUR_API_KEY&recipients=PHONE_NUMBER&message=MESSAGE_TO_SEND
参数:
- apikey:您的唯一 API 密钥。
- recipients:收件人的电话号码。
- message:要发送的消息(消息长度不限于 160 个字符;您可以发送更长的消息)
- sendwa:1 = 消息必须通过 WhatsApp 发送。
- sendsms:1 = 信息必须通过传统短信发送。(如果 发送短信 为空:默认发送,设置为0则屏蔽该短信)
- schedule_timestamp = 表示应发送短信的时间 - 必须是 **UNIX 时间戳 (GMT 0)**
例子:
GET https://api.smsmobileapi.com/sendsms?apikey=YOUR_API_KEY&recipients=+1234567890&message=Hello%20World
注意:如果由于您的运营商而导致传送错误,默认功能将在定义的时间间隔内重试发送相同的短信最多 3 次。
2. 发送短信的 OAuth2 身份验证
OAuth2提供了一种更安全、更可扩展的身份验证方法。
您需要使用客户端 ID 和客户端密钥来获取访问令牌,然后使用 授权 標軸。
client_id 和 client_secret 在您的仪表板中可用,在您的移动设备上安装应用程序并创建帐户后即可访问。
立即下载移动应用程序 或者
访问您的仪表板。
获取访问令牌
要获取访问令牌,请使用您的客户端 ID 和客户端密钥向令牌端点发送 POST 请求。
curl -X POST https://api.smsmobileapi.com/oauth2/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "client_id=1ab0ex4b4c1ef2c800690d" \
-d "client_secret=3ed108a93d0414074b94364290b6a7348475e93a0567005"
使用访问令牌:
获得访问令牌后,将其包含在 授权 您的 API 请求标头:
curl -X POST https://api.smsmobileapi.com/sendsms \
-H“授权:持有人abc123xyz456”\
-H“内容类型:application/x-www-form-urlencoded” \
-d“收件人=+1234567890”\
-d“消息=你好”
您应该使用哪种方法?
- 使用 API 密钥认证 实现快速、直接的集成。
- 使用 OAuth2 身份验证 以增强集成的安全性和可扩展性。
欲了解更多详细信息,请参阅完整文档。
Resend an unsent SMS
This API endpoint is used to resend an unsent SMS (only if the message has an error status)
https://api.smsmobileapi.com/resend/?apikey=YOUR_API_KEY
范围:
- guid: (required) GUID of the message to be resent.
从 API 发送的短信日志
此 API 端点用于检索通过 API 发送的 SMS 消息的日志。
https://api.smsmobileapi.com/log/sent/sms?apikey=YOUR_API_KEY
范围:
- apikey: (必填)您的 API 密钥。
- guid_message: 用于排序和过滤的消息的唯一标识符。
- before: 检索在此特定时间戳或 GUID 之前发送的消息。
- error_api: 输入`1`(`&error_api=1`)列出所有在 API 请求期间遇到错误的短信。
- error_mobile: 输入`1`(`&error_mobile=1`)列出所有在移动处理过程中遇到错误的短信。
- keyword: 根据收件人号码或短信内容过滤消息的关键字。
获取手机收到的短信 API
此 API 端点用于检索智能手机上收到的短信。
https://api.smsmobileapi.com/getsms/?apikey=YOUR_API_KEY
范围:
- apikey: 您拥有或将收到的 API 密钥。
.
Webhook – 短信已接收
此 webhook 系统每次收到短信时都会向配置的 URL 发送 POST 请求。系统通过将短信详细信息发送到指定的 webhook URL 来确保实时更新。
如何在仪表板中配置 Webhook
按照以下步骤在仪表板中设置您的 webhook URL:
- 登录您的账户 SMS 移动 API 仪表板.
- 导航至 Webhook 设置 部分。
- 输入你的 webhook URL(例如,
https://example.com/webhook-endpoint
- 点击 保存 Webhook 按钮。
- 保存后,系统将开始向配置的 URL 发送短信详细信息。
Webhook 负载
收到短信时,系统会将以下 JSON 负载发送到您的 webhook URL:
{
“日期”:“2025-01-20”,
“小时”:“10:15:00”,
“time_received”:“2025-01-20 10:14:50”,
"message": "您好,这是一个测试。",
“号码”:“+123456789”,
“guid”:“abcde12345”
}
有效载荷字段:
- 日期: 添加短信的日期(YYYY-MM-DD)。
- 小时: 添加短信的小时(HH:mm:ss)。
- 接收时间: 短信收到时间。
- 信息: 短信内容。
- 数字: 发件人的电话号码。
- 指南: 短信的唯一标识符。
Webhook 端点示例
您的服务器应已准备好处理传入的 POST 请求。以下是用于处理 webhook 负载的示例 PHP 脚本:
<?php
// 读取传入的有效载荷
$payload = file_get_contents('php://input');
$data = json_decode($payload, true);
// 处理短信数据
如果($数据){
// 示例:将短信数据记录到文件中
文件放入内容('sms_log.txt',print_r($data,true),FILE_APPEND);
// 以 HTTP 200 状态响应
http_response_code(200);
echo "Webhook 接收成功。";
} 别的 {
// 对错误请求使用 HTTP 400 状态进行响应
http_response_code(400);
echo "无效的有效载荷。";
}
?>
测试 Webhook
要测试你的 webhook 配置,请使用以下工具:
这些工具允许您检查系统发送的有效负载并调试 webhook 设置中的任何问题。
故障排除
- 确保 webhook URL 正确且可公开访问。
- 验证您的服务器是否在成功请求时返回 HTTP 200 状态代码。
- 检查您的服务器日志,查找处理有效负载时出现的任何错误。
删除短信 API
此 API 端点用于从 SMS Mobile API 的服务器日志中删除短信
https://api.smsmobileapi.com/deletesms/?apikey=YOUR_API_KEY
范围:
- apikey: 您拥有的 API 密钥。
- guid_message: 要删除的消息的唯一 ID。
- date_start: 如果单独使用,则会删除指定日期的所有消息。
- date_start and date_end: 合并删除指定时间段内的消息。
注意:删除的短信仅是存储在您的移动应用程序帐户日志中的短信。移动设备本身上的短信不会被删除,因为我们无法访问它们。
发送短信
WSDL 网址
https://api.smsmobileapi.com/sendsms/wsdl/sendsms.wsdl
参数:
- recipients: 收件人的手机号码。
- message: 要发送的消息。(消息长度不限于 160 个字符;您可以发送更长的消息)
- apikey: 您拥有或将收到的 API 密钥。
示例
require_once "lib/nusoap.php";
$client = new nusoap_client("https://api.smsmobileapi.com/sendsms/wsdl/sendsms.wsdl", true);
$error = $client->getError();
$result = $client->call("sendSms", array("recipients" =>$_GET['recipients'],"message" =>$_GET['message'],"apikey" =>$_GET['apikey']));
print_r($result);
发送短信和 WhatsApp
基本 cURL 命令
您可以使用以下 cURL 命令通过 SMSmobileAPI 发送短信:
curl -X POST https://api.smsmobileapi.com/sendsms/ \
-d "recipients=PHONE_NUMBER" \
-d "message=YOUR_MESSAGE" \
-d "apikey=YOUR_API_KEY"
-d "sendwa=1"
-d "sendsms=1"
PHP 中的 cURL 示例
如果您使用 PHP,可以使用 cURL 发送短信,方法如下:
<?php
$url = 'https://api.smsmobileapi.com/sendsms/';
$data = array(
'recipients' => 'PHONE_NUMBER',
'message' => 'YOUR_MESSAGE',
'apikey' => 'YOUR_API_KEY'
'sendwa' => '1'
'sendsms' => '1'
);
$options = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_RETURNTRANSFER => true,
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
此 PHP 示例演示如何使用 cURL 向 SMSmobileAPI 发送 POST 请求,并将必要的参数作为关联数组传递。
发送短信和 WhatsApp
使用我们的官方 Python 模块: https://smsmobileapi.com/python/
或者使用手动方式...
使用 `requests` 库
`requests` 库是一个流行且简单的 Python HTTP 库。以下是如何使用其通过 SMSmobileAPI 发送短信:
import requests
url = "https://api.smsmobileapi.com/sendsms/"
payload = {
"recipients": "PHONE_NUMBER",
"message": "YOUR_MESSAGE",
"apikey": "YOUR_API_KEY"
}
response = requests.post(url, data=payload)
print(response.text)
此示例演示了向 SMSmobileAPI 发送简单的 POST 请求,向特定电话号码发送消息。
使用 `http.client` 库
`http.client` 库包含在 Python 的标准库中,也可以用于与你的 API 进行交互:
import http.client
import urllib.parse
conn = http.client.HTTPSConnection("api.smsmobileapi.com")
params = urllib.parse.urlencode({
"recipients": "PHONE_NUMBER",
"message": "YOUR_MESSAGE",
"apikey": "YOUR_API_KEY"
})
headers = { "Content-type": "application/x-www-form-urlencoded" }
conn.request("POST", "/sendsms/", params, headers)
response = conn.getresponse()
data = response.read()
print(data.decode("utf-8"))
conn.close()
此示例展示了如何使用 `http.client` 库向 API 发送 POST 请求。参数经过 URL 编码,并使用适当的标头发送。
发送短信和 WhatsApp
使用 `fetch` API
`fetch` API 是一种使用 JavaScript 发出 HTTP 请求的现代且通用的方法。以下是您可以使用它通过 SMSmobileAPI 发送短信的方法:
const url = "https://api.smsmobileapi.com/sendsms/";
const data = {
recipients: "PHONE_NUMBER",
message: "YOUR_MESSAGE",
apikey: "YOUR_API_KEY"
};
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: new URLSearchParams(data)
})
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.error("Error:", error));
此示例演示如何使用大多数现代浏览器支持的“fetch”API 发送 POST 请求。
使用 XMLHttpRequest
如果需要支持旧版浏览器,可以使用 `XMLHttpRequest` 对象:
const xhr = new XMLHttpRequest();
const url = "https://api.smsmobileapi.com/sendsms/";
const data = "recipients=PHONE_NUMBER&message=YOUR_MESSAGE&apikey=YOUR_API_KEY";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText);
}
};
xhr.send(data);
此示例显示如何使用“XMLHttpRequest”发送POST请求,以兼容可能不支持“fetch”的老版本浏览器。
发送短信和 WhatsApp
使用 `axios` 库
`axios` 库是 Node.js 的一个流行 HTTP 客户端。以下是使用它通过 SMSmobileAPI 发送短信的方法:
const axios = require('axios');
const url = 'https://api.smsmobileapi.com/sendsms/';
const data = {
recipients: 'PHONE_NUMBER',
message: 'YOUR_MESSAGE',
apikey: 'YOUR_API_KEY'
};
axios.post(url, data)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Error:', error);
});
此示例演示如何使用 Node.js 中的“axios”发送 POST 请求与 SMSmobileAPI 进行交互。
发送短信和 WhatsApp
使用 `Net::HTTP` 库
您可以使用 Ruby 中的 `Net::HTTP` 库通过 SMSmobileAPI 发送短信:
require 'net/http'
require 'uri'
uri = URI.parse("https://api.smsmobileapi.com/sendsms/")
request = Net::HTTP::Post.new(uri)
request.set_form_data(
"recipients" => "PHONE_NUMBER",
"message" => "YOUR_MESSAGE",
"apikey" => "YOUR_API_KEY"
)
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
puts response.body
此示例演示如何使用 Ruby 中的“Net::HTTP”向 SMSmobileAPI 发送 POST 请求。