Use php to send WeChat template messages-PHP source code

Source: Internet
Author: User
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 ;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.