PHP发送微信模版消息

  1     public function payResult($params) {
  2 
  3         global $_GPC, $_W;
  4 
  5         $weid = $this->_weid;
  6 
  7         $order = pdo_fetch("SELECT id, status, hotelid, roomid FROM " . tablename(‘hotel2_order‘) . " WHERE id = {$params[‘tid‘]} AND weid = {$weid} LIMIT 1");
  8 
  9         pdo_update(‘hotel2_order‘, array(‘paystatus‘ => 1), array(‘id‘ => $params[‘tid‘]));
 10         if ($params[‘from‘] == ‘return‘) {
 11             $roomid = $order[‘roomid‘];
 12             $room = pdo_fetch("SELECT score FROM " . tablename(‘hotel2_room‘) . " WHERE id = {$roomid} AND weid = {$weid} LIMIT 1");
 13             $score = intval($room[‘score‘]);
 14 
 15             if ($score) {
 16                 $from_user = $this->_from_user;
 17                 pdo_fetch("UPDATE " . tablename(‘hotel2_member‘) . " SET score = (score + " . $score . ") WHERE from_user = ‘".$from_user."‘ AND weid = ".$weid."");
 18                 pdo_fetch("UPDATE " . tablename(‘fans‘) . " SET credit1 = (credit1 + " . $score . ") WHERE from_user = ‘".$from_user."‘ AND weid = ".$weid."");
 19             }
 20 
 21             /*
 22             发送模板消息
 23             */
 24 
 25             $template_id="mYQugPDOABxe1Y9HVVZy8avP9inHsQAJuL_TJsU49y4";
 26             $url=‘http://‘ . $_SERVER[‘SERVER_NAME‘] . ‘/‘.$this->createMobileUrl(‘orderdetail‘,array("id"=>$order[‘id‘]));
 27             $color=‘#FF0000‘;
 28 
 29             $sql = "SELECT o.*, h.title, h.address, h.phone";
 30             $sql .= " FROM " .tablename(‘hotel2_order‘) ." AS o";
 31             $sql .= " LEFT JOIN " .tablename(‘hotel2‘) ." AS h ON o.hotelid = h.id";
 32             $sql .= " WHERE 1 = 1";
 33             $sql .= " AND o.id = :id";
 34             $sql .= " AND o.weid = :weid";
 35             $params = array();
 36             $params[‘:weid‘] = $weid;
 37             $params[‘:id‘] = $order[‘id‘];
 38             $sql .= " LIMIT 1";
 39             $item = pdo_fetch($sql, $params);
 40 
 41             $paymethod="";
 42             if($item[‘paytype‘]==1){
 43                 $paymethod="余额支付";
 44             }elseif($item[‘paytype‘]==21){
 45                 $paymethod="微信支付";
 46             }elseif($item[‘paytype‘]==22){
 47                 $paymethod="支付宝";
 48             }else{
 49                 $paymethod="到店付款";
 50             }
 51 
 52 
 53 
 54             $remark="房间数量:".$item["nums"]."间\n房型名称:".$item["style"]."\n订单总价:¥".$item["sum_price"]."\n酒店电话:".$item["phone"]."\n酒店地址:".$item["address"]."\n付款方式:".$paymethod."\n房间将保留至次日中午12点,不能取消修改。";
 55 
 56             $data=array(
 57                 "first"=>array(
 58                     "value"=>"您好,您已成功预订".$item["title"]."!",
 59                     "color"=>"#173177"
 60                 ),
 61                 "OrderID"=>array(
 62                     "value"=>$item["ordersn"],
 63                     "color"=>"#173177"
 64                 ),
 65                 "PersonName"=>array(
 66                     "value"=>$item["name"],
 67                     "color"=>"#173177"
 68                 ),
 69                 "CheckInDate"=>array(
 70                     "value"=>date(‘Y-m-d‘,$item[‘btime‘]),
 71                     "color"=>"#173177"
 72                 ),
 73                 "CheckOutDate"=>array(
 74                     "value"=>date(‘Y-m-d‘,$item[‘etime‘]),
 75                     "color"=>"#173177"
 76                 ),
 77                 "remark"=>array(
 78                     "value"=>$remark,
 79                     "color"=>"#173177"
 80                 )
 81             );
 82             $data=json_encode($data);
 83 
 84             $this->sendtempmsg($template_id,$url,$data,$color);
 85 
 86             //end 发送模板消息
 87 
 88 
 89             message(‘支付成功!‘, "/".$this->createMobileUrl(‘orderdetail‘,array("id"=>$order[‘id‘])), ‘success‘);
 90         }
 91     }
 92 
 93 
 94     public function sendtempmsg($template_id, $url, $data, $topcolor) {
 95         global $_W, $_GPC;
 96         $from_user = $this->_from_user;
 97         $tokens = $this->get_weixin_token();
 98         //echo "token:".$tokens."..";
 99         if (empty($tokens)) {
100             return;
101         }
102         $postarr = ‘{"touser":"‘ . $from_user . ‘","template_id":"‘ . $template_id . ‘","url":"‘ . $url . ‘","topcolor":"‘ . $topcolor . ‘","data":‘ . $data . ‘}‘;
103 
104         $res = ihttp_post(‘https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=‘ . $tokens, $postarr);
105         return true;
106     }
107 
108 
109      public function get_weixin_token() {
110         global $_W, $_GPC;
111         $account = $_W[‘account‘];
112         if (is_array($account[‘access_token‘]) && !empty($account[‘access_token‘][‘token‘]) && !empty($account[‘access_token‘][‘expire‘]) && $account[‘access_token‘][‘expire‘] > TIMESTAMP) {
113             return $account[‘access_token‘][‘token‘];
114         } else {
115             if (empty($account[‘weid‘])) {
116                 message(‘参数错误.‘);
117             }
118             $appid = $account[‘key‘];
119             $secret = $account[‘secret‘];
120 
121             $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
122             $content = ihttp_get($url);
123             if (empty($content)) {
124                 message(‘获取微信公众号授权失败, 请稍后重试!‘);
125             }
126             $token = @json_decode($content[‘content‘], true);
127             if (empty($token) || !is_array($token)) {
128                 message(‘获取微信公众号授权失败, 请稍后重试! 公众平台返回原始数据为: <br />‘ . $token);
129             }
130             if (empty($token[‘access_token‘]) || empty($token[‘expires_in‘])) {
131                 message(‘解析微信公众号授权失败, 请稍后重试!‘);
132             }
133             $record = array();
134             $record[‘token‘] = $token[‘access_token‘];
135             $record[‘expire‘] = TIMESTAMP + $token[‘expires_in‘];
136             $row = array();
137             $row[‘access_token‘] = iserializer($record);
138             pdo_update(‘wechats‘, $row, array(
139                 ‘weid‘ => $account[‘weid‘]
140             ));
141             return $record[‘token‘];
142         }
143     }

 

PHP发送微信模版消息

上一篇:【转】swift实现ios类似微信输入框跟随键盘弹出的效果


下一篇:浏览器原生和微信JS-SDK两种方法获取用户经纬度和所在城市的方法