想用自己的手机号以编程方式发送短信吗?与其为昂贵的运营商API付费,不如将您的Android手机用作短信网关。方法如下。
概念
传统的短信API(Twilio, MessageBird)通过电信运营商路由消息。您需要租用一个电话号码,并按消息量和月费支付费用。
基于手机的短信网关则颠覆了这一模式:您的Android手机成为发送设备。服务器将消息排队,您的手机接收并经由其SIM卡发送。您保留自己的号码,并支付运营商的短信费率。
您需要准备什么
- 一部带有有效SIM卡的Android手机(8.0+)
- 一个MySMSGate账户(免费创建)
- MySMSGate Android应用
- 手机的WiFi或移动数据连接
第一步:创建您的账户
访问 mysmsgate.net/auth/register 并注册。您将获得10条免费短信用于测试——无需信用卡。
注册后,您将在仪表板上看到您的API密钥。请复制它——在应用和API调用中都会用到。
第二步:安装Android应用
在您的Android手机上下载并安装MySMSGate应用:
打开应用,进入网关设置,并输入您的API密钥。应用将连接到服务器,并在您的仪表板上显示为“在线”。
第三步:通过API发送短信
现在您可以通过一次API调用发送短信:
cURL
curl -X POST https://mysmsgate.net/api/v1/send \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "+1234567890",
"message": "Your appointment is tomorrow at 3 PM"
}'
Python
import requests
response = requests.post(
"https://mysmsgate.net/api/v1/send",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"to": "+1234567890",
"message": "Your appointment is tomorrow at 3 PM"
}
)
print(response.json())
JavaScript (Node.js)
const response = await fetch("https://mysmsgate.net/api/v1/send", {
method: "POST",
headers: {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
},
body: JSON.stringify({
to: "+1234567890",
message: "Your appointment is tomorrow at 3 PM"
})
});
const data = await response.json();
console.log(data);
PHP
$ch = curl_init("https://mysmsgate.net/api/v1/send");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer YOUR_API_KEY",
"Content-Type": "application/json"
],
CURLOPT_POSTFIELDS => json_encode([
"to" => "+1234567890",
"message" => "Your appointment is tomorrow at 3 PM"
]),
CURLOPT_RETURNTRANSFER => true
]);
$response = curl_exec($ch);
echo $response;
第四步:检查送达状态
API会返回一个短信ID,您可以使用它来检查送达状态:
curl https://mysmsgate.net/api/v1/sms?id=SMS_ID \
-H "Authorization: Bearer YOUR_API_KEY"
状态:pending(待处理) → sending(发送中) → sent(已发送)(或 failed(失败)并附带错误消息)。
高级功能:双SIM卡与多设备
如果您的手机有两张SIM卡,您可以选择通过哪张卡发送:
curl -X POST https://mysmsgate.net/api/v1/send \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "+1234567890",
"message": "Hello!",
"sim_slot": 1
}'
您还可以将多部手机连接到同一个账户,并将消息路由到特定设备:
{
"to": "+1234567890",
"message": "Hello!",
"device_id": "your-device-id"
}
手机离线时会发生什么?
消息会在服务器上排队。当手机重新连接时,它会自动获取待处理的消息并发送。如果手机处于睡眠模式,服务器还会发送推送通知 (FCM) 来唤醒手机。
成本比较
每月500条短信的成本:
| 提供商 | 月费用 |
|---|---|
| Twilio | $41+ (消息费 + 号码费 + 注册费) |
| MessageBird | $35+ |
| MySMSGate | $15 (500 × $0.03,无其他费用) |
这意味着每年节省300多美元——而且您保留自己的电话号码。
免费试用——包含10条短信,无需信用卡。
Comments (0)
Be the first to comment!