企业信使短信接口API代码示例

PHP 代码实例
<?php
/**

  • Created by Zhongxinrongda.
  • Date: 2017/3/3
  • Time: 14:34
  • 功能:云信通企业信使短信接口类
  • 说明:
    一下代码只是提供简单的功能,方便客户的测试,如有其他需求,客户可根据实际自行更改代码。
    /
    class smsApi{
    /*
    • @param string $sms_send_url 短信发送接口url
    • @param string $sms_query_url 短信余额查询接口url
    • @param string $userid 企业id
    • @param string $account 短信账户
    • @param string $password 账户密码
      */
      var $sms_send_url=‘‘;
      var $sms_query_url=‘‘;
      var $userid=‘‘;
      var $account=‘‘;
      var $password=‘‘;
      public function sendSms($mobile,$content,$sendTime=‘‘){
      $post_data=array(
      ‘userid‘=>$this->userid,
      ‘account‘=>$this->account,
      ‘password‘=>$this->password,
      ‘mobile‘=>$mobile,
      ‘content‘=>$content,
      ‘sendTime‘=>$sendTime //发送时间,为空是即时发送
      );
      $url=$this->sms_send_url;
      $o=‘‘;
      foreach ($post_data as $k=>$v)
      {
      $o.="$k=".urlencode($v).‘&‘;
      }
      $post_data=substr($o,0,-1);
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
      //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要将结果直接返回到变量里,那加上这句。
      $result = curl_exec($ch);
      curl_close($ch);
      return $result;
      }
      }

Java 代码实例

package com.zxrd.interfacej;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**

  • 云信通企业信使短信接口Java示例
  • @param url 应用地址,类似于http://ip:port/sms.aspx
  • @param userid 用户ID
  • @param account 账号
  • @param pssword 密码
  • @param mobile 手机号码,多个号码使用","分割
  • @param content 短信内容
  • @return 返回值定义参见HTTP协议文档
  • @throws Exception
    */

public class Sms {
public static String HTTPPost(String sendUrl, String sendParam) {
String codingType = "UTF-8";
StringBuffer receive = new StringBuffer();
BufferedWriter wr = null;
try {
//建立连接
URL url = new URL(sendUrl);
HttpURLConnection URLConn = (HttpURLConnection) url.openConnection();
URLConn.setDoOutput(true);
URLConn.setDoInput(true);
((HttpURLConnection) URLConn).setRequestMethod("POST");
URLConn.setUseCaches(false);
URLConn.setAllowUserInteraction(true);
HttpURLConnection.setFollowRedirects(true);
URLConn.setInstanceFollowRedirects(true);
URLConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
URLConn.connect();

        DataOutputStream dos = new DataOutputStream(URLConn.getOutputStream());
        dos.writeBytes(sendParam);
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                URLConn.getInputStream(), codingType));
        String line;
        while ((line = rd.readLine()) != null) {
            receive.append(line).append("\r\n");
        }
        rd.close();
    } catch (java.io.IOException e) {
        receive.append("访问产生了异常-->").append(e.getMessage());
        e.printStackTrace();
    } finally {
        if (wr != null) {
            try {
                wr.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            wr = null;
        }
    }
    return receive.toString();
}

//发送短信
public static String send(String sendUrl, String userid, String account,
        String password, String mobile, String content) {
    String codingType = "UTF-8";
    StringBuffer sendParam = new StringBuffer();
    try {
        sendParam.append("action=").append("send");
        sendParam.append("&userid=").append(userid);
        sendParam.append("&account=").append(URLEncoder.encode(account, codingType));
        sendParam.append("&password=").append(URLEncoder.encode(password, codingType));
        sendParam.append("&mobile=").append(mobile);
        sendParam.append("&content=").append(URLEncoder.encode(content, codingType));
    } catch (Exception e) {
        //处理异常
        e.printStackTrace();
    }
    return Sms.HTTPPost(sendUrl,sendParam.toString());
}

//查询余额
public static String Overage(String sendUrl, String userid, String account,
        String password) {
    String codingType = "UTF-8";
    StringBuffer sendParam = new StringBuffer();
    try {
        sendParam.append("action=").append("overage");
        sendParam.append("&userid=").append(userid);
        sendParam.append("&account=").append(URLEncoder.encode(account, codingType));
        sendParam.append("&password=").append(URLEncoder.encode(password, codingType));
    } catch (Exception e) {
        //处理异常
        e.printStackTrace();
    }
    return Sms.HTTPPost(sendUrl,sendParam.toString());
}

