支持开源精神,欢迎贡献代码示例,审核通过可获短信优惠或奖励! 我要贡献

已提交的第三方源码(注:所有第三方源码均为开发者贡献,仅供参考使用): Github

本样例基于 Apache HttpClient v4.3可下载完整样例项目。 同时,也可 下载基于3.1版的完整样例项目。 样例项目包含代码、依赖 jar包和pom.xml配置

Windows用户请注意:下载的代码及说明文件都是UTF-8格式编码,且换行风格为Linux的。 用记事本或写字板直接打开显示会不正常。建议将其导入(或复制)到Eclipse等IDE, 并将项目编码格式设置为UTF-8后再打开这些文件查看。若直接查看请用Notepad++、EditPlus等编辑器。

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。


/**
* 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.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 短信http接口的java代码调用示例
* 基于Apache HttpClient 4.3
*
* @author songchao
* @since 2015-04-03
*/

public class JavaSmsApi {

    //查账户信息的http地址
    private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v2/user/get.json";

    //智能匹配模板发送接口的http地址
    private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";

    //模板发送接口的http地址
    private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";

    //发送语音验证码接口的http地址
    private static String URI_SEND_VOICE = "https://voice.yunpian.com/v2/voice/send.json";

    //绑定主叫、被叫关系的接口http地址
    private static String URI_SEND_BIND = "https://call.yunpian.com/v2/call/bind.json";

    //解绑主叫、被叫关系的接口http地址
    private static String URI_SEND_UNBIND = "https://call.yunpian.com/v2/call/unbind.json";

    //编码格式。发送编码格式统一用UTF-8
    private static String ENCODING = "UTF-8";

    public static void main(String[] args) throws IOException, URISyntaxException {

        //修改为您的apikey.apikey可在官网(http://www.yunpian.com)登录后获取
        String apikey = "xxxxxxxxxxxxxxxxxxxxx";

        //修改为您要发送的手机号
        String mobile = "130xxxxxxxx";

        /**************** 查账户信息调用示例 *****************/
        System.out.println(JavaSmsApi.getUserInfo(apikey));

        /**************** 使用智能匹配模板接口发短信(推荐) *****************/
        //设置您要发送的内容(内容必须和某个模板匹配。以下例子匹配的是系统提供的1号模板)
        String text = "【云片网】您的验证码是1234";
        //发短信调用示例
        // System.out.println(JavaSmsApi.sendSms(apikey, text, mobile));

        /**************** 使用指定模板接口发短信(不推荐,建议使用智能匹配模板接口) *****************/
        //设置模板ID,如使用1号模板:【#company#】您的验证码是#code#
        long tpl_id = 1;
        //设置对应的模板变量值

        String tpl_value = URLEncoder.encode("#code#",ENCODING) +"="
        + URLEncoder.encode("1234", ENCODING) + "&"
        + URLEncoder.encode("#company#",ENCODING) + "="
        + URLEncoder.encode("云片网",ENCODING);
        //模板发送的调用示例
        System.out.println(tpl_value);
        System.out.println(JavaSmsApi.tplSendSms(apikey, tpl_id, tpl_value, mobile));

        /**************** 使用接口发语音验证码 *****************/
        String code = "1234";
        //System.out.println(JavaSmsApi.sendVoice(apikey, mobile ,code));

        /**************** 使用接口绑定主被叫号码 *****************/
        String from = "+86130xxxxxxxx";
        String to = "+86131xxxxxxxx";
        Integer duration = 30*60;// 绑定30分钟
        //        System.out.println(JavaSmsApi.bindCall(apikey, from ,to , duration));

        /**************** 使用接口解绑主被叫号码 *****************/
        //        System.out.println(JavaSmsApi.unbindCall(apikey, from, to));
    }

    /**
    * 取账户信息
    *
    * @return json格式字符串
    * @throws java.io.IOException
    */

    public static String getUserInfo(String apikey) throws IOException, URISyntaxException {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        return post(URI_GET_USER_INFO, params);
    }

    /**
    * 智能匹配模板接口发短信
    *
    * @param apikey apikey
    * @param text    短信内容
    * @param mobile  接受的手机号
    * @return json格式字符串
    * @throws IOException
    */

    public static String sendSms(String apikey, String text, String mobile) throws IOException {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        params.put("text", text);
        params.put("mobile", mobile);
        return post(URI_SEND_SMS, params);
    }

    /**
    * 通过模板发送短信(不推荐)
    *
    * @param apikey    apikey
    * @param tpl_id     模板id
    * @param tpl_value  模板变量值
    * @param mobile     接受的手机号
    * @return json格式字符串
    * @throws IOException
    */

    public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile) throws IOException {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        params.put("tpl_id", String.valueOf(tpl_id));
        params.put("tpl_value", tpl_value);
        params.put("mobile", mobile);
        return post(URI_TPL_SEND_SMS, params);
    }

    /**
    * 通过接口发送语音验证码
    * @param apikey apikey
    * @param mobile 接收的手机号
    * @param code   验证码
    * @return
    */

    public static String sendVoice(String apikey, String mobile, String code) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        params.put("mobile", mobile);
        params.put("code", code);
        return post(URI_SEND_VOICE, params);
    }

    /**
    * 通过接口绑定主被叫号码
    * @param apikey apikey
    * @param from 主叫
    * @param to   被叫
    * @param duration 有效时长,单位:秒
    * @return
    */

    public static String bindCall(String apikey, String from, String to , Integer duration ) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        params.put("from", from);
        params.put("to", to);
        params.put("duration", String.valueOf(duration));
        return post(URI_SEND_BIND, params);
    }

    /**
    * 通过接口解绑绑定主被叫号码
    * @param apikey apikey
    * @param from 主叫
    * @param to   被叫
    * @return
    */
    public static String unbindCall(String apikey, String from, String to) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        params.put("from", from);
        params.put("to", to);
        return post(URI_SEND_UNBIND, params);
    }

    /**
    * 基于HttpClient 4.3的通用POST方法
    *
    * @param url       提交的URL
    * @param paramsMap 提交<参数,值>Map
    * @return 提交响应
    */

    public static String post(String url, Map<String, String> paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
            try {
                HttpPost method = new HttpPost(url);
                if (paramsMap != null) {
                    List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                    for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                        NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
                        paramList.add(pair);
                    }
                    method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
                }
                response = client.execute(method);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    responseText = EntityUtils.toString(entity, ENCODING);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return responseText;
        }
}

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

#!/usr/local/bin/python
#-*-coding:utf-8-*-

#author: jacky
# Time: 15-12-15
# Desc: 短信http接口的python代码调用示例
# https://www.yunpian.com/api/demo.html
# https访问,需要安装  openssl-devel库。apt-get install openssl-devel

import httplib
import urllib
import json
#服务地址
sms_host = "sms.yunpian.com"
voice_host = "voice.yunpian.com"
#端口号
port = 443
#版本号
version = "v2"
#查账户信息的URI
user_get_uri = "/" + version + "/user/get.json"
#智能匹配模板短信接口的URI
sms_send_uri = "/" + version + "/sms/single_send.json"
#模板短信接口的URI
sms_tpl_send_uri = "/" + version + "/sms/tpl_single_send.json"
#语音短信接口的URI
sms_voice_send_uri = "/" + version + "/voice/send.json"
#语音验证码
voiceCode = 1234
def get_user_info(apikey):
    """
    取账户信息
    """
    conn = httplib.HTTPSConnection(sms_host , port=port)
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    conn.request('POST',user_get_uri,urllib.urlencode( {'apikey' : apikey}))
    response = conn.getresponse()
    response_str = response.read()
    conn.close()
    return response_str

def send_sms(apikey, text, mobile):
    """
    通用接口发短信
    """
    params = urllib.urlencode({'apikey': apikey, 'text': text, 'mobile':mobile})
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    conn = httplib.HTTPSConnection(sms_host, port=port, timeout=30)
    conn.request("POST", sms_send_uri, params, headers)
    response = conn.getresponse()
    response_str = response.read()
    conn.close()
    return response_str

