c++利用飛信API免費發短信

1、飛信接口介紹php

    該飛信接口是基於HTTP協議的接口,能夠在任何支持HTTP協議的程序中使用。一些不能用PHP的朋友們能夠使用此飛信接口,另外能夠避免飛信不斷升級致使的源程序不可用。本接口同時支持HTTPS,可避免在網絡傳輸中泄漏數據。ios

    (1)飛信接口調用方式,參數同時支持GET、POST兩種方式 api

http://quanapi.sinaapp.com/fetion.php?u=飛信登陸手機號&p=飛信登陸密碼&to=接收飛信的手機號&m=飛信內容

(2)使用HTTPS調用飛信接口,只需將HTTP改成HTTPS 網絡

https://quanapi.sinaapp.com/fetion.php?u=飛信登陸手機號&p=飛信登陸密碼&to=接收飛信的手機號&m=飛信內容

 (3)返回結果爲Json格式,result=0時表示發送成功 app

{「result」:0,」message」:」\u53d1\u9001\u6210\u529f」}

    2、使用HTTPS調用飛信接口舉例函數

 一、頭文件以下:url

/************************************************************************/ 
/* 
文件名稱:FeiXin.h
功能介紹:利用飛信的免費API接口發送短信
版本:v1.00(第一版)
做者:moki
日期:2014.01.06
*/
/************************************************************************/
#ifndef __FEIXIN__H_
#define __FEIXIN__H_
#include <Windows.h>
#include <string>
#include <wininet.h>
#include <stdio.h>

 

#define FX_MAXSIZE 4096
#pragma comment(lib, "Wininet.lib") 
class CFeiXin
{
public:
 //發送短信
 static int FX_SendMessage(char* _usrname,char* _password,char* _number,char* _sms);
 //發送http請求
 static int FX_SendHttp(char* _url);
protected:
 CFeiXin(){}
 ~CFeiXin(){}
private:
 static std::string m_url;
};
#endif

二、cpp文件以下:spa

#include "FeiXin.h"
std::string CFeiXin::m_url = "https://quanapi.sinaapp.com/fetion.php";
int CFeiXin::FX_SendMessage(char* _usrname,char* _password,char* _number,char* _sms)
{
 int ret = 0;
 char chbuf[FX_MAXSIZE] = {0};
 sprintf(chbuf,"%s?u=%s&p=%s&to=%s&m=%s",m_url.c_str(),_usrname,_password,\
   _number,_sms);
 ret = FX_SendHttp(chbuf);
 return ret;
}
//發送http請求
int CFeiXin::FX_SendHttp(char* _url)
{
 int ret = 0;
 HINTERNET hSession = InternetOpen("UrlFeiXin", INTERNET_OPEN_TYPE_PRECONFIG, 
  NULL, NULL, 0);
 char strresult[256] = {0};
 if(hSession != NULL)
 {
  HINTERNET hHttp = InternetOpenUrl(hSession, _url, NULL, 0, 
   INTERNET_FLAG_DONT_CACHE, 0);
  if (hHttp != NULL)
  {
   BYTE Temp[FX_MAXSIZE];
   ULONG Number = 1;
   while (Number > 0)
   {
    InternetReadFile(hHttp, Temp, FX_MAXSIZE - 1, &Number);
    Temp[Number] = '\0';
    sprintf(strresult,"%s", Temp);
    //解析返回碼
    std::string str = strresult;
    int startpos = str.find_first_of(":")+1;
    int endpos = str.find_first_of(",");
    std::string strcode = str.substr(startpos,endpos-startpos);
    ret = atol(strcode.c_str());
   }
   InternetCloseHandle(hHttp);
   hHttp = NULL;
  }
  InternetCloseHandle(hSession);
  hSession = NULL;
 } 
 return ret;
}

三、main函數調用發送3d

#include <iostream>
#include "..\FeiXin\FeiXin.h"
using namespace std;
int main(int argc, char* argv[])
{
 //
 DWORD ret = CFeiXin::FX_SendMessage("飛信登陸手機號","飛信登陸密碼","接收飛信的手機號",
  "飛信內容");
 if (ret != 0)
 {
  cout << "發送短信失敗" << endl;
 }
 else
 {
  cout << "發送短信成功" << endl;
 }
 return 0;
}

使用vs2010編譯經過,源碼以下:code

http://yunpan.cn/QzJ3VmLT9iFmY 

訪問密碼請發我郵箱索取,個人郵箱是:

mokimail126@126.com

相關文章
相關標籤/搜索