public static String url = "http://ip:port/msg/";   //对应平台地址
public static String userid = "0001";   //客户id
public static String account = "xxxx";  //账号
public static String password = "123456";   //密码
public static String mobile = "13000000000";    //手机号码,多个号码使用","分割
public static String content= "尊敬的用户您的验证码是:123456【你的签名】";   //短信内容

public static void main(String[] args) {
    //发送短信
    String sendReturn = Sms.send(url, userid, account, password, mobile, content);
    System.out.println(sendReturn);//处理返回值,参见HTTP协议文档

    //查询余额
    String overReturn = Sms.Overage(url, userid, account, password);
    System.out.println(overReturn);//处理返回值,参见HTTP协议文档
}

}

.NET(webform)代码实例

//发送短信的方法,phone:手机号码,content:短信内容
    public static void smsSend(string phone,string content)
    {
        string userid = "*";//企业ID
        string account = "*";           //用户名
        string password = "*";  //密码
        StringBuilder sbTemp = new StringBuilder();
        //POST 传值
        sbTemp.Append("action=send&userid=" + userid + "&account=" + account + "&password=" + password + "&mobile=" + phone + "&content=" + content);
        byte[] bTemp = System.Text.Encoding.GetEncoding("GBK").GetBytes(sbTemp.ToString());
        String postReturn = doPostRequest("请求地址", bTemp);

        //解析返回的XML数据
         XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(postReturn);
            XmlNode xmlNode = xmlDoc.SelectSingleNode("returnsms/returnstatus");
            string value = xmlNode.FirstChild.Value;    //Success表示发送成功
    }

    private static String doPostRequest(string url, byte[] bData)
    {
        System.Net.HttpWebRequest hwRequest;
        System.Net.HttpWebResponse hwResponse;

        string strResult = string.Empty;
        try
        {
            hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            hwRequest.Timeout = 5000;
            hwRequest.Method = "POST";
            hwRequest.ContentType = "application/x-www-form-urlencoded";
            hwRequest.ContentLength = bData.Length;

            System.IO.Stream smWrite = hwRequest.GetRequestStream();
            smWrite.Write(bData, 0, bData.Length);
            smWrite.Close();
        }
        catch (System.Exception err)
        {
            WriteErrLog(err.ToString());
            return strResult;
        }

        //get response
        try
        {
            hwResponse = (HttpWebResponse)hwRequest.GetResponse();
            StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
            strResult = srReader.ReadToEnd();
            srReader.Close();
            hwResponse.Close();
        }
        catch (System.Exception err)
        {
            WriteErrLog(err.ToString());
        }

        return strResult;
    }

    private static void WriteErrLog(string strErr)
    {
        Console.WriteLine(strErr);
        System.Diagnostics.Trace.WriteLine(strErr);
 &nnbsp;  }

.NET(winform) 代码实例