def tpl_send_sms(apikey, tpl_id, tpl_value, mobile):
    """
    模板接口发短信
    """
    params = urllib.urlencode({'apikey': apikey, 'tpl_id':tpl_id, 'tpl_value': urllib.urlencode(tpl_value), 'mobile':mobile})
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    conn = httplib.HTTPSConnection(sms_host, port=port, timeout=30)
    conn.request("POST", sms_tpl_send_uri, params, headers)
    response = conn.getresponse()
    response_str = response.read()
    conn.close()
    return response_str

def send_voice_sms(apikey, code, mobile):
    """
    通用接口发短信
    """
    params = urllib.urlencode({'apikey': apikey, 'code': code, 'mobile':mobile})
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    conn = httplib.HTTPSConnection(voice_host, port=port, timeout=30)
    conn.request("POST", sms_voice_send_uri, params, headers)
    response = conn.getresponse()
    response_str = response.read()
    conn.close()
    return response_str

if __name__ == '__main__':
    #修改为您的apikey.可在官网(http://www.yunpian.com)登录后获取
    apikey = "xxxxxxxxxxxxxxxx"
    #修改为您要发送的手机号码,多个号码用逗号隔开
    mobile = "xxxxxxxxxxxxxxxx"
    #修改为您要发送的短信内容
    text = "【云片网】您的验证码是1234"
    #查账户信息
    print(get_user_info(apikey))
    #调用智能匹配模板接口发短信
    print send_sms(apikey,text,mobile)
    #调用模板接口发短信
    tpl_id = 1 #对应的模板内容为:您的验证码是#code#【#company#】
    tpl_value = {'#code#':'1234','#company#':'云片网'}
    print tpl_send_sms(apikey, tpl_id, tpl_value, mobile)
    #调用模板接口发语音短信
    print send_voice_sms(apikey,voiceCode,mobile)

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

//项目需要添加System.web引用
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace yunpianSmsClient
{
    class Program
    {
        static void Main(string[] args)
        {
            // 设置为您的apikey(https://www.yunpian.com)可查
            string apikey = "xxxxxxxxxxxxxxxxxxxxxxx";
            // 发送的手机号
            string mobile = "xxxxxxxxxxxxxxxxxxxxxxx";
            // 发送模板编号
            int tpl_id = 1;
            // 发送模板内容
            mobile = HttpUtility.UrlEncode(mobile, Encoding.UTF8);
            string tpl_value = HttpUtility.UrlEncode(
            HttpUtility.UrlEncode("#code#", Encoding.UTF8)    + "=" +
            HttpUtility.UrlEncode("1234", Encoding.UTF8)      + "&" +
            HttpUtility.UrlEncode("#company#", Encoding.UTF8) + "=" +
            HttpUtility.UrlEncode("云片网", Encoding.UTF8), Encoding.UTF8);
            // 发送内容
            string text = "【云片网】您的验证码是1234";
            // 获取user信息url
            string url_get_user     = "https://sms.yunpian.com/v2/user/get.json";
            // 智能模板发送短信url
            string url_send_sms     = "https://sms.yunpian.com/v2/sms/single_send.json";
            // 指定模板发送短信url
            string url_tpl_sms      = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
            // 发送语音短信url
            string url_send_voice   = "https://voice.yunpian.com/v2/voice/send.json";

            string data_get_user    = "apikey=" + apikey;
            string data_send_sms    = "apikey=" + apikey + "&mobile=" + mobile + "&text=" + text;
            string data_tpl_sms     = "apikey=" + apikey + "&mobile=" + mobile + "&tpl_id=" + tpl_id.ToString() + "&tpl_value=" + tpl_value;
            string data_send_voice  = "apikey=" + apikey + "&mobile=" + mobile + "&code=" + "1234";

           
            HttpPost(url_get_user, data_get_user);
            HttpPost(url_send_sms, data_send_sms);
            HttpPost(url_tpl_sms, data_tpl_sms);
            HttpPost(url_send_voice, data_send_voice);
        }
        public static void HttpPost(string Url, string postDataStr)
        {
            byte[] dataArray = Encoding.UTF8.GetBytes(postDataStr);
           // Console.Write(Encoding.UTF8.GetString(dataArray));
            
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = dataArray.Length;
            //request.CookieContainer = cookie;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(dataArray, 0, dataArray.Length);
            dataStream.Close();
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                String res = reader.ReadToEnd();
                reader.Close();
                Console.Write("\nResponse Content:\n" + res + "\n");
            }
            catch(Exception e)
            {
                Console.Write(e.Message + e.ToString());
            }
        }
    }
}

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

