Table of Contents
作者
修正记录:
SDK 源码:
调用方法:
Home php教程 php手册 微信公众平台自定义菜单及高级接口PHP SDK

微信公众平台自定义菜单及高级接口PHP SDK

Jun 13, 2016 am 09:36 AM
aspnet software programming

本文介绍介绍微信公众平台自定义菜单及高级接口的PHP SDK及使用方法。

 

作者

方倍工作室

修正记录:

2014.05.03 v1.0

 

SDK 源码:

<span   1</span> <span /*</span>
<span   2</span> <span     方倍工作室 http://www.cnblogs.com/txw1958/
</span><span   3</span> <span     CopyRight 2014 www.doucube.com All Rights Reserved
</span><span   4</span> <span */</span>
<span   5</span> 
<span   6</span> <span class</span><span  class_weixin_adv
</span><span   7</span> <span {
</span><span   8</span>     <span var</span> <span $appid</span> = ""<span ;
</span><span   9</span>     <span var</span> <span $appsecret</span> = ""<span ;
</span><span  10</span> 
<span  11</span>     <span //</span><span 构造函数,获取Access Token</span>
<span  12</span>     <span public</span> <span function</span> __construct(<span $appid</span> = <span NULL</span>, <span $appsecret</span> = <span NULL</span><span )
</span><span  13</span> <span     {
</span><span  14</span>         <span if</span>(<span $appid</span><span ){
</span><span  15</span>             <span $this</span>->appid = <span $appid</span><span ;
</span><span  16</span> <span         }
</span><span  17</span>         <span if</span>(<span $appsecret</span><span ){
</span><span  18</span>             <span $this</span>->appsecret = <span $appsecret</span><span ;
</span><span  19</span> <span         }
</span><span  20</span> 
<span  21</span>         <span //</span><span hardcode</span>
<span  22</span>         <span $this</span>->lasttime = 1395049256<span ;
</span><span  23</span>         <span $this</span>->access_token = "nRZvVpDU7LxcSi7GnG2LrUcmKbAECzRf0NyDBwKlng4nMPf88d34pkzdNcvhqm4clidLGAS18cN1RTSK60p49zIZY4aO13sF-eqsCs0xjlbad-lKVskk8T7gALQ5dIrgXbQQ_TAesSasjJ210vIqTQ"<span ;
</span><span  24</span> 
<span  25</span>         <span if</span> (<span time</span>() > (<span $this</span>->lasttime + 7200<span )){
</span><span  26</span>             <span $url</span> = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".<span $this</span>->appid."&secret=".<span $this</span>-><span appsecret;
</span><span  27</span>             <span $res</span> = <span $this</span>->https_request(<span $url</span><span );
</span><span  28</span>             <span $result</span> = json_decode(<span $res</span>, <span true</span><span );
</span><span  29</span>             <span //</span><span save to Database or Memcache</span>
<span  30</span>             <span $this</span>->access_token = <span $result</span>["access_token"<span ];
</span><span  31</span>             <span $this</span>->lasttime = <span time</span><span ();
</span><span  32</span> <span         }
</span><span  33</span> <span     }
</span><span  34</span> 
<span  35</span>     <span //</span><span 获取关注者列表</span>
<span  36</span>     <span public</span> <span function</span> get_user_list(<span $next_openid</span> = <span NULL</span><span )
</span><span  37</span> <span     {
</span><span  38</span>         <span $url</span> = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".<span $this</span>->access_token."&next_openid=".<span $next_openid</span><span ;
</span><span  39</span>         <span $res</span> = <span $this</span>->https_request(<span $url</span><span );
</span><span  40</span>         <span return</span> json_decode(<span $res</span>, <span true</span><span );
</span><span  41</span> <span     }
</span><span  42</span> 
<span  43</span>     <span //</span><span 获取用户基本信息</span>
<span  44</span>     <span public</span> <span function</span> get_user_info(<span $openid</span><span )
</span><span  45</span> <span     {
</span><span  46</span>         <span $url</span> = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".<span $this</span>->access_token."&openid=".<span $openid</span>."&lang=zh_CN"<span ;
</span><span  47</span>         <span $res</span> = <span $this</span>->https_request(<span $url</span><span );
</span><span  48</span>         <span return</span> json_decode(<span $res</span>, <span true</span><span );
</span><span  49</span> <span     }
</span><span  50</span> 
<span  51</span>     <span //</span><span 创建菜单</span>
<span  52</span>     <span public</span> <span function</span> create_menu(<span $data</span><span )
</span><span  53</span> <span     {
</span><span  54</span>         <span $url</span> = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".<span $this</span>-><span access_token;
</span><span  55</span>         <span $res</span> = <span $this</span>->https_request(<span $url</span>, <span $data</span><span );
</span><span  56</span>         <span return</span> json_decode(<span $res</span>, <span true</span><span );
</span><span  57</span> <span     }
</span><span  58</span> 
<span  59</span>     <span //</span><span 发送客服消息,已实现发送文本,其他类型可扩展</span>
<span  60</span>     <span public</span> <span function</span> send_custom_message(<span $touser</span>, <span $type</span>, <span $data</span><span )
</span><span  61</span> <span     {
</span><span  62</span>         <span $msg</span> = <span array</span>('touser' =><span $touser</span><span );
</span><span  63</span>         <span switch</span>(<span $type</span><span )
</span><span  64</span> <span         {
</span><span  65</span>             <span case</span> 'text':
<span  66</span>                 <span $msg</span>['msgtype'] = 'text'<span ;
</span><span  67</span>                 <span $msg</span>['text']    = <span array</span>('content'=> <span urlencode</span>(<span $data</span><span ));
</span><span  68</span>                 <span break</span><span ;
</span><span  69</span> <span         }
</span><span  70</span>         <span $url</span> = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".<span $this</span>-><span access_token;
</span><span  71</span>         <span return</span> <span $this</span>->https_request(<span $url</span>, <span urldecode</span>(json_encode(<span $msg</span><span )));
</span><span  72</span> <span     }
</span><span  73</span> 
<span  74</span>     <span //</span><span 生成参数二维码</span>
<span  75</span>     <span public</span> <span function</span> create_qrcode(<span $scene_type</span>, <span $scene_id</span><span )
</span><span  76</span> <span     {
</span><span  77</span>         <span switch</span>(<span $scene_type</span><span )
</span><span  78</span> <span         {
</span><span  79</span>             <span case</span> 'QR_LIMIT_SCENE': <span //</span><span 永久</span>
<span  80</span>                 <span $data</span> = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.<span $scene_id</span>.'}}}'<span ;
</span><span  81</span>                 <span break</span><span ;
</span><span  82</span>             <span case</span> 'QR_SCENE':       <span //</span><span 临时</span>
<span  83</span>                 <span $data</span> = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.<span $scene_id</span>.'}}}'<span ;
</span><span  84</span>                 <span break</span><span ;
</span><span  85</span> <span         }
</span><span  86</span>         <span $url</span> = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".<span $this</span>-><span access_token;
</span><span  87</span>         <span $res</span> = <span $this</span>->https_request(<span $url</span>, <span $data</span><span );
</span><span  88</span>         <span $result</span> = json_decode(<span $res</span>, <span true</span><span );
</span><span  89</span>         <span return</span> "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".<span urlencode</span>(<span $result</span>["ticket"<span ]);
</span><span  90</span> <span     }
</span><span  91</span>     
<span  92</span>     <span //</span><span 创建分组</span>
<span  93</span>     <span public</span> <span function</span> create_group(<span $name</span><span )
</span><span  94</span> <span     {
</span><span  95</span>         <span $data</span> = '{"group": {"name": "'.<span $name</span>.'"}}'<span ;
</span><span  96</span>         <span $url</span> = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=".<span $this</span>-><span access_token;
</span><span  97</span>         <span $res</span> = <span $this</span>->https_request(<span $url</span>, <span $data</span><span );
</span><span  98</span>         <span return</span> json_decode(<span $res</span>, <span true</span><span );
</span><span  99</span> <span     }
</span><span 100</span>     
<span 101</span>     <span //</span><span 移动用户分组</span>
<span 102</span>     <span public</span> <span function</span> update_group(<span $openid</span>, <span $to_groupid</span><span )
</span><span 103</span> <span     {
</span><span 104</span>         <span $data</span> = '{"openid":"'.<span $openid</span>.'","to_groupid":'.<span $to_groupid</span>.'}'<span ;
</span><span 105</span>         <span $url</span> = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=".<span $this</span>-><span access_token;
</span><span 106</span>         <span $res</span> = <span $this</span>->https_request(<span $url</span>, <span $data</span><span );
</span><span 107</span>         <span return</span> json_decode(<span $res</span>, <span true</span><span );
</span><span 108</span> <span     }
</span><span 109</span>     
<span 110</span>     <span //</span><span 上传多媒体文件</span>
<span 111</span>     <span public</span> <span function</span> upload_media(<span $type</span>, <span $file</span><span )
</span><span 112</span> <span     {
</span><span 113</span>         <span $data</span> = <span array</span>("media"  => "@".<span dirname</span>(<span __FILE__</span>).'\\'.<span $file</span><span );
</span><span 114</span>         <span $url</span> = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".<span $this</span>->access_token."&type=".<span $type</span><span ;
</span><span 115</span>         <span $res</span> = <span $this</span>->https_request(<span $url</span>, <span $data</span><span );
</span><span 116</span>         <span return</span> json_decode(<span $res</span>, <span true</span><span );
</span><span 117</span> <span     }
</span><span 118</span> 
<span 119</span>     <span //</span><span https请求(支持GET和POST)</span>
<span 120</span>     <span protected</span> <span function</span> https_request(<span $url</span>, <span $data</span> = <span null</span><span )
</span><span 121</span> <span     {
</span><span 122</span>         <span $curl</span> =<span  curl_init();
</span><span 123</span>         curl_setopt(<span $curl</span>, CURLOPT_URL, <span $url</span><span );
</span><span 124</span>         curl_setopt(<span $curl</span>, CURLOPT_SSL_VERIFYPEER, <span FALSE</span><span );
</span><span 125</span>         curl_setopt(<span $curl</span>, CURLOPT_SSL_VERIFYHOST, <span FALSE</span><span );
</span><span 126</span>         <span if</span> (!<span empty</span>(<span $data</span><span )){
</span><span 127</span>             curl_setopt(<span $curl</span>, CURLOPT_POST, 1<span );
</span><span 128</span>             curl_setopt(<span $curl</span>, CURLOPT_POSTFIELDS, <span $data</span><span );
</span><span 129</span> <span         }
</span><span 130</span>         curl_setopt(<span $curl</span>, CURLOPT_RETURNTRANSFER, 1<span );
</span><span 131</span>         <span $output</span> = curl_exec(<span $curl</span><span );
</span><span 132</span>         curl_close(<span $curl</span><span );
</span><span 133</span>         <span return</span> <span $output</span><span ;
</span><span 134</span> <span     }
</span><span 135</span> }
Copy after login

