TwSMS 發簡訊 (Linux C 版)

一月 21st, 2008

台灣簡訊(TwSMS)是國內一家線上傳簡訊的服務商,提供文字簡訊、語音簡訊等服務,價格也很合理,最重要的是有提供 API 介面,方便用戶在自己的程式中加入發送簡訊功能,官網已經有提供不少範例(PHP/ASP/JSP/Java/Perl/VB/BCB/Delphi),這邊也有 Ruby 的版本,不過就是沒看到 C 的,所以大略寫了一個 Linux C 版本,打算加入自己的嵌入式專題使用。

TwSMS 提供的 API 很簡單,只要由 HTTP 對 API server 發送 Request 即可,接著 server 就會回傳結果。程式先建立一個 socket 連線,然後發送簡訊,最後再擷取回傳碼檢查是否成功。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int main()
{
	/* TWSMS 相關設定 */
	char *username = "username";   // 帳號
	char *password = "password"; // 密碼
	char *type = "now";         // 發送型態
	char *mobile = "0912xxxxxx"; // 電話
	char *message = "簡訊測試"; // 簡訊內容
	char *encoding = "big5";    // 簡訊內容編碼
	char *popup = "";          // 使用 POPUP 顯示
	char *mo = "";             // 使用雙向簡訊
	char *vldtime = "86400";    // 簡訊有效期限(秒)
	char *dlvtime = "";         // 預約時間

	int sockfd;
	int len = 0;
	char *host = "api.twsms.com";
	char msg[512], MSGData[512], buf[512];
	char *res, *checkRes;
	struct sockaddr_in address;
	struct hostent *hostinfo;

	bzero(&address, sizeof(address));
	hostinfo = gethostbyname(host);
	if (!hostinfo) {
		fprintf(stderr, "no host: %s\n", host);
		exit(1);
	}
	address.sin_family = AF_INET;
	address.sin_port = htons(80);
	address.sin_addr = *(struct in_addr *)*hostinfo->h_addr_list;

	/* Create socket */
	sockfd = socket(AF_INET, SOCK_STREAM, 0);

	/* Connect to server */
	if (connect(sockfd, (struct sockaddr *)&address, sizeof(address)) == -1) {
		perror("connect faild!\n");
		exit(1);
	}

	/* Request string */
	len = snprintf(msg, 512,
				  "username=%s&password=%s&type=%s&encoding=%s&popup=%s&mo=%s&mobile=%s"
				  "&message=%s&vldtime=%s&dlvtime=%s", username, password, type, encoding,
				  popup, mo, mobile, message, vldtime, dlvtime);

	/* HTTP request content */
	snprintf(MSGData, 512,
			"POST /send_sms.php HTTP/1.1\r\n"
			"Host: %s\r\n"
			"Content-Length: %d\r\n"
			"Content-Type: application/x-www-form-urlencoded\r\n"
			"Connection: Close\r\n\r\n"
			"%s\r\n", host, len, msg);

	/* Send message */
	send(sockfd, MSGData, strlen(MSGData), 0);

	/* Response message */
	recv(sockfd, buf, 512, 0);

	for (res = strtok(buf, "\n"); strncmp(res, "resp", 4) != 0; res = strtok(NULL, "\n"));
	checkRes = strtok(res, "=");
	checkRes = strtok(NULL, "=");

	if (atoi(checkRes) <= 0) {
		printf("傳送失敗\n");
	} else {
		printf("傳送完成\n");
	}

	close(sockfd);
	return 0;
}

C/C++, 程式筆記 , , ,

  1. charlie
    二月 4th, 2008 at 08:57 | #1

    您這套發簡訊能夠在FreeBSD執行嗎
    我是過好像不行,有很多的TCP API不清楚是否
    FreeBSD能提供,或者還要配合您指定的一些LIBRARY
    Charlie

  2. 二月 5th, 2008 at 00:53 | #2

    charlie:
    今天特地裝了 FreeBSD 6.3 來測試,只安裝基本套件並設定好網路而已,結果是完全 OK 的
    此程式都是標準 Unix socket 函式,請您再試試看囉!

  3. shanhan.yu
    三月 15th, 2008 at 03:44 | #3

    你好
    最近要開始著手製作專題
    其中有個功能就是要發送簡訊到手機上面
    因為之前就有稍微看過說可以架設linux然後發送
    但是不太了解這個
    所以有幾點事情想要請問你一下

    透過主機發送簡訊到手機這較啥名稱/技術呢?
    需要收取任何費用嗎!?還是說主機架設好就可以了

  4. 三月 16th, 2008 at 10:12 | #4

    shanhan.yu:
    您好,大致上來說發簡訊的方式有兩種。
    一種就是透過簡訊服務商,如 Hinet 及本篇介紹的 TwSMS,這些服務商會提供一套 API 讓您在自己的程式裡使用,所以只要撰寫 socket 程式和 API 溝通即可,費用依服務商規定收取。

    另一種方式就是自行架一台 SMS Server,這台 server 必須要有 GSM Modem,並且需要插上您的 SIM 卡,再寫程式去向 Modem 發送 AT 指令集,來達到傳簡訊的功能,費用就和一般手機傳簡訊費率相同。

  5. featarhers
    七月 9th, 2008 at 14:15 | #5

    在專題使用了台灣簡訊,下載BCB範例並修改後可以使用,但如移植到自己的程式中執行,只出現回傳碼=20的message box,不知到底是哪裡不對。請指教! 謝謝!

  6. featarhers
    七月 9th, 2008 at 14:42 | #6

    阿...問題解決了..原來是我在HTTP元件的設定沒有設好。打擾了,真不好意思^^"。
    還是謝謝您~!

  7. 一月 18th, 2009 at 18:51 | #7

    Hi!
    我是那個Ruby版本的原開發者
    我重新寫了一份Ruby的TWSMS Library
    如果可以,還希望您可以將網址換成:http://github.com/cfc/twsmsr/tree/master
    感激不盡:D

  8. 一月 19th, 2009 at 11:37 | #8

    @CFC
    已更新囉!

  9. C
    九月 8th, 2009 at 20:03 | #9

    我有問題想問 在媽?

  10. 九月 10th, 2009 at 14:38 | #10

    @C
    請問

Comment pages
  1. No trackbacks yet.