Home  >  Article  >  Backend Development  >  WeChat public platform development (99) Custom menu to obtain OpenID_PHP tutorial

WeChat public platform development (99) Custom menu to obtain OpenID_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:26:561202browse

Keyword WeChat public platform custom menu OpenID
Author: Fangbei Studio
Original text: http://www.cnblogs.com/txw1958/p/weixin-menu-get-openid .html

In this WeChat public platform development tutorial, we will introduce how to obtain the user's OpenID in the custom menu.

The essence of this development tutorial is the flexible use of WeChat custom menu and OAuth2.0 authorization.

This article is divided into the following four parts:

1. Have advanced interface permissions

When you have advanced interface permissions, we use OAuth2.0 authorization to obtain them.

For detailed setting methods of OAuth2.0, please refer to WeChat Public Platform Development (71) OAuth2.0 Web Authorization

After setting the domain name of the authorization callback page, we need to construct a menu. In order to facilitate the explanation of the next section at the same time, our menu structure is as follows

<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>

This will generate two menus as follows

When you click Authorization Obtain, you will access the authorization page, which will use OAuth2.0 to obtain the openid.

For the code, please refer to "Best Practices in WeChat Public Platform Development" page 72.

The following is displayed after the jump:

2. No advanced interface permissions

For accounts without advanced interface permissions, you cannot use the view event to obtain the OpenID. You need to use the click event to achieve this

The implementation principle is to obtain the FromUserName value in the received message and construct the openid parameter in the url

The response code in the custom menu is as follows

<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> }

The effect after running is as follows.

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

How to follow Fangbei Studio’s WeChat public platform account:
1. WeChat address book-add friend-search public account-search for “Fangbei Studio”
2. WeChat address book-add friend-search Number - enter "pondbaystudio"
3. Use WeChat to scan the QR code below

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/821269.htmlTechArticleKeywords WeChat public platform custom menu OpenID Author: Fangbei Studio Original text: http://www.cnblogs .com/txw1958/p/weixin-menu-get-openid.html In this WeChat public platform development tutorial...
Statement:
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