使用C++语言调用短信接口发送验证码短信教程
C++对接验证码短信接口DEMO示例
<p><img title="使用C++语言调用短信接口发送验证码短信教程(图1)" alt="使用C++语言调用短信接口发送验证码短信教程(图1)" src="/uploads/allimg/20240526/1-24052623461YP.jpeg"/></p>
<p>开发过程中,验证码短信是常用的安全验证手段。这篇文章讲怎么用 C++ 调用短信接口,把验证码短信发出去。接口选用 <code>https://api.3yit.com/api/send-sms-single</code>,通过 POST 请求携带参数,从返回结果里拿到发送状态。</p>
<h2>准备工作</h2>
<p>开始之前,先确认已经拿到短信服务商的接口文档,弄明白 <code>sp_id</code>、<code>password</code>(或签名算法)等关键信息,同时确认开发环境支持 C++,并装好合适的网络请求库,比如 curl 或 libcurl。</p>
<h2>安装和配置 libcurl</h2>
<p>C++ 里发 HTTP 请求一般用 libcurl。先在系统里装好 libcurl,不同操作系统的安装方式不一样,具体可以看 libcurl 官网的安装指南。</p>
<h2>获取接口参数</h2>
<p>登录智慧云信官网,选择对应的产品(如【验证码】),在上方选项卡中点击【开发者】→【HTTP开发文档】,就能看到具体的 API 方法名称,以及各参数的 key 和对应值,通常包括 <code>sp_id</code>、<code>mobile</code>、<code>content</code>、<code>password</code> 等。各参数含义如下:</p>
<ul><li><code>sp_id</code>:产品编号,全局唯一,一般由 6 位数字组成。</li><li><code>mobile</code>:接收短信的手机号码。</li><li><code>content</code>:短信内容,包含签名和正文。例如“【智慧云信】您的验证码是456790,请妥善保管”,其中【智慧云信】是签名,需在签名报备中申请;后面的提示语是短信模板,需在模板报备中申请,审核通过后才能发送。</li><li><code>password</code>:用 SP_ID 密码经 MD5 加密后生成的接口密码(非登录密码),由 32 位随机字符串组成。</li></ul>
<h2>编写 C++ 代码</h2>
<p>下面是用 C++ 和 libcurl 调用短信接口发送验证码短信的示例代码:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;curl/curl.h&gt;
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
((std::string*)userp)-&gt;append((char*)contents, size * nmemb);
return size * nmemb;
}
int main() {
CURL *curl;
CURLcode res;
std::string readBuffer;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
std::string postData = "sp_id=5xxxxx&amp;mobile=176xxxxxxxx&amp;content=%E3%80%90%E7%AD%BE%E5%90%8D%E3%80%91%E9%A2%98%E7%A0%81123&amp;password=xxxxxxxxxxx";
curl_easy_setopt(curl, CURLOPT_URL, "https://api.3yit.com/api/send-sms-single");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &amp;readBuffer);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
std::cerr &lt;&lt; "curl_easy_perform() failed: " &lt;&lt; curl_easy_strerror(res) &lt;&lt; std::endl;
} else {
std::cout &lt;&lt; "Response: " &lt;&lt; readBuffer &lt;&lt; std::endl;
// 解析JSON响应(这里省略了JSON解析代码,您可以使用如nlohmann/json等库进行解析)
// ...
// 提取code、msg和msg_id等关键信息
// ...
// 输出结果
// ...
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}</code></pre>
<h3>代码说明</h3>
<ul><li>引入头文件:开头引入 <code>iostream</code>、<code>string</code> 和 <code>curl/curl.h</code>。</li><li><code>WriteCallback</code> 函数:接收服务器返回的响应数据,追加到一个 <code>std::string</code> 对象里。</li><li><code>main</code> 函数:先初始化 libcurl,创建 <code>CURL</code> 对象,设置请求 URL、请求头和 POST 数据,然后调用 <code>curl_easy_perform</code> 发送请求,把响应写入 <code>readBuffer</code>,最后检查是否成功并输出响应数据。</li><li>JSON 解析部分在示例里省略了,可以用 nlohmann/json 这类库来实现。</li></ul>
<h2>解析 JSON 响应</h2>
<p>收到响应后,需要解析 JSON 拿到发送结果。C++ 里可以用 nlohmann/json 这样的库。</p>
<h3>安装 nlohmann/json</h3>
<p>用包管理器(vcpkg、Conan 或项目构建系统支持的方式)安装 nlohmann/json,也可以直接从 GitHub 下载源码包含进项目。</p>
<h3>解析 JSON 并提取关键信息</h3>
<p>在收到响应后添加一个解析块,用 nlohmann/json 提取关键信息:</p>
<pre><code>#include &lt;nlohmann/json.hpp&gt;
using json = nlohmann::json;
// ... 其他代码 ...
if(res != CURLE_OK) {
std::cerr &lt;&lt; "curl_easy_perform() failed: " &lt;&lt; curl_easy_strerror(res) &lt;&lt; std::endl;
} else {
std::cout &lt;&lt; "Response: " &lt;&lt; readBuffer &lt;&lt; std::endl;
// 解析JSON响应
try {
json response = json::parse(readBuffer);
// 提取code、msg和msg_id等关键信息
int code = response["code"];
std::string msg = response["msg"];
std::string msg_id = response.contains("msg_id") ? response["msg_id"] : "N/A"; // 检查是否存在msg_id
// 输出结果
std::cout &lt;&lt; "Code: " &lt;&lt; code &lt;&lt; std::endl;
std::cout &lt;&lt; "Message: " &lt;&lt; msg &lt;&lt; std::endl;
std::cout &lt;&lt; "Message ID: " &lt;&lt; msg_id &lt;&lt; std::endl;
// 根据code值判断发送是否成功,并做相应处理
if (code == 0) {
std::cout &lt;&lt; "验证码短信发送成功!" &lt;&lt; std::endl;
} else {
std::cout &lt;&lt; "验证码短信发送失败:" &lt;&lt; msg &lt;&lt; std::endl;
}
} catch (const json::exception&amp; e) {
std::cerr &lt;&lt; "JSON解析错误: " &lt;&lt; e.what() &lt;&lt; std::endl;
}
}
// ... 其他代码 ...</code></pre>
<h2>错误处理</h2>
<p>上面的代码里用 try-catch 块捕获 nlohmann/json 解析时可能抛出的异常,同时检查响应里的 <code>code</code> 值判断发送是否成功,并输出相应消息。</p>
<h2>注意事项</h2>
<ul><li>示例只用了密码(<code>password</code>)做认证。如果改用签名(<code>signature</code>),要按服务商的签名算法算好签名,作为请求参数一起发送。</li><li>实际开发中要对各类错误做充分处理,包括网络错误、请求超时、服务器内部错误等。</li><li>方便调试的话,在代码里加日志记录功能,记下关键信息和错误信息。</li><li>用户手机号、验证码属于敏感信息,注意数据安全和隐私保护,不要泄露给未授权的第三方。</li></ul>
<p>按上面几步,就能用 C++ 和 libcurl 调通短信接口,发验证码短信并解析返回结果。</p>
<p><strong>其他语言教程:</strong><a href="https://www.3yit.com/smsapi/262.html" title="PHP短信接口教程">PHP</a> · <a href="https://www.3yit.com/smsapi/282.html" title="Java短信接口教程">Java</a> · <a href="https://www.3yit.com/smsapi/283.html" title="ASP短信接口教程">ASP</a> · <a href="https://www.3yit.com/smsapi/284.html" title="ASP.NET(C#)短信接口教程">ASP.NET(C#)</a> · <a href="https://www.3yit.com/smsapi/285.html" title="JSP短信接口教程">JSP</a> · <a href="https://www.3yit.com/smsapi/286.html" title="Node.js短信接口教程">Node.js</a> · <a href="https://www.3yit.com/smsapi/298.html" title="Delphi短信接口教程">Delphi</a> · <a href="https://www.3yit.com/smsapi/300.html" title="Go短信接口教程">Go</a> · <a href="https://www.3yit.com/smsapi/301.html" title="Python短信接口教程">Python</a> · <a href="https://www.3yit.com/smsapi/302.html" title="Ruby短信接口教程">Ruby</a> · <a href="https://www.3yit.com/smsapi/303.html" title="Shell短信接口教程">Shell</a> · <a href="https://www.3yit.com/smsapi/304.html" title="VB短信接口教程">VB</a> · <a href="https://www.3yit.com/smsapi/306.html" title="C短信接口教程">C</a>。更多信息见<a href="https://www.3yit.com/smsapi/613.html" title="短信API接入指南">短信API接入指南</a>。</p>
<p>代码里的 API 地址、账号和密码替换成你自己的就能跑通。如果还没有账号,去智慧云信官网注册一个,注册就能免费测试,验证码、通知、营销短信都支持。</p>
内容可信度说明
作者:智慧云信通信技术团队
技术审核:国际短信与号码检测产品组
最后更新:2026-08-13
本文主题:使用C++语言调用短信接口发送验证码短信教程
本文围绕「使用C++语言调用短信接口发送验证码短信教程」展开,结合智慧云信平台在短信接口API场景下的产品能力与客户接入经验整理。不同国家/地区的 Sender ID、模板审核、计费方式和合规要求可能变化,实际发送前建议以当前通道审核与项目沟通结果为准。