You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.0 KiB

  1. <?php
  2. namespace App\Common;
  3. /**
  4. * 微信自定义菜单
  5. */
  6. class WechatMenu
  7. {
  8. //高级功能->开发者模式->获取
  9. private $app_id;
  10. private $app_secret;
  11. private $access_token;
  12. private $expires_in;
  13. public function __construct($app_id, $app_secret)
  14. {
  15. $this->app_id = $app_id;
  16. $this->app_secret = $app_secret;
  17. $token = $this->get_access_token();
  18. $this->access_token = $token['access_token'];
  19. $this->expires_in = $token['expires_in'];
  20. }
  21. /**
  22. * 获取授权access_token
  23. *
  24. */
  25. public function get_access_token()
  26. {
  27. $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->app_id}&secret={$this->app_secret}";
  28. $token_data = $this->http($token_url);
  29. return json_decode($token_data, true);
  30. }
  31. //获取关注者列表
  32. public function get_user_list($next_openid = NULL)
  33. {
  34. $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token."&next_openid=".$next_openid;
  35. $res = $this->http($url);
  36. return json_decode($res, true);
  37. }
  38. /**
  39. * 自定义菜单创建
  40. *
  41. * @param string $jsonmenu
  42. */
  43. public function create_menu($jsonmenu)
  44. {
  45. $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token;
  46. return $this->http($url, $jsonmenu);
  47. }
  48. // cURL函数简单封装
  49. public function http($url, $data = null)
  50. {
  51. $curl = curl_init();
  52. curl_setopt($curl, CURLOPT_URL, $url);
  53. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  54. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  55. if (!empty($data))
  56. {
  57. curl_setopt($curl, CURLOPT_POST, 1);
  58. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  59. }
  60. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  61. $output = curl_exec($curl);
  62. curl_close($curl);
  63. return $output;
  64. }
  65. }