C/C++ DEMO
功能說明:該接口要求提前在云片后臺添加模板,提交短信時,系統(tǒng)會自動匹配審核通過的模板,匹配成功任意一個模板即可發(fā)送。系統(tǒng)已提供的默認模板添加簽名后可以直接使用。
#include <stdio.h> #include <curl/curl.h> #include <stdarg.h> #include <stdlib.h> #include <string.h> #define MAXPARAM 2048 CURL *curl; CURLcode res; /** bingone 本樣例依賴libcurl庫 下載地址 https://curl.haxx.se/download.html */ // 修改為您的apikey(http://www.ssjdyy2.com)登錄官網(wǎng)后獲取 char *apikey = "xxxxxxxxxxxxxxxx"; // 修改為您要發(fā)送的手機號 char *mobile = "xxxxxxxxxxxxxxxx"; // 設置您要發(fā)送的內(nèi)容 char *text = "【云片網(wǎng)】您的驗證碼是1234,5分鐘內(nèi)有效。"; // 指定發(fā)送的模板id int tpl_id = 378641; // 指定發(fā)送模板內(nèi)容 char *tpl_data[4] = {"#code#","1234","#min#","5"}; // 發(fā)送語音驗證碼內(nèi)容 int code = 1234; // 獲取user信息url char *url_get_user = "https://sms.yunpian.com/v2/user/get.json"; // 智能模板發(fā)送短信url char *url_send_sms = "https://sms.yunpian.com/v2/sms/single_send.json"; // 指定模板發(fā)送短信url char *url_tpl_sms = "https://sms.yunpian.com/v2/sms/tpl_single_send.json"; // 發(fā)送語音驗證碼短信url char *url_send_voice = "https://voice.yunpian.com/v2/voice/send.json"; void send_data(char *url,char *data) { // specify the url curl_easy_setopt(curl, CURLOPT_URL, url); // specify the POST data curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); // get response data curl_easy_perform(curl); printf("\n\n"); } /** * 查賬戶信息 */ void get_user(char *apikey) { char params[MAXPARAM + 1]; char *cp = params; sprintf(params,"apikey=%s", apikey); send_data(url_get_user, params); } /** * 使用智能匹配模板接口發(fā)短信 */ void send_sms(char *apikey, char *mobile, char *text) { char params[MAXPARAM + 1]; char *cp = params; sprintf(params,"apikey=%s&mobile=%s&text=%s", apikey, mobile, text); send_data(url_send_sms, params); } /** * 指定模板單發(fā) */ void send_tpl_sms(char *apikey, char *mobile, int tpl_id, char *tpl_value) { char params[MAXPARAM + 1]; char *cp = params; sprintf(params, "apikey=%s&mobile=%s&tpl_id=%d&tpl_value=%s", apikey, mobile, tpl_id, tpl_value); send_data(url_tpl_sms, params); } /** * 發(fā)送語音驗證碼 */ void send_voice(char *apikey, char *mobile, int code) { char params[MAXPARAM + 1]; char *cp = params; sprintf(params,"apikey=%s&mobile=%s&code=%d", apikey, mobile, code); send_data(url_send_voice, params); } int main(void) { curl = curl_easy_init(); mobile = curl_easy_escape(curl,mobile,strlen(mobile)); if(NULL == curl) { perror("curl open fail\n"); } // 獲取用戶信息 get_user(apikey); // 發(fā)送短信 send_sms(apikey,mobile,text); // 發(fā)送語音驗證碼 send_voice(apikey,mobile,code); char *tmp; char *tpl_value = (char *)malloc(sizeof(char) * 500); bzero(tpl_value, sizeof(char)*500); // 模板短信發(fā)送需要編碼兩次,第一次URL編碼轉(zhuǎn)換 int len = 0; tmp = curl_easy_escape(curl,tpl_data[0],strlen(tpl_data[0])); memcpy(tpl_value+len,tmp,strlen(tmp)); len += strlen(tmp); tpl_value[len++] = '='; tmp = curl_easy_escape(curl,tpl_data[1],strlen(tpl_data[1])); memcpy(tpl_value+len,tmp,strlen(tmp)); len += strlen(tmp); tpl_value[len++] = '&'; tmp = curl_easy_escape(curl,tpl_data[2],strlen(tpl_data[2])); memcpy(tpl_value+len,tmp,strlen(tmp)); len += strlen(tmp); tpl_value[len++] = '='; tmp = curl_easy_escape(curl,tpl_data[3],strlen(tpl_data[3])); memcpy(tpl_value+len,tmp,strlen(tmp)); len += strlen(tmp); tmp=tpl_value; // 第二次URL編碼 tpl_value = curl_easy_escape(curl,tpl_value,strlen(tpl_value)); send_tpl_sms(apikey,mobile,tpl_id,tpl_value); free(tmp); curl_easy_cleanup(curl); return 0; }