WeChat template message PHP, template message php

Source: Internet
Author: User
Tags oauth openid

Template message PHP, template message php

SDK:

<? Phpclass Oauth {// obtain the global access_tokenpublic function get_token (){
// If access_token already exists, return access_token // if ($ _ SESSION ['Access _ token'] & $ _ SESSION ['expire _ time']> time ()) {// return $ _ SESSION ['Access _ token']; //} else {// 1. request url address $ appid = APPID; // appid $ appsecret = APPSECRET; // appsecret $ url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". $ appid. "& secret = ". $ appsecret; // request address // 2 initialize curl request $ ch = curl_init (); // 3. configure the request parameter curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); // skip the certificate check curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false ); // check from the certificate whether the SSL encryption algorithm has curl_setopt ($ ch, CURLOPT_URL, $ url); // request curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // do not directly output data // 4. start request $ res = curl_exec ($ ch); // obtain the request result if (curl_errno ($ ch) {var_d Ump (curl_error ($ ch); // print the error message} // 5. close curlcurl_close ($ ch); $ arr = json_decode ($ res, true ); // convert the result to an array. // $ _ SESSION ['Access _ token'] = $ arr ['Access _ token']; // save access_token to the session, you do not need to save the token. Each time you get a new token // $ _ SESSION ['expire _ time'] = time () + 7200; return $ arr ['Access _ token']; ///} // push template information parameters: openid of the sender, customer name, customer phone number, recommended Real Estate (custom parameters) function sendMessage ($ openid, $ customName, $ customPhone, $ reportBuilding) {// obtain the global token $ token = $ This-> get_token (); $ url = "https://api.weixin.qq.com/cgi-bin/message/template/send? Access_token = ". $ token; // template information request address // The template information sent (json format is required, which is in the array format (to facilitate variable addition) and then converted to json format) $ post_data = array ("touser" => $ openid, // to whom to push, openid "template_id" => "nKu4eyktzxOslxq0KfPxhGXbiOo873K9mIxKvs23EVU ", // background template information id "url" => "http://www.baidu.com ", // The following is an example of the booking template "data" => array ("first" => array ("value" => "you have new customers. Please check them in time! "," Color "=>" #173177 ")," customName "=> array (" value "=> $ customName, // The passed variable "color" => "#173177"), "customPhone" => array ("value" => $ customPhone, "color" => "#173177"), "reportBuilding" => array ("value" => $ reportBuilding, "color" => "#173177 "), "reportTime" => array ("value" => date ('Y-m-d H: I: s'), "color" => "#173177 "), "remark" => array ("value" => "please contact the customer in time! "," Color "=>" #173177 "),); // convert the preceding array data to the json format $ post_data = json_encode ($ post_data); // send data, post method
// Configure curl request $ ch = curl_init (); // create curl request curl_setopt ($ ch, CURLOPT_URL, $ url); // set the url for sending data curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // If the returned value is 0, curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, 0) is displayed directly; // disable certificate verification curl_setopt ($ ch, CURLOPT_POST, 1 ); // post Method Request curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); // The data packet sent by the post request // receive the data returned by the execution $ data = curl_exec ($ ch ); // close the handle curl_close ($ ch); $ data = json_decode ($ data, true); // convert json data Return $ data;} // get template information-industry information (reference, example not used) function getHangye () {// After the user agrees to the authorization, A code $ token = $ this-> get_token (); $ url = "https://api.weixin.qq.com/cgi-bin/template/get_industry? Access_token = ". $ token; // request token, get method $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, 0); $ data = curl_exec ($ ch); curl_close ($ ch); $ data = json_decode ($ data, true ); // convert json data into an array // return $ data ["access_token"]; return $ data ;}}

PHP code:

// Push the template information to the real estate consultant $ send = new Oauth (); // instantiate the class $ send-> sendMessage ($ zhiyeguwen, $ clientName, $ tel, $ product ); // call Method

Complete. The template information is not difficult. If you have any questions, contact each other !!!

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.