开源一个微信公众号向关注用户下发消息的简单类库

$msg = new wxmsg();#实例化消息模板
$msg->setAccessToken($msg->getAccessToken()); #获取令牌并写入到类成员
$templatelist = $msg->getTemplateList();
if(count($templatelist)<=0){ #如果模板数量小于0,直接中止
return false;
}
for($i=0;$i<count($templatelist);$i++){
if($templatelist[$i][‘title’]==’订单支付成功’){ #如果消息模板中包含订单支付成功,下发通知信息
$openid=’ocbV9wGjFMbfGTUvh89Ro9J-ng7s’;
$productName=’测试的产品’;
$productDescription=’发送时间:’.date(‘Y-m-d H:i:s’);
$return_url = ‘http://www.qingxinzui.com/’;
$money = ‘123.45’;
$data = $msg->SendMsg($openid, $templatelist[$i][‘template_id’], $productName, $productDescription,$return_url,$money);
break;
}
}

 

然后是类的扩展::

<?php

/*******
*
* 本类库只提供小心模板下发,不进行模板配置
* 需要设置的模板库为:TM00015
*
* @author IPanYing
*
*/
class wxmsg{

private $appid;
private $appsecret;
private $access_token;

public function __construct(){
$this->appid = ”;
$this->appsecret = ”;
}

public function setAccessToken($access_token){
$this->access_token = $access_token;
}
public function getAccessToken(){
$url = “https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appsecret”;
$AccessToken = $this->IPanYingGETCURL($url);
if (isset($AccessToken[‘access_token’]) && !empty($AccessToken[‘access_token’])){
return $AccessToken[‘access_token’];
} else {
return false;
}
}

public function getMsgModelName(){
$url = “https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=$this->access_token”;
return $this->IPanYingGETCURL($url);
}

public function getTemplateList(){
$url=”https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=$this->access_token”;
$list = $this->IPanYingGETCURL($url);
return $list[‘template_list’];
}

public function SendMsg($openid,$template_id,$producttext,$remarktext,$return_url,$money){
$return_url=’http://www.ipanying.com/’;
$data = array(
‘touser’=>”$openid”,
‘template_id’=>”$template_id”,
‘url’ =>”$return_url”,
‘topcolor’=>’#FF0000’,
‘data’=>array(
‘first’=>array(‘value’=>’尊敬的用户,你的支付订单信息如下:’,’color’=>’#173177′),
‘orderMoneySum’=>array(‘value’=>$money,’color’=>’#173177’),
‘orderProductName’=>array(‘value’=>$producttext,’color’=>’#173177’),
‘Remark’=>array(‘value’=>$remarktext,’color’=>’#173177’)
)
);
$url = “https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$this->access_token”;
return $this->IPanYingPOSTCURL($url, $data);
}

private function IPanYingGETCURL($url){
$data = file_get_contents($url);
$data = (array)json_decode($data,true);
return $data;
}

private function IPanYingPOSTCURL($url, $data){
//$data = urldecode(json_encode($data));
$data = json_encode($data);
$post = array(
‘http’ =>array(
‘method’ => ‘POST’,
‘header’ => ‘Content-type: application/x-www-form-urlencoded’,
‘content’ => $data
)
);
$context = stream_context_create($post);
$data = file_get_contents($url, false, $context);
$data = (array)json_decode($data,true);
return $data;
}

}

?>

wxmsg-class

发表评论

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!: