Micro-Letter custom menu creation/query/cancellation PHP sample code _php Tips

Source: Internet
Author: User
Tags curl mixed php sample code

Micro-Credit Public account service number can use the custom menu function. It failed to create the menu before, because the $DATA format has not been transmitted correctly, and then finally resolved. Here is a record of the passing of a class to facilitate custom menu management. This class is just a management class for custom menus and does not involve the code for micro-mail custom replies and menu events.

The code is as follows

 /** * @author LSH 2013-09-06 * * Micro-Letter custom Menu Creation | query | Cancel * * Class Weixinmenu {public static $appid = null; The requested AppID public static $secret = null;

 The requested secret public static $getToken = "Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

 public static $createMenu = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";

 public static $selMenu = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=";

 public static $delMenu = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="; public static $opt = Array (Curlopt_ssl_verifypeer => false, Curlopt_ssl_verifyhost => false, Curlopt_useragen T => ' mozilla/5.0 (compatible; MSIE 5.01; 

 Windows NT 5.0) ', Curlopt_followlocation => 1, curlopt_autoreferer =>1, Curlopt_returntransfer => true);

 Public $ACCESS _token = null;

 /** * Create Menu */Public function Create () {$this->token (); $strMeau = ' {button ': [{' Type ': ' Click ', ' na 'Me ": Menu left", "key": "V_menu_left"}, {"Type": "click", "Name": "Menu", "Key": "V_menu_center"

 }, {"Type": "click", "Name": "Menu Right", "key": "V_menu_right"}]} ';

 $ret = $this->httppost (self:: $createMenu. $this->access_token,self:: $opt, $strMeau);
 Echo $ret;

 /** * Query Menu */Public Function sel () {$this->token ();

 $ret = $this->httpget (self:: $selMenu. $this->access_token,self:: $opt);
 Echo $ret;

 /** * Cancel Menu */Public Function del () {$this->token ();

 $ret = $this->httpget (self:: $delMenu. $this->access_token,self:: $opt);
 Echo $ret; /** * Get token/Private Function token () {$tokenUrl = self:: $getToken. " &appid= ". Self:: $appid."
 &secret= ". Self:: $secret;

 $ret = $this->httpget ($TOKENURL, Self:: $opt);

 $arrRet = Json_decode ($ret, true);
 $this->access_token = $arrRet [' Access_token ']; /** * Post mode * @param string $url post address * @param array $opt post options * @param arraY $post _data Post data * @return Mixed/Private Function HttpPost ($url, $opt = Array (), $post _data) {$setopt = array (Curlopt_header => 0, Curlopt_returntransfer => 1, curlopt_url => $url, Curlopt_customreq

 Uest => ' POST ', Curlopt_post => 1, curlopt_postfields => $post _data,);
  if (!empty ($opt)) {foreach ($opt as $key => $value) {$setopt [$key] = $value;

 }} $curl = Curl_init ($url);
 foreach ($setopt as $key => $value) {curl_setopt ($curl, $key, $value);

 } $responseText = Curl_exec ($curl);

 Curl_close ($curl);
 return $responseText; /** * Get method * @param stinrg $url Get URL * @param array $opt get options * @return Mixed/Private function Http Get ($url, $opt = Array ()) {$setopt = array (curlopt_header => 0, Curlopt_returntransfer => 1, Curlopt_url

 => $url);
  if (!empty ($opt)) {foreach ($opt as $key => $value) {$setopt [$key] = $value; }} $curl = CUrl_init ($url);
 foreach ($setopt as $key => $value) {curl_setopt ($curl, $key, $value);

 } $responseText = Curl_exec ($curl);

 Curl_close ($curl);
 return $responseText;
 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.