設置推送類:
<?php /** * tpshop 微信支付插件 * ============================================================================ * 版權所有 2015-2027 深圳搜豹網絡科技有限公司,並保留所有權利。 * 網站地址: http://www.tp-shop.cn * ---------------------------------------------------------------------------- * 這不是一個自由軟件!您只能在不用於商業目的的前提下對程序代碼進行修改和使用 . * 不允許對程序代碼以任何形式任何目的的再發布。 * ============================================================================ * Author: IT宇宙人 * Date: 2015-09-09 */ use Think\Model\RelationModel; /** * 支付 邏輯定義 * Class * @package Home\Payment */ class wxtemple extends RelationModel { public $tableName = 'plugin'; // 插件表 public $alipay_config = array();// 支付寶支付配置參數 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'), ) */ print_r($data); die; $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)); print_r($dataRes); if ($dataRes['errcode'] == 0) { return true; } else { return false; } } }
調用推送類:
public function wxtemp(){ include_once "plugins/payment/weixin/wxtemple.class.php"; // $code = '\\'.wxtemple; $apiKey = 'w1a34'; $apiSecret = '12333'; $wmes = new $code($apiKey,$apiSecret); $touser="12333"; $template="wMakljn8USR1RDS0vrkCW6IfJQy8idB_m4_3RNsCIpE"; $templateData = array( 'first' => array( 'value' => "訂單已審核通過", 'color' => '#0e356e' ), 'orderno' => array( 'value' => "123213213", 'color' => '#2832e1' ), 'refundno' => array( 'value' => "我的小瓶蓋", 'color' => '#0e356e' ), 'refundproduct' => array( 'value' => "我的小瓶蓋", 'color' => '#0e356e' ), 'remark' => array( 'value' => "fdsfdsff", 'color' => '#0e356e' ) ); $rs = $wmes->doSend($touser,$template,"",$templateData); }
templatedata: