标签:
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 应用地点,类似于: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协议文档
}
}