Processing and development example of WeChat custom menu

Source: Internet
Author: User

Development example of custom menu Processing

Create a custom menu

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

<? Php

 

Define ("APPID", "Your appid ");

Define ("APPSECRET", "Your appsecret ");

 

$ Token_access_url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". APPID." & secret = ". APPSECRET;

$ Res = file_get_contents ($ token_access_url); // get the file content or the content of the network request

// Echo $ res;

$ Result = json_decode ($ res, true); // receives a JSON string and converts it to a PHP variable.

$ Access_token = $ result ['Access _ token'];

 

Define ("ACCESS_TOKEN", $ access_token); // defines access_token as a constant for ease of use.

 

$ Make_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = ". ACCESS_TOKEN;

 

$ MenuData = '{

"Button ":[

{

"Type": "click ",

"Name": "Today's songs ",

"Key": "V1001_TODAY_MUSIC"

},

{

"Name": "menu ",

"Sub_button ":[

{

"Type": "view ",

"Name": "Search ",

"Url": "http://www.soso.com /"

},

{

"Type": "view ",

"Name": "video ",

"Url": "http://v.qq.com /"

},

{

"Type": "click ",

"Name": "like us ",

"Key": "V1001_GOOD"

}]

}]

}';

 

$ Ch = curl_init ();

 

Curl_setopt ($ ch, CURLOPT_URL, $ make_menu_url );

Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST ");

Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );

Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE );

Curl_setopt ($ ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1;. net clr 1.1.4322 )");

Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );

Curl_setopt ($ ch, CURLOPT_AUTOREFERER, 1 );

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ menuData );

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE );

 

$ Info = curl_exec ($ ch );

 

// If an error occurs during execution, a data error report is sent.

If (curl_errno ($ ch )){

Echo 'error'. curl_error ($ ch); // check whether the curl module is enabled in the php runtime environment.

}

 

Curl_close ($ ch );

Print_r ($ info); // view the data returned after the post is submitted to the server.

Custom menu acquisition

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<? Php

 

Define ("APPID", "Your appid ");

Define ("APPSECRET", "Your appsecret ");

 

$ Token_access_url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". APPID." & secret = ". APPSECRET;

$ Res = file_get_contents ($ token_access_url); // get the file content or the content of the network request

$ Result = json_decode ($ res, true); // receives a JSON string and converts it to a PHP variable.

$ Access_token = $ result ['Access _ token'];

 

$ Make_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/get? Access_token = ". $ access_token;

 

$ Menu_json = file_get_contents ($ make_menu_url );

 

Echo $ menu_json;

Delete a custom menu

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<? Php

 

Define ("APPID", "Your appid ");

Define ("APPSECRET", "Your appsecret ");

 

$ Token_access_url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". APPID." & secret = ". APPSECRET;

$ Res = file_get_contents ($ token_access_url); // get the file content or the content of the network request

$ Result = json_decode ($ res, true); // receives a JSON string and converts it to a PHP variable.

$ Access_token = $ result ['Access _ token'];

 

$ Make_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/delete? Access_token = ". $ access_token;

 

$ Menu_json = file_get_contents ($ make_menu_url );

 

Echo $ menu_json;

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.