php版微信公众平台账号自定义菜单类

微信公众平台服务号可申请自定义菜单了,其它的号暂时不支持自定义菜单了,这个不但可以使用api来操作,还可以直接在后台定义菜单与参数哦。

申请自定义菜单

服务号可以申请自定义菜单,使用QQ登录的公众号,可以升级为邮箱登录,使用邮箱登录的公众号,可以修改登录邮箱,群发消息可以同步到腾讯微博.

微信公众平台账号api程序

  1. <?php
  2. //define your token
  3. define("TOKEN", "chenxiang");//改成自己的TOKEN
  4. define('APP_ID', '');//改成自己的APPID
  5. define('APP_SECRET', '');//改成自己的APPSECRET
  6. $wechatObj = new wechatCallbackapiTest(APP_ID,APP_SECRET);
  7. $wechatObj->Run();
  8. class wechatCallbackapiTest
  9. {
  10. private $fromUsername;
  11. private $toUsername;
  12. private $times;
  13. private $keyword;
  14. private $app_id;
  15. private $app_secret;
  16. public function __construct($appid,$appsecret)
  17. {
  18. # code...
  19. $this->app_id = $appid;
  20. $this->app_secret = $appsecret;
  21. }
  22. public function valid()
  23. {
  24. $echoStr = $_GET["echostr"];
  25. if($this->checkSignature()){
  26. echo $echoStr;
  27. exit;
  28. }
  29. }
  30. /**
  31. * 运行程序
  32. * @param string $value [description]
  33. */
  34. public function Run()
  35. {
  36. $this->responseMsg();
  37. $arr[]= "您好,这是自动回复,我现在不在,有事请留言,我会尽快回复你的^_^";
  38. echo $this->make_xml("text",$arr);
  39. }
  40. public function responseMsg()
  41. {
  42. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//返回回复数据
  43. if (!emptyempty($postStr)){
  44. $access_token = $this->get_access_token();//获取access_token
  45. $this->createmenu($access_token);//创建菜单
  46. //$this->delmenu($access_token);//删除菜单
  47. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  48. $this->fromUsername = $postObj->FromUserName;//发送消息方ID
  49. $this->toUsername = $postObj->ToUserName;//接收消息方ID
  50. $this->keyword = trim($postObj->Content);//用户发送的消息
  51. $this->times = time();//发送时间
  52. $MsgType = $postObj->MsgType;//消息类型
  53. if($MsgType=='event'){
  54. $MsgEvent = $postObj->Event;//获取事件类型
  55. if ($MsgEvent=='subscribe') {//订阅事件
  56. $arr[] = "你好,我是xxx,现在我们是好友咯![愉快][玫瑰]";
  57. echo $this->make_xml("text",$arr);
  58. exit;
  59. }elseif ($MsgEvent=='CLICK') {//点击事件
  60. $EventKey = $postObj->EventKey;//菜单的自定义的key值,可以根据此值判断用户点击了什么内容,从而推送不同信息
  61. $arr[] = $EventKey;
  62. echo $this->make_xml("text",$arr);
  63. exit;
  64. }
  65. }
  66. }else {
  67. echo "this a file for weixin API!";
  68. exit;
  69. }
  70. }
  71. /**
  72. * 获取access_token
  73. */
  74. private function get_access_token()
  75. {
  76. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&app&secret=".$this->app_secret;
  77. $data = json_decode(file_get_contents($url),true);
  78. if($data['access_token']){
  79. return $data['access_token'];
  80. }else{
  81. return "获取access_token错误";
  82. }
  83. }
  84. /**
  85. * 创建菜单
  86. * @param $access_token 已获取的ACCESS_TOKEN
  87. */
  88. public function createmenu($access_token)
  89. {
  90. $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
  91. $arr = array(
  92. 'button' =>array(
  93. array(
  94. 'name'=>urlencode("生活查询"),
  95. 'sub_button'=>array(
  96. array(
  97. 'name'=>urlencode("天气查询"),
  98. 'type'=>'click',
  99. 'key'=>'VCX_WEATHER'
  100. ),
  101. array(
  102. 'name'=>urlencode("身份证查询"),
  103. 'type'=>'click',
  104. 'key'=>'VCX_IDENT'
  105. )
  106. )
  107. ),
  108. array(
  109. 'name'=>urlencode("轻松娱乐"),
  110. 'sub_button'=>array(
  111. array(
  112. 'name'=>urlencode("刮刮乐"),
  113. 'type'=>'click',
  114. 'key'=>'VCX_GUAHAPPY'
  115. ),
  116. array(
  117. 'name'=>urlencode("幸运大转盘"),
  118. 'type'=>'click',
  119. 'key'=>'VCX_LUCKPAN'
  120. )
  121. )
  122. ),
  123. array(
  124. 'name'=>urlencode("我的信息"),
  125. 'sub_button'=>array(
  126. array(
  127. 'name'=>urlencode("关于我"),
  128. 'type'=>'click',
  129. 'key'=>'VCX_ABOUTME'
  130. ),
  131. array(
  132. 'name'=>urlencode("工作信息"),
  133. 'type'=>'click',
  134. 'key'=>'VCX_JOBINFORMATION'
  135. )
  136. )
  137. )
  138. )
  139. );
  140. $jsondata = urldecode(json_encode($arr));
  141. $ch = curl_init();
  142. curl_setopt($ch,CURLOPT_URL,$url);
  143. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  144. curl_setopt($ch,CURLOPT_POST,1);
  145. curl_setopt($ch,CURLOPT_POSTFIELDS,$jsondata);
  146. curl_exec($ch);
  147. curl_close($ch);
  148. }
  149. /**
  150. * 查询菜单
  151. * @param $access_token 已获取的ACCESS_TOKEN
  152. */
  153. private function getmenu($access_token)
  154. {
  155. # code...
  156. $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token;
  157. $data = file_get_contents($url);
  158. return $data;
  159. }
  160. /**
  161. * 删除菜单
  162. * @param $access_token 已获取的ACCESS_TOKEN
  163. */
  164. private function delmenu($access_token)
  165. {
  166. # code...
  167. $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$access_token;
  168. $data = json_decode(file_get_contents($url),true);
  169. if ($data['errcode']==0) {
  170. # code...
  171. return true;
  172. }else{
  173. return false;
  174. }
  175. }
  176. /**
  177. *@param type: text 文本类型, news 图文类型
  178. *@param value_arr array(内容),array(ID)
  179. *@param o_arr array(array(标题,介绍,图片,超链接),...小于10条),array(条数,ID)
  180. */
  181. private function make_xml($type,$value_arr,$o_arr=array(0)){
  182. //=================xml header============
  183. $con="<xml>
  184. <ToUserName><![CDATA[{$this->fromUsername}]]></ToUserName>
  185. <FromUserName><![CDATA[{$this->toUsername}]]></FromUserName>
  186. <CreateTime>{$this->times}</CreateTime>
  187. <MsgType><![CDATA[{$type}]]></MsgType>";
  188. //=================type content============
  189. switch($type){
  190. case "text" :
  191. $con.="<Content><![CDATA[{$value_arr[0]}]]></Content>
  192. <FuncFlag>{$o_arr}</FuncFlag>";
  193. break;
  194. case "news" :
  195. $con.="<ArticleCount>{$o_arr[0]}</ArticleCount>
  196. <Articles>";
  197. foreach($value_arr as $id=>$v){
  198. if($id>=$o_arr[0]) break; else null; //判断数组数不超过设置数
  199. $con.="<item>
  200. <Title><![CDATA[{$v[0]}]]></Title>
  201. <Description><![CDATA[{$v[1]}]]></Description>
  202. <PicUrl><![CDATA[{$v[2]}]]></PicUrl>
  203. <Url><![CDATA[{$v[3]}]]></Url>
  204. </item>";
  205. }
  206. $con.="</Articles>
  207. <FuncFlag>{$o_arr[1]}</FuncFlag>";
  208. break;
  209. } //end switch
  210. //=================end return============
  211. $con.="</xml>";
  212. return $con;
  213. }
  214. private function checkSignature()
  215. {
  216. $signature = $_GET["signature"];
  217. $timestamp = $_GET["timestamp"];
  218. $nonce = $_GET["nonce"];
  219. $token = TOKEN;
  220. $tmpArr = array($token, $timestamp, $nonce);
  221. sort($tmpArr);
  222. $tmpStr = implode( $tmpArr );
  223. $tmpStr = sha1( $tmpStr );
  224. if( $tmpStr == $signature ){
  225. return true;
  226. }else{
  227. return false;
  228. }
  229. }
  230. }
  231. ?>