调用方法:

初始化对象

<span $weixin</span> = <span new</span> class_weixin_adv("wx6222221b11111111", "3079cb22ad383ae7371d12aed1b2d0cc");
Copy after login

查看Access Token

<span var_dump</span>(<span $weixin</span>->access_token);
Copy after login

创建二维码

<span var_dump</span>(<span $weixin</span>->create_qrcode("QR_SCENE", "134324234"));
Copy after login

获取关注者列表

<span var_dump</span>(<span $weixin</span>->get_user_list());
Copy after login

获取用户信息

<span $openid</span> = "oLVPpjkttuZTbwDwN7vjHNlqsmPs"<span ;
</span><span var_dump</span>(<span $weixin</span>->get_user_info(<span $openid</span>));
Copy after login

创建菜单

<span $data</span> ='{"button":[{"name":"方倍工作室","sub_button":[{"type":"click","name":"公司简介","key":"公司简介"},{"type":"click","name":"社会责任","key":"社会责任"},{"type":"click","name":"联系我们","key":"联系我们"}]},{"name":"产品服务","sub_button":[{"type":"click","name":"微信平台","key":"微信平台"},{"type":"click","name":"微博应用","key":"微博应用"},{"type":"click","name":"手机网站","key":"手机网站"}]},{"name":"技术支持","sub_button":[{"type":"click","name":"文档下载","key":"文档下载"},{"type":"click","name":"技术社区","key":"技术社区"},{"type":"click","name":"服务热线","key":"服务热线"}]}]}'<span ;
</span><span var_dump</span>(<span $weixin</span>->create_menu(<span $data</span>));
Copy after login

