您现在的位置是:首页 > 其他

李清波 2019-09-09 其他 3074 复制当前网址

云信PHP短信发送接口方法详解说明


功能:使用smsapi.fun.php功能函数发送短信示例

说明:http://api.sms.cn/sms/?ac=send&uid=用户账号&pwd=MD5位32密码&mobile=号码&content=内容

官网:www.sms.cn


变量模板发送示例

demo.php

<?php

include 'smsapi.fun.php';

//用户账号
$uid = 'testsms';
//MD5密码
$pwd = '353447s535dd';


/*
* 变量模板发送示例
* 模板内容:您的验证码是:{$code},对用户{$username}操作绑定手机号,有效期为5分钟。如非本人操作,可不用理会。【云信】
* 变量模板ID:100003
*/

//号码
$mobile	 = '13900001111,13900001112';
//变量模板ID
$template = '100005';
//6位随机验证码
$code = randNumber();

//短信内容参数
$contentParam = array(
	'code'		=> $code,
	'username'	=> '您好'
	);
//即时发送
$res = sendSMS($uid,$pwd,$mobile,array_to_json($contentParam),$template);
if( $res['stat']=='100' ) {
	echo "发送成功!";
} else {
	echo "发送失败! 状态:".$res['stat'].'|'.$res['message'];
}



全文模板发送示例

demo2.php

<?php

include 'smsapi.fun.php';

//用户账号
$uid = 'testsms';
//MD5密码
$pwd = '353447s535dd';


/*
* 全文模板发送示例
* 模板内容:登录验证码:{**}。如非本人操作,可不用理会!【云信】
*
*/

//号码 多个号码用,英文逗号隔开
$mobile	 = '13900001111';
//短信内容
$code = randNumber();	//验证码
$content = '登录验证码:'.$code.'。如非本人操作,可不用理会!【云信】';
//即时发送
$res = sendSMS($uid,$pwd,$mobile,$content);
if( $res['stat']=='100' ) {
	echo "发送成功!";
} else {
	echo "发送失败! 状态:".$res['stat'].'|'.$res['message'];
}



smsapi.fun.php

<?php
/**
SMS短信发送函数
@author		sms.cn
@link		http://www.sms.cn
*/

/**
* 短信发送
*
* @param string $uid 短信账号
* @param string $pwd MD5接口密码
* @param string $mobile 手机号码
* @param string $content 短信内容
* @param string $template 短信模板ID
* @return array
*/
function sendSMS($uid,$pwd,$mobile,$content,$template='')
{
	$apiUrl = 'http://api.sms.cn/sms/';		//短信接口地址
	$data = array(
		'ac' =>		'send',
		'uid'=>		$uid,					//用户账号
		'pwd'=>		md5($pwd.$uid),					//MD5位32密码,密码和用户名拼接字符
		'mobile'=>	$mobile,				//号码
		'content'=>	$content,				//内容
		'template'=>$template,				//变量模板ID 全文模板不用填写
		'format' => 'json',					//接口返回信息格式 json\xml\txt
		);
	
	$result = postSMS($apiUrl,$data);			//POST方式提交
	$re = json_to_array($result);			    //JSON数据转为数组
	//$re = getSMS($apiUrl,$data);				//GET方式提交
	
	return $re;
	/*
	if( $re['stat']=='100' )
	{
		return "发送成功!";
	}
	else if( $re['stat']=='101')
	{
		return "验证失败! 状态:".$re;
	}
	else 
	{
		return "发送失败! 状态:".$re;
	}
	*/
}

/*
//密码直接写的函数里

function sendSMS($mobile,$content,$template='')
{
	$uid = 'test';
	$pwd = 'testpass';
	$apiUrl = 'http://api.sms.cn/sms/';		//短信接口地址
	$data = array(
		'ac' =>		'send',
		'uid'=>		$uid,					//用户账号
		'pwd'=>		md5($pwd.$uid),					//MD5位32密码,密码和用户名拼接字符
		'mobile'=>	$mobile,				//号码
		'content'=>	$content,				//内容
		'template'=>$template,				//变量模板ID 全文模板不用填写
		'format' => 'json',					//接口返回信息格式 json\xml\txt
		);
	
	$result = postSMS($apiUrl,$data);			//POST方式提交
	$re = json_to_array($result);			    //JSON数据转为数组
	
	if( $re['stat']=='100' )
	{
		return "发送成功";
	}
	else if( $re['stat']=='101')
	{
		return "验证失败! 状态:".$re;
	}
	else 
	{
		return "发送失败! 状态:".$re;
	}
}
*/

/**
* POST方式HTTP请求
*
* @param string $url URL地址
* @param array $data POST参数
* @return string
*/
function postSMS($url,$data='')
{
	$row = parse_url($url);
	$host = $row['host'];
	$port='';
	$port = $row['port'] ? $row['port']:80;
	$file = $row['path'];
	$post='';
	while (list($k,$v) = each($data)) 
	{
		$post .= rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
	}
	$post = substr( $post , 0 , -1 );
	$len = strlen($post);
	$fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
	if (!$fp) {
		return "$errstr ($errno)\n";
	} else {
		$receive = '';
		$out = "POST $file HTTP/1.1\r\n";
		$out .= "Host: $host\r\n";
		$out .= "Content-type: application/x-www-form-urlencoded\r\n";
		$out .= "Connection: Close\r\n";
		$out .= "Content-Length: $len\r\n\r\n";
		$out .= $post;		
		fwrite($fp, $out);
		while (!feof($fp)) {
			$receive .= fgets($fp, 128);
		}
		fclose($fp);
		$receive = explode("\r\n\r\n",$receive);
		unset($receive[0]);
		return implode("",$receive);
	}
}
/**
* GET方式HTTP请求
*
* @param string $url URL地址
* @param array $data POST参数
* @return string
*/
function getSMS($url,$data='')
{
	$get='';
	while (list($k,$v) = each($data)) 
	{
		$get .= $k."=".urlencode($v)."&";	//转URL标准码
	}
	return file_get_contents($url.'?'.$get);
}
//数字随机码
function randNumber($len = 6)
{
	$chars = str_repeat('0123456789', 10);
	$chars = str_shuffle($chars);
	$str   = substr($chars, 0, $len);
	return $str;
}
//把数组转json字符串
function array_to_json($p)
{
	return urldecode(json_encode(json_urlencode($p)));
}
//url转码
function json_urlencode($p)
{
	if( is_array($p) )
	{
		foreach( $p as $key => $value )$p[$key] = json_urlencode($value);
	}
	else
	{
		$p = urlencode($p);
	}
	return $p;
}

//把json字符串转数组
function json_to_array($p)
{
	if( mb_detect_encoding($p,array('ASCII','UTF-8','GB2312','GBK')) != 'UTF-8' )
	{
		$p = iconv('GBK','UTF-8',$p);
	}
	return json_decode($p, true);
}


?>


状态码:

状态:{"stat":"100","message":"发送成功"}
100 发送成功
101 验证失败
102 短信不足
103 操作失败
104 非法字符
105 内容过多
106 号码过多
107 频率过快
108 号码内容空
109 账号冻结
112号码错误
116 禁止接口发送
117 绑定IP不正确
161 未添加短信模板
162 模板格式不正确
163 模板ID不正确
164 全文模板不匹配



文章来源:http://liqingbo.com/blog-1289.html

评论