PHP WeChat template message Push step

Source: Internet
Author: User
Tags php template urlencode
This time to everyone to bring PHP template message push steps in detail, PHP template message Push the note of what, the following is the actual case, together to see.

1. Public number Template message configuration

2.PHP Code

/*** Send Template message */Public function Send_notice () {//Get Access_token if ($_cookie[' Access_token ']) {$access _token2=$_cookie[  ' Access_token ']; }else{$json _token= $this >curl_post ("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential &appid= '. $appid. '  &secret= '. $appsecret. ');  $access _token1=json_decode ($json _token,true);  $access _token2= $access _token1[' Access_token ';  Setcookie (' Access_token ', $access _token2,7200);   }//Template message $json _template = $this->json_tempalte ();   $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=". $access _token2;   $res = $this->curl_post ($url, UrlDecode ($json _template));   if ($res [' Errcode ']==0) {return ' sent successfully ';   }else{return ' send failed '; }}/** * Format the template message JSON */Public Function Json_tempalte () {//Template message $template =array (' touser ' = '). $openi D. ',//user OpenID ' template_id ' + '. $tenpalate _id. ',//template id ' url ' + ' configured under the public number '. $uel. ",//click on the template message to jump to the link ' to Pcolor ' + ' #7B68EE ',      ' Data ' =>array (' =>array ' (' Value ' =>urlencode ("Your Activity passed"), ' color ' = ' #FF0000 '), ' keyword1 ' =>array (' Value ' =>urlencode (' Test article title '), ' color ' = ' #FF0000 '),//keyword needs to correspond to the configured template message ' Keyword2 ' =>array (' Value ' =>urlencode (Date ("y-m-d h:i:s")), ' color ' = ' #FF0000 '), ' Keyword3 ' =>array (' Value ' =>urlencode (' Test publisher '), ' color ' = ' #FF0000 '), ' Keyword4 ' =>array (' Value ' =>urlencode (' Test status '), ' color ' = ' #FF0000 '), '    Remark ' =>array (' Value ' =>urlencode (' remark: this is Test '), ' color ' = ' #FF0000 '),));    $json _template=json_encode ($template);  return $json _template;     }/** * @param $url * @param array $data * @return Mixed * Curl Request */function Curl_post ($url, $data =array ()) {    $ch = Curl_init ();    curl_setopt ($ch, Curlopt_url, $url);    curl_setopt ($ch, Curlopt_returntransfer, 1);    curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);    curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE); Post Data curl_setopt ($CH, curlopt_post, 1);    Add the post variables to curl_setopt ($ch, Curlopt_postfields, $data);    $output = curl_exec ($ch);    Curl_close ($ch);  return $output; }

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Decorator Mode Use detailed

Php Template Method Pattern use detailed

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.