public string HttpPost(string uri, string parameters)
{

        WebRequest webRequest = WebRequest.Create(uri);

        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.Method = "POST";
        byte[] bytes = Encoding.UTF8.GetBytes(parameters);//这里需要指定提交的编码
        System.GC.Collect();
        Stream os = null;
        try
        { // send the Post
            webRequest.ContentLength = bytes.Length;   //Count bytes to send
            os = webRequest.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);         //Send it
            os.Flush();
            os.Close();
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message, "HttpPost: Request error",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        try
        { // get the response
            WebResponse webResponse = webRequest.GetResponse();
            if (webResponse == null)
            { return null; }
            StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.UTF8);
            //上面一句需要将返回的编码进行指定,指定成默认的即可
            return sr.ReadToEnd().Trim();

        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message, "HttpPost: Response error",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

  private void button3_Click(object sender, EventArgs e)
    {
         string url="";
         string userid="";
         string account="";
         string password="";
        if (checkBox1.Checked)

        else

    }

ASP 代码实例

<%
Function getHTTPPage(url)
Dim Http
Set Http = Server.CreateObject("MSXML2.XMLHTTP")
Http.Open "Get", url, False
Http.send()
If Http.readystate <> 4 Then
Exit Function
End If
getHTTPPage = BytesToBstr(Http.responseBody, "UTF-8")
Set Http = Nothing
If Err.Number <> 0 Then Err.Clear
End Function

Function BytesToBstr(body, Cset)
Dim objstream
Set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode = 3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
Set objstream = Nothing
End Function

response.write getHTTPPage("请求地址?action=send&userid=企业ID&account=账号&password=密码&mobile=手机号码&content=内容&sendTime=&extno=")

%>

PYTHON 代码实例

-- coding: utf-8 --

#特别注意:参数传递时去除“<>”符号!
import requests;
import json;
def send_messag_example():
resp = requests.post(("<接口地址>"),
data={
"action": "send",
"userid": "<企业id>",
"account": "<客户用户名>",
"password": "<客户密码>",
"mobile": "<手机号码>",
"content": "<短信内容>",
"type": "json"
},timeout=3 , verify=False);
result = json.loads( resp.content )
print result
if name == "main":
send_messag_example();
#注意:以上参数传入时不包括“<>”符号

C++ 代码实例

#define MAXLINE 4096
#define MAXSUB 2000
#define MAXPARAM 2048

char *hostname = "123.59.105.84";//相应服务器IP

/**

    • 发http post请求
  • /
    ssize_t http_post(char
    poststr)
    {
    char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
    ssize_t n;
    snprintf(sendline, MAXSUB,
    "POST %s HTTP/1.0\r\n"
    "Host: URL\r\n"//URL请求地址
    "Content-type: application/x-www-form-urlencoded\r\n"
    "Content-length: %zu\r\n\r\n"
    "%s", strlen(poststr), poststr);
    write(sockfd, sendline, strlen(sendline));
    printf("\n%s", sendline);
    printf("\n--------------------------\n");
    while ((n = read(sockfd, recvline, MAXLINE)) > 0) {
    recvline[n] = ‘\0‘;
    printf("%s\n", recvline);
    }
    return n;
    }
    • 发送短信
  • /
    ssize_t send_sms(char
    account, char password, char mobile, char content)
    {
    char params[MAXPARAM + 1];
    char
    cp = params;

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

    return http_post(cp);
    }

int main(void)
{
struct sockaddr_in servaddr;
char str[50];

//建立socket连接
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_addr.s_addr = inet_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));

char *userid= "企业ID"
char *account = "账号";
char *password = "密码";
char *mobile = "手机号";
//必须带签名
char *msg = "【签名】您的验证码是123400";

//get_balance(account, password);
send_sms(account, password, mobile, content);
close(sockfd);
exit(0);

}

VB 代码实例
Public Function getHtmlStr(strUrl As String) ‘获取远程接口函数
On Error Resume Next
Dim XmlHttp As Object, stime, ntime
Set XmlHttp = CreateObject("Microsoft.XMLHTTP")
XmlHttp.open "GET", strUrl, True
XmlHttp.send
stime = Now ‘获取当前时间
While XmlHttp.ReadyState <> 4
DoEvents
ntime = Now ‘获取循环时间
If DateDiff("s", stime, ntime) > 3 Then getHtmlStr = "": Exit Function
Wend
getHtmlStr = StrConv(XmlHttp.responseBody, vbUnicode)
Set XmlHttp = Nothing
End Function

代码使用:在窗体代码相应位置写如下代码
dim a as string
a=getHtmlStr("url?action=send&userid=企业ID&account=账号&password=密码&mobile=手机号码&content=内容&sendTime=&extno=) ‘获取接口返回值

PB 代码实例

建个对象n_ir_msgbox,继承自internetresult,直接在internetdata函数中返回1(这一步很关键,必须有个返回值)

建立窗口,定义实例变量n_ir_msgbox iir_msgbox

增加按钮,click事件中:

inet linet_base
String ls_url
integer li_rc

iir_msgbox = CREATE n_ir_msgbox

if GetContextService("Internet", linet_base) = 1 THEN

ls_url = "URL?action=send&userid=企业ID&account=帐号&password=密码&mobile=手机号码&content=短信内容"

li_rc = linet_base.GetURL(ls_url, iir_msgbox)

END IF

DESTROY iir_msgbox

原文地址:http://blog.51cto.com/13569310/2059601

时间: 2024-10-08 04:39:41

企业信使短信接口API代码示例的相关文章

Redis实践操作之—— 直播视频定时控制【TCP长连接框架(WorkerMan)+键空间通知的机制 ( Keyspace Notifications)+短信接口(API)】

一.思路梳理 同步直播视频到Redis 用户观看直播模板,点击直播按钮,检查是否有权限. 直播定时免费观看(免费观看10分钟),用户点击播放按钮开始,异步检查获取直播活动设置的免费观看时间(后台维护人员设置,Redis的hash存储信息),是否是直播. 是直播视频:判断该客户是否已经观看过了免费的20分钟时间, 没有看过,则获取该直播视频的免费时间根据活动ID,同时设置该直播视频的过期时间(只针对该用户自己哦),返回个模板,说:这个人可以观看的. 直播视频已经看过了,则不可以继续观看哦!嘻嘻..

短信接口api、聚合科技、

import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.

短信接口API

/** * Created by bingone on 15/12/16. */ import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apach

短信接口对接代码

1. Create a service on the RAC cluster to setup for TAF. Example creates a service called server_taf to the database called rac, with instance names rac1 & rac2. Please note for the service name, select a name that is unique and not the same as the d

c#短信接口代码实现(发短信)

我这里以56短信接口为例! 首先需要到56短信网上去注册个账号,代码中需要用到4个参数:企业ID.用户名.密码.所用平台 在里面充钱配合下面的代码就可以发送短信啦! public void Send(string orderIDs, string mobile, string productName)    { //下文中的tel:就是要发送的手机号码 //msg:要发送的内容        //发送短信        string cid = "1345";//企业号ID      

阿里大鱼短信接口 for Thinkphp

不得不说阿里云为创业者在起步时期提供了不少便利,短信接口不管是web应用还是APP都必不可少.大鱼一张口,这是要生吞其他小虾米的节奏啊,四分的价格很便宜了,现在注册会送60元代金券,时间不到一个月,也就是让你免费调试,短信发送效果很好,基本能够达到承诺的三秒,一般情况两三秒,具体效果还待检验,好了,直奔主题. 第一步:注册阿里大鱼账号:http://www.alidayu.com 第二步:进入<开发者控制台>--><应用管理>中创建应用,应用标签选择"阿里大鱼&qu

短信接口的产品优势在哪里?

大家都知道,短信接口请添加链接描述是面向有一定技术开发能力的企业而单独研发的,是系统语言开发支持的汇总,支持多种开发语言,适应多种运行环境,有效解决了各企业对于大批量短信群发的需求,做到无缝衔接.那么短信接口的产品优势都在哪里呢? 一.提升服务质量请添加链接描述 通过对短信接口的应用,企业可以说是搭建了与客户之间的有效沟通渠道,快速搜集.掌握目标客户及潜在客户的意见和需求,从而有针对性的开展服务,更好的满足客户的需求.短信接口的上下行功能,不仅能发送短信,还能接收短信,相当于强化了企业与客户之间

速码云信息-----短信接口代码参数

短信接口(HTTP协议)向指定手机号码发送短信息,内容由客户自定义,HTTP接口为GB2312编码.使用前需要先注册帐号并于客户经理取得联系,开启授权,充值测试费用短信验证码30分钟相同手机号发送请勿超过5条,其余内容相同号码接收量控制在<=20温馨提示! 尊敬的客户,为了你调用我司接口快速收到不被拦截,请在调用接口的发送短信的时候请发送正规的验证码内容,如: [速码云]尊敬的客户你好,你获取的验证码为:548976.或者发送祝福短信.切勿发送违规,违法,辱骂等短信内容,谢谢!发送短信余额接口相

短信接口调用——阿里大于API开发心得

互联网上有许多公司提供短信接口服务,诸如网易云信.阿里大于等等.我在自己项目里需要使用到短信服务起到通知作用,实际开发周期三天,完成配置.开发和使用,总的说,阿里大于提供的接口易于开发,非常的方便,短信费用是计数缴纳的,作为个人开发者,我使用的服务产生的费用为0.45¥/条(10万条以下). 现在要实现一个例会短信群发通知的功能,所有被通知对象信息均存于Mysql中,应用架构采用asp.net MVC .首先准备好获取的API各项(以下各项服务参数都需要在大于官网上申请), 申请好自己的短信签名