#include <stdio.h>
#include <curl/curl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#define MAXPARAM 2048

CURL *curl;
CURLcode res;
/**

bingone

本样例依赖libcurl库
下载地址 https://curl.haxx.se/download.html

*/
// 修改为您的apikey(https://www.yunpian.com)登录官网后获取
char *apikey = "xxxxxxxxxxxxxxxx";
// 修改为您要发送的手机号
char *mobile = "xxxxxxxxxxxxxxxx";
// 设置您要发送的内容
char *text = "【云片网】您的验证码是1234";
// 指定发送的模板id
int tpl_id = 1;
// 指定发送模板内容


char *tpl_data[4] = {"#code#","1234","#company#","云片网"};

// 发送语音验证码内容
int code = 1234;
// 获取user信息url
char *url_get_user     = "https://sms.yunpian.com/v2/user/get.json";
// 智能模板发送短信url
char *url_send_sms     = "https://sms.yunpian.com/v2/sms/single_send.json";
// 指定模板发送短信url
char *url_tpl_sms      = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
// 发送语音短信url
char *url_send_voice   = "https://voice.yunpian.com/v2/voice/send.json";

void send_data(char *url,char *data)
{
    // specify the url
    curl_easy_setopt(curl, CURLOPT_URL, url);
    // specify the POST data
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
    // get response data
    curl_easy_perform(curl);
    printf("\n\n");
}
/**
* 查账户信息
*/
void get_user(char *apikey)
{
    char params[MAXPARAM + 1];
    char *cp = params;
    sprintf(params,"apikey=%s", apikey);
    send_data(url_get_user, params);
}

/**
* 使用智能匹配模板接口发短信
*/
void send_sms(char *apikey, char *mobile, char *text)
{
    char params[MAXPARAM + 1];
    char *cp = params;
    sprintf(params,"apikey=%s&mobile=%s&text=%s", apikey, mobile, text);
    send_data(url_send_sms, params);
}
/**
* 使用智能匹配模板接口发短信
*/
void send_tpl_sms(char *apikey, char *mobile, int tpl_id, char *tpl_value)
{
    char params[MAXPARAM + 1];
    char *cp = params;
    sprintf(params,"apikey=%s&mobile=%s&tpl_id=%d&tpl_value=%s", apikey, mobile, tpl_id, tpl_value);
    send_data(url_tpl_sms, params);
}
/**
* 使用智能匹配模板接口发短信
*/
void send_voice(char *apikey, char *mobile, int code)
{
    char params[MAXPARAM + 1];
    char *cp = params;
    sprintf(params,"apikey=%s&mobile=%s&code=%d", apikey, mobile, code);
    send_data(url_send_voice, params);
}
int main(void)
{

    curl = curl_easy_init();
    mobile = curl_easy_escape(curl,mobile,strlen(mobile));
    if(NULL == curl) {
        perror("curl open fail\n");
    }
    // 获取用户信息
    get_user(apikey);
    // 发送短信
    send_sms(apikey,mobile,text);
    // 发送语音验证码
    send_voice(apikey,mobile,code);

    char *tmp;
    char *tpl_value = (char *)malloc(sizeof(char) * 500);
    bzero(tpl_value, sizeof(char)*500);

    // 模板短信发送需要编码两次,第一次URL编码转换
    int len = 0;
    tmp = curl_easy_escape(curl,tpl_data[0],strlen(tpl_data[0]));
    memcpy(tpl_value+len,tmp,strlen(tmp));
    len += strlen(tmp);
    tpl_value[len++] = '=';

    tmp = curl_easy_escape(curl,tpl_data[1],strlen(tpl_data[1]));
    memcpy(tpl_value+len,tmp,strlen(tmp));
    len += strlen(tmp);
    tpl_value[len++] = '&';

    tmp = curl_easy_escape(curl,tpl_data[2],strlen(tpl_data[2]));
    memcpy(tpl_value+len,tmp,strlen(tmp));
    len += strlen(tmp);
    tpl_value[len++] = '=';

    tmp = curl_easy_escape(curl,tpl_data[3],strlen(tpl_data[3]));
    memcpy(tpl_value+len,tmp,strlen(tmp));
    len += strlen(tmp);

    tmp=tpl_value;
    // 第二次URL编码
    tpl_value = curl_easy_escape(curl,tpl_value,strlen(tpl_value));
    send_tpl_sms(apikey,mobile,tpl_id,tpl_value);
    free(tmp);

    curl_easy_cleanup(curl);
    
    return 0;
}

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

#/bin/sh
#author jacky
#修改为您的apikey
apikey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#修改为您要发送的手机号
mobile="188xxxxxxxx"
#设置您要发送的内容
text="您的验证码是1234。如非本人操作,请忽略。【云片网】"
echo "get user info:"
curl --data "apikey=$apikey" "https://sms.yunpian.com/v2/user/get.json"
echo "\nsend sms:"
curl --data "apikey=$apikey&mobile=$mobile&text=$text" "https://sms.yunpian.com/v2/sms/single_send.json"

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

<?php
header("Content-Type:text/html;charset=utf-8");
$apikey = "xxxxxxxxxxx"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取
$mobile = "xxxxxxxxxxx"; //请用自己的手机号代替
$text="【云片网】您的验证码是1234";
$ch = curl_init();

/* 设置验证方式 */

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));

/* 设置返回结果为流 */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

/* 设置超时时间*/
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

/* 设置通信方式 */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 取得用户信息
$json_data = get_user($ch,$apikey);
$array = json_decode($json_data,true);
echo '<pre>';print_r($array);
// 发送短信
$data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
$json_data = send($ch,$data);
$array = json_decode($json_data,true);
echo '<pre>';print_r($array);
// 发送模板短信
// 需要对value进行编码
$data=array('tpl_id'=>'1','tpl_value'=>('#code#').'='.urlencode('1234').'&'.urlencode('#company#').'='.urlencode('欢乐行'),'apikey'=>$apikey,'mobile'=>$mobile);
print_r ($data);
$json_data = tpl_send($ch,$data);
$array = json_decode($json_data,true);
echo '<pre>';print_r($array);

// 发送语音验证码
$data=array('code'=>'9876','apikey'=>$apikey,'mobile'=>$mobile);
$json_data =voice_send($ch,$data);
$array = json_decode($json_data,true);
echo '<pre>';print_r($array);

curl_close($ch);

/***************************************************************************************/
//获得账户
function get_user($ch,$apikey){
    curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
    return curl_exec($ch);
}
function send($ch,$data){
    curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    return curl_exec($ch);
}
function tpl_send($ch,$data){
    curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    return curl_exec($ch);
}
function voice_send($ch,$data){
    curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    return curl_exec($ch);
}

?>

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

<%
'安装IIS并重启后可用
@LANGUAGE="VBSCRIPT" CODEPAGE="65001"
%>
<%
response.contenttype = "text/html;charset=utf-8"
'提交方法
method = "POST"
'您要发送的手机号
mobile = Server.URLEncode("xxxxxxxxxxx")
'修改为您的apikey(https://www.yunpian.com)登录官网后获取
apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
'发送内容
text ="【云片网】您的验证码是1234"
'使用模板号
tpl_id = 1
'使用模板内容
tpl_value = Server.URLEncode("#code#=1234&#company#=云片网")
'查询账户信息https地址
url_get_user   = "https://sms.yunpian.com/v2/user/get.json"
'智能匹配模板发送https地址
url_send_sms   = "https://sms.yunpian.com/v2/sms/single_send.json"
'指定模板发送接口https地址
url_tpl_sms   = "https://sms.yunpian.com/v2/sms/tpl_single_send.json"
'发送语音验证码接口https地址
url_send_voice = "https://voice.yunpian.com/v2/voice/send.json"
data_get_user  =  "apikey=" & apikey
data_send_sms  =  "apikey=" & apikey & "&mobile=" & mobile & "&text=" & text
data_tpl_sms    = "apikey=" & apikey & "&mobile=" & mobile & "&tpl_id=" & tpl_id & "&tpl_value=" & tpl_value
data_send_voice = "apikey=" & apikey & "&mobile=" & mobile & "&code=" & "1234"
response.write GetBody(url_get_user,data_get_user)
response.write GetBody(url_send_sms,data_send_sms)
response.write GetBody(url_tpl_sms,data_tpl_sms)
response.write GetBody(url_send_voice,data_send_voice)
Function GetBody(url,data) 
    Set https = Server.CreateObject("MSXML2.ServerXMLHTTP") 
    With https
    .Open method, url, False
    .setRequestHeader "Content-Type","application/x-www-form-urlencoded"
    .Send data
    
    GetBody= .ResponseBody
    End With
    GetBody = bytetostr(https.ResponseBody,"utf-8")
    Set https = Nothing
    