用户分组

<span var_dump</span>(<span $weixin</span>->create_group("老师"<span ));
</span><span var_dump</span>(<span $weixin</span>->update_group(<span $openid</span>, "100"));
Copy after login

上传下载多媒体

<span var_dump</span>(<span $weixin</span>->upload_media("image","pondbay.jpg"));
Copy after login

发送客服消息

<span var_dump</span>(<span $weixin</span>->send_custom_message(<span $openid</span>, "text", "asdf"));
Copy after login

 

 

=========================================================

方倍工作室微信公众平台账号关注方法:
1. 微信通讯录-添加朋友-查找公众号-搜索“方倍工作室”
2. 微信通讯录-添加朋友-搜号码-输入“pondbaystudio”
3. 使用微信扫描下面的二维码

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The combination of Vue.js and ASP.NET provides tips and suggestions for performance optimization and expansion of web applications. The combination of Vue.js and ASP.NET provides tips and suggestions for performance optimization and expansion of web applications. Jul 29, 2023 pm 05:19 PM

The combination of Vue.js and ASP.NET provides tips and suggestions for performance optimization and expansion of web applications. With the rapid development of web applications, performance optimization has become an indispensable and important task for developers. As a popular front-end framework, Vue.js combined with ASP.NET can help us achieve better performance optimization and expansion. This article will introduce some tips and suggestions, and provide some code examples. 1. Reduce HTTP requests The number of HTTP requests directly affects the loading speed of web applications. pass

