C/C++ DEMO

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

  1. #include <stdio.h>
  2. #include <curl/curl.h>
  3. #include <stdarg.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define MAXPARAM 2048
  7. CURL *curl;
  8. CURLcode res;
  9. /**
  10. bingone
  11. 本样例依赖libcurl库
  12. 下载地址 https://curl.haxx.se/download.html
  13. */
  14. // 修改为您的apikey(https://www.yunpian.com)登录官网后获取
  15. char *apikey = "xxxxxxxxxxxxxxxx";
  16. // 修改为您要发送的手机号
  17. char *mobile = "xxxxxxxxxxxxxxxx";
  18. // 设置您要发送的内容
  19. char *text = "【云片网】您的验证码是1234";
  20. // 指定发送的模板id
  21. int tpl_id = 1;
  22. // 指定发送模板内容
  23. char *tpl_data[4] = {"#code#","1234","#company#","云片网"};
  24. // 发送语音验证码内容
  25. int code = 1234;
  26. // 获取user信息url
  27. char *url_get_user = "https://sms.yunpian.com/v2/user/get.json";
  28. // 智能模板发送短信url
  29. char *url_send_sms = "https://sms.yunpian.com/v2/sms/single_send.json";
  30. // 指定模板发送短信url
  31. char *url_tpl_sms = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
  32. // 发送语音验证码短信url
  33. char *url_send_voice = "https://voice.yunpian.com/v2/voice/send.json";
  34. void send_data(char *url,char *data)
  35. {
  36. // specify the url
  37. curl_easy_setopt(curl, CURLOPT_URL, url);
  38. // specify the POST data
  39. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  40. // get response data
  41. curl_easy_perform(curl);
  42. printf("\n\n");
  43. }
  44. /**
  45. * 查账户信息
  46. */
  47. void get_user(char *apikey)
  48. {
  49. char params[MAXPARAM + 1];
  50. char *cp = params;
  51. sprintf(params,"apikey=%s", apikey);
  52. send_data(url_get_user, params);
  53. }
  54. /**
  55. * 使用智能匹配模板接口发短信
  56. */
  57. void send_sms(char *apikey, char *mobile, char *text)
  58. {
  59. char params[MAXPARAM + 1];
  60. char *cp = params;
  61. sprintf(params,"apikey=%s&mobile=%s&text=%s", apikey, mobile, text);
  62. send_data(url_send_sms, params);
  63. }
  64. /**
  65. * 指定模板单发
  66. */
  67. void send_tpl_sms(char *apikey, char *mobile, int tpl_id, char *tpl_value)
  68. {
  69. char params[MAXPARAM + 1];
  70. char *cp = params;
  71. sprintf(params, "apikey=%s&mobile=%s&tpl_id=%d&tpl_value=%s", apikey, mobile,
  72. tpl_id, tpl_value);
  73. send_data(url_tpl_sms, params);
  74. }
  75. /**
  76. * 发送语音验证码
  77. */
  78. void send_voice(char *apikey, char *mobile, int code)
  79. {
  80. char params[MAXPARAM + 1];
  81. char *cp = params;
  82. sprintf(params,"apikey=%s&mobile=%s&code=%d", apikey, mobile, code);
  83. send_data(url_send_voice, params);
  84. }
  85. int main(void)
  86. {
  87. curl = curl_easy_init();
  88. mobile = curl_easy_escape(curl,mobile,strlen(mobile));
  89. if(NULL == curl) {
  90. perror("curl open fail\n");
  91. }
  92. // 获取用户信息
  93. get_user(apikey);
  94. // 发送短信
  95. send_sms(apikey,mobile,text);
  96. // 发送语音验证码
  97. send_voice(apikey,mobile,code);
  98. char *tmp;
  99. char *tpl_value = (char *)malloc(sizeof(char) * 500);
  100. bzero(tpl_value, sizeof(char)*500);
  101. // 模板短信发送需要编码两次,第一次URL编码转换
  102. int len = 0;
  103. tmp = curl_easy_escape(curl,tpl_data[0],strlen(tpl_data[0]));
  104. memcpy(tpl_value+len,tmp,strlen(tmp));
  105. len += strlen(tmp);
  106. tpl_value[len++] = '=';
  107. tmp = curl_easy_escape(curl,tpl_data[1],strlen(tpl_data[1]));
  108. memcpy(tpl_value+len,tmp,strlen(tmp));
  109. len += strlen(tmp);
  110. tpl_value[len++] = '&';
  111. tmp = curl_easy_escape(curl,tpl_data[2],strlen(tpl_data[2]));
  112. memcpy(tpl_value+len,tmp,strlen(tmp));
  113. len += strlen(tmp);
  114. tpl_value[len++] = '=';
  115. tmp = curl_easy_escape(curl,tpl_data[3],strlen(tpl_data[3]));
  116. memcpy(tpl_value+len,tmp,strlen(tmp));
  117. len += strlen(tmp);
  118. tmp=tpl_value;
  119. // 第二次URL编码
  120. tpl_value = curl_easy_escape(curl,tpl_value,strlen(tpl_value));
  121. send_tpl_sms(apikey,mobile,tpl_id,tpl_value);
  122. free(tmp);
  123. curl_easy_cleanup(curl);
  124. return 0;
  125. }