End Function
function bytetostr(vin,cset)
dim bs,sr
set bs = server.createObject("adodb.stream")
    bs.type = 2
    bs.open
    bs.writetext vin
    bs.position = 0
    bs.charset = cset
    bs.position = 2
    sr = bs.readtext
    bs.close
set bs = nothing
bytetostr = sr
end function
%>

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

// 修改为您的apikey.可在官网(https://www.yunpian.com)登录后获取
var https = require('https');
var qs = require('querystring');

var apikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// 修改为您要发送的手机号码,多个号码用逗号隔开
var mobile = 'xxxxxxxxxxx';
// 修改为您要发送的短信内容
var text = '【云片网】您的验证码是1234';
// 指定发送的模板编号
var tpl_id = 1;
// 指定发送模板的内容
var tpl_value =  {'#code#':'1234','#company#':'yunpian'};
// 语音短信的内容
var code = '1234';
// 查询账户信息https地址
var get_user_info_uri = '/v2/user/get.json';
// 智能匹配模板发送https地址
var sms_host = 'sms.yunpian.com';
var voice_host = 'voice.yunpian.com';

send_sms_uri = '/v2/sms/single_send.json';
// 指定模板发送接口https地址
send_tpl_sms_uri = '/v2/sms/tpl_single_send.json';
// 发送语音验证码接口https地址
send_voice_uri = '/v2/voice/send.json';





query_user_info(get_user_info_uri,apikey);

send_sms(send_sms_uri,apikey,mobile,text);

send_tpl_sms(send_tpl_sms_uri,apikey,mobile,tpl_id,tpl_value);

send_voice_sms(send_voice_uri,apikey,mobile,code);
function query_user_info(uri,apikey){
    var post_data = {  
    'apikey': apikey,  
    };//这是需要提交的数据
    var content = qs.stringify(post_data);  
    post(uri,content,sms_host);
}

function send_sms(uri,apikey,mobile,text){
    var post_data = {  
    'apikey': apikey,  
    'mobile':mobile,
    'text':text,
    };//这是需要提交的数据  
    var content = qs.stringify(post_data);  
    post(uri,content,sms_host);
}

function send_tpl_sms(uri,apikey,mobile,tpl_id,tpl_value){
    var post_data = {  
    'apikey': apikey,
    'mobile':mobile,
    'tpl_id':tpl_id,
    'tpl_value':qs.stringify(tpl_value),  
    };//这是需要提交的数据  
    var content = qs.stringify(post_data);  
    post(uri,content,sms_host); 
}
function send_voice_sms(uri,apikey,mobile,code){
    var post_data = {  
    'apikey': apikey,
    'mobile':mobile,
    'code':code,
    };//这是需要提交的数据  
    var content = qs.stringify(post_data);  
    console.log(content);
    post(uri,content,voice_host); 
}
function post(uri,content,host){
    var options = {  
        hostname: host,
        port: 443,  
        path: uri,  
        method: 'POST',  
        headers: {  
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'  
        }  
    };
    var req = https.request(options, function (res) {  
        // console.log('STATUS: ' + res.statusCode);  
        // console.log('HEADERS: ' + JSON.stringify(res.headers));  
        res.setEncoding('utf8');  
        res.on('data', function (chunk) {  
            console.log('BODY: ' + chunk);  
        });  
    }); 
    //console.log(content);
    req.write(content);  
  
    req.end();   
}

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

