Heim > Artikel > Backend-Entwicklung > 微信公众平台开发(99) 自定义菜单获取OpenID_PHP教程
关键字 微信公众平台 自定义菜单 OpenID
作者:方倍工作室
原文:http://www.cnblogs.com/txw1958/p/weixin-menu-get-openid.html
在这篇微信公众平台开发教程中,我们将介绍如何在自定义菜单中获得用户的OpenID。
本篇开发教程的实质是微信自定义菜单及OAuth2.0授权的灵活运用。
本文分为以下四个部分:
拥有高级接口权限时,我们使用OAuth2.0授权的方式来获取。
OAuth2.0的详细设置方法,请参考 微信公众平台开发(71)OAuth2.0网页授权
在设置好授权回调页面域名之后,我们需要构造菜单,为了方便同时讲解下一小节,我们菜单构造如下
<span>{ </span><span>"</span><span>button</span><span>"</span><span>: [ { </span><span>"</span><span>type</span><span>"</span>: <span>"</span><span>click</span><span>"</span><span>, </span><span>"</span><span>name</span><span>"</span>: <span>"</span><span>图文获取</span><span>"</span><span>, </span><span>"</span><span>key</span><span>"</span>: <span>"</span><span>图文</span><span>"</span><span> }, { </span><span>"</span><span>type</span><span>"</span>: <span>"</span><span>view</span><span>"</span><span>, </span><span>"</span><span>name</span><span>"</span>: <span>"</span><span>授权获取</span><span>"</span><span>, </span><span>"</span><span>url</span><span>"</span>: <span>"</span><span>https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx0738856156f7323c&redirect_uri=http://israel.sinaapp.com/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect</span><span>"</span><span> } ] }</span>
这样将生成两个菜单如下
当点击 授权获取的时候,将访问授权页面,页面将使用OAuth2.0的方式获得openid。
代码可以参考《微信公众平台开发最佳实践》第72页。
跳转后显示如下:
对于没有高级接口权限的账号,就不能使用view事件获得OpenID,需要使用click事件来实现
实现原理就是获得接收消息中的FromUserName值,并且在url中构造openid的参数即可
在自定义菜单中响应代码如下
<span> 1</span> <span>private</span> <span>function</span> receiveEvent(<span>$object</span><span>) </span><span> 2</span> <span>{ </span><span> 3</span> <span>$content</span> = ""<span>; </span><span> 4</span> <span>switch</span> (<span>$object</span>-><span>Event) </span><span> 5</span> <span> { </span><span> 6</span> <span>case</span> "subscribe": <span> 7</span> <span>$content</span>[] = <span>array</span>("Title"=>"欢迎关注方倍工作室", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"<span>); </span><span> 8</span> <span>break</span><span>; </span><span> 9</span> <span>case</span> "CLICK": <span>10</span> <span>switch</span> (<span>$object</span>-><span>EventKey) </span><span>11</span> <span> { </span><span>12</span> <span>case</span> "图文": <span>13</span> <span>$content</span>[] = <span>array</span>("Title"=>"OpenID", "Description"=>"你的OpenID为:".<span>$object</span>->FromUserName, "PicUrl"=>"", "Url" =>"http://m.cnblogs.com/?u=txw1958&openid=".<span>$object</span>-><span>FromUserName); </span><span>14</span> <span>break</span><span>; </span><span>15</span> <span> } </span><span>16</span> <span>break</span><span>; </span><span>17</span> <span> } </span><span>18</span> <span>if</span>(<span>is_array</span>(<span>$content</span><span>)){ </span><span>19</span> <span>$result</span> = <span>$this</span>->transmitNews(<span>$object</span>, <span>$content</span><span>); </span><span>20</span> }<span>else</span><span>{ </span><span>21</span> <span>$result</span> = <span>$this</span>->transmitText(<span>$object</span>, <span>$content</span><span>); </span><span>22</span> <span> } </span><span>23</span> <span>return</span> <span>$result</span><span>; </span><span>24</span> }
运行后效果如下所示。
============================================
方倍工作室微信公众平台账号关注方法:
1. 微信通讯录-添加朋友-查找公众号-搜索“方倍工作室”
2. 微信通讯录-添加朋友-搜号码-输入“pondbaystudio”
3. 使用微信扫描下面的二维码