PHP WeChat Template Message operation example _php tips

Source: Internet
Author: User
Tags oauth openid php template
This article mainly introduces the PHP template message operation method, combined with instance form analysis of PHP template message definition and call method, the need for friends can refer to the next

Specific as follows:

Sdk:

<?phpclass Oauth {//Get global Access_token Public Function Get_token () {//If there is already a direct return Access_token//if ($_session[' acces    S_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. Configuration Request parameter curl_setopt ($ch, Curlopt_ssl_verifypeer, false); Skip certificate Check curl_setopt ($ch, Curlopt_ssl_verifyhost, false);  Check that the SSL encryption algorithm exists curl_setopt ($ch, Curlopt_url, $url) from the certificate; Request curl_setopt ($ch, Curlopt_returntransfer, 1); Does not directly output data//4. Start request $res = Curl_exec ($ch);    Gets the request result if (Curl_errno ($ch)) {Var_dump (Curl_error ($ch));//Print error message}//5. Turn off Curl Curl_close ($ch); $arr = Json_decode ($res, true);  Convert the result to an array//$_session[' Access_token ']= $arr [' Access_token ']; Will ACcess_token deposit SESSION, you can not save, each time get a new token//$_session[' Expire_time ']=time () +7200;    return $arr [' Access_token ']; }}//push template information parameters: Who to send to OpenID, customer name, customer phone, recommended property (parametric custom) function sendMessage ($openid, $customName, $customPhone, $    reportbuilding) {//get global token $token = $this->get_token (); $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=". $token; Template Information request address//Send template information (requires JSON format, here is an array (easy to add variable) format, and then to JSON) $post _data = Array ("touser" + = $openid,//push to whom, open ID "template_id" = "Nku4eyktzxoslxq0kfpxhgxbioo873k9mixkvs23evu",//background template information id "url" = "Http://www.baidu". COM ",//below is an example of an appointment view template" data "= = Array (" first "= = Array (" Value "=" You have a new customer, please check in time! ") "," color "=" #173177 ")," Customname "=>array (" value "= = $custo Mname,//Pass the 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 customers in time!"    "," color "=" #173177 "),));    Convert the above array data to JSON format $post _data = Json_encode ($post _data);  Send data, Post mode//configure Curl Request $ch = Curl_init (); Create Curl Request curl_setopt ($ch, Curlopt_url, $url); Set the URL to send data curl_setopt ($ch, curlopt_returntransfer,1); Set the return value, 0, direct display curl_setopt ($ch, curlopt_ssl_verifypeer,0);  Disable certificate validation curl_setopt ($ch, Curlopt_post, 1);    The Post method requests curl_setopt ($ch, Curlopt_postfields, $post _data),//post request sent packets//Receive to perform the returned data $data = Curl_exec ($ch);    Close handle curl_close ($ch); $data = Json_decode ($data, true); The JSON numberAccording to the group return $data;    }//Get template information-industry information (reference, example not used) function Gethangye () {//user agrees to authorize, will pass over a code $token = $this->get_token ();    $url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=". $token;    Request Token,get Mode $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 to a group//return $data ["Access_token"];  return $data; }}

PHP Code:

Push template information to real estate consultant $send = new Oauth ();  Instantiate class $send->sendmessage ($zhiyeguwen, $clientName, $tel, $product); Calling methods

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.