栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP > PHP实例

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

PHP实例 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


变量模板发送示例

demo.php

 $api->randNumber(),
	'username'	=> '您好'
	);

//变量模板ID
$template = '100005';

//发送变量模板短信
$result = $api->send($mobile,$contentParam,$template);

if($result['stat']=='100') {
	echo '发送成功';
} else {
	echo '发送失败:'.$result['stat'].'('.$result['message'].')';
}


全文模板发送示例

demo2.php

randNumber().'。如非本人操作,可不用理会!【云信】';


//发送全文模板短信
$result = $api->sendAll($mobile,$content);

if($result['stat']=='100')
{
	echo '发送成功';
}
else
{
	echo '发送失败:'.$result['stat'].'('.$result['message'].')';
}


//当前请求返回的原始信息
//echo $api->getResult();

//取剩余短信条数
//print_r($api->getNumber());

//获取发送状态
//print_r($api->getStatus());

//接收上行短信(回复)
//print_r($api->getReply());



smsapi.class.php

uid	= $uid ?: $def_uid;
        $this->pwd	= $pwd ?: $def_pwd;
        $this->apiURL = self::API_URL;
		$this->format = 'json';
    }
	
    protected function publicParams()
    {
        return array(
            'uid'		=> $this->uid,
            'pwd'		=> md5($this->pwd.$this->uid),
            'format'	=> $this->format,
        );
    }
	
	public function send($mobile, $contentParam,$template='') {
		//短信发送参数
		$this->smsParams = array(
			'ac'		=> 'send',
			'mobile'	=> $mobile,
			'content'	=> $this->array_to_json($contentParam),
			'template'	=> $template
		);
		$this->resultMsg = $this->request();
		return $this->json_to_array($this->resultMsg, true);
	}

	
	public function sendAll($mobile, $content) {
		//短信发送参数
		$this->smsParams = array(
			'ac'		=> 'send',
			'mobile'	=> $mobile,
			'content'	=> $content,
		);
		$this->resultMsg = $this->request();

		return $this->json_to_array($this->resultMsg, true);
	}

	
	public function getNumber() {
		//参数
		$this->smsParams = array(
			'ac'		=> 'number',
		);
		$this->resultMsg = $this->request();
		return $this->json_to_array($this->resultMsg, true);
	}


	
	public function getStatus() {
		//参数
		$this->smsParams = array(
			'ac'		=> 'status',
		);
		$this->resultMsg = $this->request();
		return $this->json_to_array($this->resultMsg, true);
	}
	
	public function getReply() {
		//参数
		$this->smsParams = array(
			'ac'		=> 'reply',
		);
		$this->resultMsg = $this->request();
		return $this->json_to_array($this->resultMsg, true);
	}
	
	public function getSendTotal() {
		//参数
		$this->smsParams = array(
			'ac'		=> 'number',
			'cmd'		=> 'send',
		);
		$this->resultMsg = $this->request();
		return $this->json_to_array($this->resultMsg, true);
	}

	
	public function getQuery() {
		//参数
		$this->smsParams = array(
			'ac'		=> 'query',
		);
		$this->resultMsg = $this->request();
		return $this->json_to_array($this->resultMsg, true);
	}

	
	private function request()
	{
		$params = array_merge($this->publicParams(),$this->smsParams);
		if( function_exists('curl_init') )
		{
			return $this->curl_request($this->apiURL,$params);
		}
		else
		{
			return $this->file_get_request($this->apiURL,$params);
		}
	}
	
	private function curl_request($url,$postFields){
		$postFields = http_build_query($postFields);
		//echo $url.'?'.$postFields;
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_POST, 1 );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
		$result = curl_exec ( $ch );
		curl_close ( $ch );
		return $result;
	}
	
	private function file_get_request($url,$postFields)
	{
		$post='';
		while (list($k,$v) = each($postFields)) 
		{
			$post .= rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
		}
		return file_get_contents($url.'?'.$post);
	}
	
	public function getResult()
	{
		$this->resultMsg;
	}
	
	public 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($this->json_urlencode($p)));
	}
	//url转码
	function json_urlencode($p)
	{
		if( is_array($p) )
		{
			foreach( $p as $key => $value )$p[$key] = $this->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);
	}
}

?>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/824924.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号