編程學習網 > PHP技術 > php教程之如何開發手機版短信驗證碼
2021
09-09

php教程之如何開發手機版短信驗證碼

php驗證碼通知類短信接口代碼示例說明

注意事項:

//(1)調試期間,請使用用系統默認的短信內容:您的驗證碼是:【變量】。請不要把驗證碼泄露給其他人。

//(2)請使用 APIID 及 APIKEY來調用接口,可在會員中心獲取;

//(3)該代碼僅供接入互億無線短信接口參考使用,客戶可根據實際需要自行編寫;

// DEMO僅作參考


#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;

char *send_sms_uri = "/webservice/sms.php?method=Submit&format=json";


/**

* 發http post請求

*/

ssize_t http_post(char *page, char *poststr)

{

char sendline[MAXLINE + 1], recvline[MAXLINE + 1];

ssize_t n;

snprintf(sendline, MAXSUB,

"POST %s HTTP/1.0\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;

}


/**

* 發送短信

*/

ssize_t send_sms(char *account, char *password, char *mobile, char *content)

{

char params[MAXPARAM + 1];

char *cp = params;

sprintf(cp,"account=%s&password=%s&mobile=%s&content=%s", account, password, mobile, content);

return http_post(send_sms_uri, cp);

}


int socked_connect(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");

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);

//查看用戶名 登錄用戶中心->驗證碼通知短信>產品總覽->API接口信息->APIID

char *account = "用戶名";


//查看密碼 登錄用戶中心->驗證碼通知短信>產品總覽->API接口信息->APIKEY

char *password = "密碼";


//手機號

char *mobile = "138xxxxxxxx";


//短信內容

char *message = "您的驗證碼是:1212。請不要把驗證碼泄露給其他人。";


/**************** 發送短信 *****************/

send_sms(account, password, mobile, message);

printf("send the message is success\n");

close(basefd);

exit(0);

}

以上就是“php教程之如何開發手機版短信驗證碼”的詳細內容,想要了解更多php教程歡迎持續關注編程學習網

掃碼二維碼 獲取免費視頻學習資料

Python編程學習

查 看2022高級編程視頻教程免費獲取