您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php实现发送微信模板消息的方法

2024/3/21 22:12:49发布29次查看
本文实例讲述了php实现发送微信模板消息的方法。分享给大家供大家参考      
1. [代码][php]代码  <?php namespace org\weixin; class orderpush { protected $appid; protected $secrect; protected $accesstoken; function __construct($appid, $secrect) { $this->appid = $appid; $this->secrect = $secrect; $this->accesstoken = $this->gettoken($appid, $secrect); } /** * 发送post请求 * @param string $url * @param string $param * @return bool|mixed */ function request_post($url = '', $param = '') { if (empty($url) || empty($param)) { return false; } $posturl = $url; $curlpost = $param; $ch = curl_init(); //初始化curl curl_setopt($ch, curlopt_url, $posturl); //抓取指定网页 curl_setopt($ch, curlopt_header, 0); //设置header curl_setopt($ch, curlopt_returntransfer, 1); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, curlopt_post, 1); //post提交方式 curl_setopt($ch, curlopt_postfields, $curlpost); $data = curl_exec($ch); //运行curl curl_close($ch); return $data; } /** * 发送get请求 * @param string $url * @return bool|mixed */ function request_get($url = '') { if (empty($url)) { return false; } $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, 1); $data = curl_exec($ch); curl_close($ch); return $data; } /** * @param $appid * @param $appsecret * @return mixed * 获取token */ protected function gettoken($appid, $appsecret) { if (s($appid)) { $access_token = s($appid); } else { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret; $token = $this->request_get($url); $token = json_decode(stripslashes($token)); $arr = json_decode(json_encode($token), true); $access_token = $arr['access_token']; s($appid, $access_token, 720); } return $access_token; } /** * 发送自定义的模板消息 * @param $touser * @param $template_id * @param $url * @param $data * @param string $topcolor * @return bool */ public function dosend($touser, $template_id, $url, $data, $topcolor = '#7b68ee') { /* * data=>array( 'first'=>array('value'=>urlencode("您好,您已购买成功"),'color'=>"#743a3a"), 'name'=>array('value'=>urlencode("商品信息:微时代电影票"),'color'=>'#eeeeee'), 'remark'=>array('value'=>urlencode('永久有效!密码为:1231313'),'color'=>'#ffffff'), ) */ $template = array( 'touser' => $touser, 'template_id' => $template_id, 'url' => $url, 'topcolor' => $topcolor, 'data' => $data ); $json_template = json_encode($template); $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accesstoken; $datares = $this->request_post($url, urldecode($json_template)); if ($datares['errcode'] == 0) { return true; } else { return false; } } }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product