本文為您提供了C++語言版本的短信單條發(fā)送接口對(duì)接DEMO示例
//linux下的編譯:g++ ./test.cpp -o ./test
//linux下的執(zhí)行:chmod +x test &&./test
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <unistd.h>
#define SA struct sockaddr
#define MAXLINE 4096
#define MAXSUB 2000
#define MAXPARAM 2048
#define LISTENQ 1024
extern int h_errno;
int basefd;
const char *hostname = "api.ihuyi.com";
const char *send_sms_uri = "/sms/Submit.json";
/**
* 發(fā)http post請(qǐng)求
*/
ssize_t http_post(const char *page, const char *poststr)
{
char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
ssize_t n;
snprintf(sendline, MAXSUB,
"POST %s HTTP/1.1\r\n"
"Host: %s\r\n"
"Content-type: application/x-www-form-urlencoded\r\n"
"Content-length: %zu\r\n\r\n"
"%s", page, hostname, strlen(poststr), poststr);
write(basefd, sendline, strlen(sendline));
while ((n = read(basefd, recvline, MAXLINE)) > 0) {
recvline[n] = '\0';
printf("%s", recvline);
}
return n;
}
int socked_connect(const char *arg)
{
struct sockaddr_in their_addr = {0};
char buf[1024] = {0};
char rbuf[1024] = {0};
char pass[128] = {0};
struct hostent *host = NULL;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0)
{
printf ("create the sockfd is failed\n");
return -1;
}
if((host = gethostbyname(arg))==NULL)
{
printf("Gethostname error: %s\n", hstrerror(h_errno));
return -1;
}
memset(&their_addr, 0, sizeof(their_addr));
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(80);
their_addr.sin_addr = *((struct in_addr *)host->h_addr);
if(connect(sockfd,(struct sockaddr *)&their_addr, sizeof(struct sockaddr)) < 0)
{
close(sockfd);
return -1;
}
printf ("connect is success\n");
return sockfd;
}
int main(void)
{
struct sockaddr_in servaddr;
char str[50];
#if 0
//建立socket連接
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_addr =*(hostname);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(80);
inet_pton(AF_INET, str, &servaddr.sin_addr);
connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
#endif
if((basefd= socked_connect(hostname))==-1)
{
printf("connect is failed\n");
return -1;
}
printf("basefd is =%d\n",basefd);
const char *account = "xxxxxxxx"; //APIID(用戶中心【文本短信】-【驗(yàn)證碼/通知短信】-【產(chǎn)品總覽】查看)
const char *password = "xxxxxxxxx"; //1、APIKEY(用戶中心【文本短信】-【驗(yàn)證碼/通知短信】-【產(chǎn)品總覽】查看)
2、動(dòng)態(tài)密碼(生成動(dòng)態(tài)密碼方式請(qǐng)看該文檔末尾的說明)
const char *mobile = "136xxxxxxxx"; //根據(jù)發(fā)送方式不同:
1、完整內(nèi)容方式
提交完整的短信內(nèi)容,如:您的驗(yàn)證碼是:1234。請(qǐng)不要把驗(yàn)證碼泄露給其他人。
2、模板變量方式
模板中的變量內(nèi)容,多個(gè)變量以英文豎線(|)隔開
①單變量示例
模板內(nèi)容:您的驗(yàn)證碼是:【變量】。請(qǐng)不要把驗(yàn)證碼泄露給其他人。
參數(shù)寫法:content=1234
最終短信為:您的驗(yàn)證碼是:1234。請(qǐng)不要把驗(yàn)證碼泄露給其他人。
②多變量示例
模板內(nèi)容:訂單號(hào):【變量1】,聯(lián)系人:【變量2】,手機(jī)號(hào):【變量3】,金額:【變量4】。
參數(shù)寫法:content=20180515006|張三|136xxxxxxxx|100元
最終短信為:訂單號(hào):20180515006,聯(lián)系人:張三,手機(jī)號(hào):136xxxxxxxx,金額:100元。
支持500字以內(nèi)的長短信,長短信按多條計(jì)費(fèi)
const char *content = "您的驗(yàn)證碼是:1234。請(qǐng)不要把驗(yàn)證碼泄露給其他人。"; //短信內(nèi)容
const char *templateid = "1"; //短信模板ID(使用模板變量方式發(fā)送時(shí)必填)
調(diào)試階段可使用系統(tǒng)默認(rèn)模板ID:1(模板內(nèi)容為:您的驗(yàn)證碼是:【變量】。請(qǐng)不要把驗(yàn)證碼泄露給其他人。)
const char *time = "1623643787"; //Unix時(shí)間戳(10位整型數(shù)字,當(dāng)使用動(dòng)態(tài)密碼方式時(shí)為必填)
char params[MAXPARAM + 1];
char *cp = params;
snprintf(cp, MAXPARAM + 1, "account=%s&password=%s&mobile=%s&content=%s&templateid=%s&time=%s&s=%s", account,password,mobile,content,templateid,time, "s");
http_post(send_sms_uri, cp);
printf("send the message is success\n");
close(basefd);
exit(0);
}
國內(nèi)專業(yè)互聯(lián)網(wǎng)團(tuán)隊(duì)
21年行業(yè)經(jīng)驗(yàn)
7x24小時(shí)售后支持
豐富的行業(yè)經(jīng)驗(yàn)
互 億 無 線 官 方 微 信 號(hào)
Copyright ? 2004-2025 上海思銳信息技術(shù)有限公司 All rights reserved. 滬ICP備07035915號(hào)-15 電信增值業(yè)務(wù)許可證:B2-20160082
服務(wù)熱線:
4008 808 898
服務(wù)熱線(工作時(shí)間):
4008 808 898
業(yè)務(wù)咨詢(非工作時(shí)間):
售后咨詢(非工作時(shí)間):