MySQL connection pool usage and optimization techniques in ASP.NET programs MySQL connection pool usage and optimization techniques in ASP.NET programs Jun 30, 2023 pm 11:54 PM

How to correctly use and optimize the MySQL connection pool in ASP.NET programs? Introduction: MySQL is a widely used database management system that features high performance, reliability, and ease of use. In ASP.NET development, using MySQL database for data storage is a common requirement. In order to improve the efficiency and performance of database connections, we need to correctly use and optimize the MySQL connection pool. This article will introduce how to correctly use and optimize the MySQL connection pool in ASP.NET programs.

Ten ways generative AI will change software development Ten ways generative AI will change software development Mar 11, 2024 pm 12:10 PM

Translator | Reviewed by Chen Jun | Chonglou In the 1990s, when people mentioned software programming, it usually meant choosing an editor, checking the code into the CVS or SVN code base, and then compiling the code into an executable file. Corresponding integrated development environments (IDEs) such as Eclipse and Visual Studio can integrate programming, development, documentation, construction, testing, deployment and other steps into a complete software development life cycle (SDLC), thus improving the work of developers. efficiency. In recent years, popular cloud computing and DevSecOps automation tools have improved developers' comprehensive capabilities, making it easier for more enterprises to develop, deploy and maintain software applications. Today, generative AI is the next generation development

How to reconnect to MySQL in ASP.NET program? How to reconnect to MySQL in ASP.NET program? Jun 29, 2023 pm 02:21 PM

How to reconnect to MySQL in ASP.NET program? In ASP.NET development, it is very common to use the MySQL database. However, due to network or database server reasons, the database connection may sometimes be interrupted or time out. In this case, in order to ensure the stability and reliability of the program, we need to re-establish the connection after the connection is disconnected. This article will introduce how to reconnect MySQL connections in ASP.NET programs. To reference the necessary namespaces first, reference them at the head of the code file

The combination of Vue.js and ASP.NET enables the development and deployment of enterprise-level applications The combination of Vue.js and ASP.NET enables the development and deployment of enterprise-level applications Jul 29, 2023 pm 02:37 PM

The combination of Vue.js and ASP.NET enables the development and deployment of enterprise-level applications. In today's rapidly developing Internet technology field, the development and deployment of enterprise-level applications has become more and more important. Vue.js and ASP.NET are two technologies widely used in front-end and back-end development. Combining them can bring many advantages to the development and deployment of enterprise-level applications. This article will introduce how to use Vue.js and ASP.NET to develop and deploy enterprise-level applications through code examples. First, we need to install

How to correctly configure and use MySQL connection pool in ASP.NET program? How to correctly configure and use MySQL connection pool in ASP.NET program? Jun 29, 2023 pm 12:56 PM

How to correctly configure and use MySQL connection pool in ASP.NET program? With the development of the Internet and the increase in data volume, the demand for database access and connections is also increasing. In order to improve the performance and stability of the database, connection pooling has become an essential technology. This article mainly introduces how to correctly configure and use the MySQL connection pool in ASP.NET programs to improve the efficiency and response speed of the database. 1. The concept and function of connection pooling. Connection pooling is a technology that reuses database connections. At the beginning of the program,

What are the built-in objects in aspnet? What are the built-in objects in aspnet? Nov 21, 2023 pm 02:59 PM

The built-in objects in ASP.NET include "Request", "Response", "Session", "Server", "Application", "HttpContext", "Cache", "Trace", "Cookie" and "Server.MapPath": 1. Request, indicating the HTTP request issued by the client; 2. Response: indicating the HTTP response returned by the web server to the client, etc.

Recommended configuration for ASP.NET development using Visual Studio on Linux Recommended configuration for ASP.NET development using Visual Studio on Linux Jul 06, 2023 pm 08:45 PM

Overview of the recommended configuration for using Visual Studio for ASP.NET development on Linux: With the development of open source software and the popularity of the Linux operating system, more and more developers are beginning to develop ASP.NET on Linux. As a powerful development tool, Visual Studio has always occupied a dominant position on the Windows platform. This article will introduce how to configure VisualStudio for ASP.NE on Linux

See all articles