Template message call
[PHP] code
/** Curl request */public function wtw_request ($ url, $ data = null) {$ curl = curl_init (); // Start a CURL session curl_setopt ($ curl, CURLOPT_URL, $ url); // The address to be accessed curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, false); // Check the certificate source curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, false ); // check from the certificate whether the SSL encryption algorithm has curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['http _ USER_AGENT ']); // simulate the browser if ($ data! = Null) {curl_setopt ($ curl, CURLOPT_POST, 1); // send a regular Post request curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data ); // data packet submitted by Post} curl_setopt ($ curl, CURLOPT_TIMEOUT, 300); // sets the timeout limit to prevent endless loops curl_setopt ($ curl, CURLOPT_HEADER, 0 ); // display the returned Header content curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information returns $ info = curl_exec ($ curl) as a file stream ); // execute the operation if (curl_errno ($ curl) {echo 'errno :'. curl_getinfo ($ curl); // capture an exception dump (curl_getinfo ($ curl);} return $ info;}/** Template Message */public function templetemsg () {// Obtain ACCESS_TOKEN/* string (86) "*****" * string (45) "{" errcode ": 0," errmsg ":" OK ", "msgid": 201652404} "*/$ ACCESS_TOKEN = $ this-> get_Token (); $ msg_url =" https://api.weixin.qq.com/cgi-bin/message/template/send?access_token= ". $ ACCESS_TOKEN." "; $ time = date (" Y-m-d H: I: s ", time (); $ url =" http://www . Baidu.com /";
/// The request package is a json: $ msg_json = '{"touser": "oPLOfjqw1fKtdUQPk1vDXCf7K8s0", "template_id": "iZOHjFSa-Ax2UuDlzjJx6IrwUrSvdDhD4TQPnM8kFSY", "url ":"'. $ url. '"," topcolor ":" # FF0000 "," data ": {" first ": {" value ":" Dear Sir: \ n Congratulations, you have successfully called the template interface \ n "," color ":" #000000 "}," keyword1 ": {" value ": "Template interface call Test \ n", "color": "#000000"}, "keyword2": {"value ":"'. $ time. '\ n "," color ":" #000000 "}," remark ": {" value ": "\ n \ nThis message is intended only for the use of the inpidual or entity activated", "color": "# 0099FF "}}}'; $ result = $ this-> wtw_request ($ msg_url, $ msg_json); // $ result = $ this-> msg_request ($ msg_url, $ msg_json, $ header = array (); dump ($ result );}