Use thinkphp to send template messages
Appid = $ appid; $ this-> secrect = $ secrect; $ this-> accessToken = $ this-> getToken ($ appid, $ secrect );} /*** send a post request * @ 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 (); // initialize curl curl_setopt ($ ch, CURLOPT_URL, $ postUrl); // capture the specified webpage curl_setopt ($ ch, CURLOPT_HEADER, 0 ); // Set the header curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result must be a string and be output to the screen curl_setopt ($ ch, CURLOPT_POST, 1 ); // post submission method curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost); $ data = curl_exec ($ ch); // run curl curl_close ($ ch); return $ data ;} /*** send get request * @ 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 * get 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 ;} /*** send a custom template message * @ param $ touser * @ param $ template_id * @ param $ url * @ param $ data * @ param string $ topcolor * @ return bool */public functi On doSend ($ touser, $ template_id, $ url, $ data, $ topcolor = '# 7B68EE ') {/** data => array ('first' => array ('value' => urlencode ("Hello, you have purchased "), 'color' => "# 743A3A"), 'name' => array ('value' => urlencode ("product info: movie tickets in the micro age "), 'color' => '# eeeeeee'), 'remark' => array ('value' => urlencode ('permanently valid! Password: 1231313 '), 'color' =>' # ffff'),) */$ 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 ;}}}