WeChat template message call-PHP source code

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

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.