package main
import (
    "net/http"
    "io/ioutil"
    "net/url"
    "fmt"
 )
// bingone
func main(){

    // 修改为您的apikey(https://www.yunpian.com)登录官网后获取
    apikey      := "xxxxxxxxxxxxxxxxxx"
     // 修改为您要发送的手机号码,多个号码用逗号隔开
    mobile      := "xxxxxxxxxxxxxxxxxx"
    // 发送内容
    text        := "【云片网】您的验证码是1234"
    // 发送模板编号
    tpl_id      := 1
    // 语音验证码
    code        := "1234"
    company     := "云片网"
    // 发送模板内容
    tpl_value   := url.Values{"#code#":{code},"#company#":{company}}.Encode()
    
    // 获取user信息url
    url_get_user    := "https://sms.yunpian.com/v2/user/get.json";
    // 智能模板发送短信url
    url_send_sms    := "https://sms.yunpian.com/v2/sms/single_send.json";
    // 指定模板发送短信url
    url_tpl_sms     := "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
    // 发送语音短信url
    url_send_voice  := "https://voice.yunpian.com/v2/voice/send.json";

    data_get_user   := url.Values{"apikey": {apikey}}
    data_send_sms   := url.Values{"apikey": {apikey}, "mobile": {mobile},"text":{text}}
    data_tpl_sms    := url.Values{"apikey": {apikey}, "mobile": {mobile},"tpl_id":{fmt.Sprintf("%d", tpl_id)},"tpl_value":{tpl_value}}
    data_send_voice := url.Values{"apikey": {apikey}, "mobile": {mobile},"code":{code}}


    httpsPostForm(url_get_user,data_get_user)
    httpsPostForm(url_send_sms,data_send_sms)
    httpsPostForm(url_tpl_sms,data_tpl_sms)
    httpsPostForm(url_send_voice,data_send_voice)
}

func httpsPostForm(url string,data url.Values) {
    resp, err := http.PostForm(url,data)

    if err != nil {
        // handle error
    }

    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // handle error
    }

    fmt.Println(string(body))

}

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

=begin

Desc:短信http接口的ruby代码调用示例
author shaoyan
date 2015-10.28

=end

require 'net/http'
require 'uri'
params = {}


#修改为您的apikey.可在官网(http://www.yunpian.com)登录后用户中心首页看到
apikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#修改为您要发送的手机号码,多个号码用逗号隔开
mobile = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#修改为您要发送的短信内容
text = '【云片网】您的验证码是1234'
#查询账户信息HTTP地址
get_user_info_uri = URI.parse('https://sms.yunpian.com/v2/user/get.json')
#智能匹配模板发送HTTP地址
send_sms_uri = URI.parse('https://sms.yunpian.com/v2/sms/single_send.json')
#指定模板发送接口HTTP地址
send_tpl_sms_uri = URI.parse('https://sms.yunpian.com/v2/sms/tpl_single_send.json')
#发送语音验证码接口HTTP地址
send_voice_uri = URI.parse('https://voice.yunpian.com/v2/voice/send.json')

params['apikey'] = apikey
#打印用户信息
response =  Net::HTTP.post_form(get_user_info_uri,params)
print response.body + "\n"

params['mobile'] = mobile
params['text'] = text
#智能匹配模板发送
response = Net::HTTP.post_form(send_sms_uri,params)
print response.body + "\n"
#指定模板发送
#设置模板ID,如使用1号模板:【#company#】您的验证码是#code#
#设置对应的模板变量值

params['tpl_id'] = 1
params['tpl_value'] = URI::escape('#code#')+'='+URI::escape('1234')+'&'+URI::escape('#company#')+'='+URI::escape('yunpian')
response = Net::HTTP.post_form(send_tpl_sms_uri,params)
print response.body + "\n"
#发送语音验证码
params['code'] = 1234
response = Net::HTTP.post_form(send_voice_uri,params)
print response.body + "\n"