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

作者

方倍工作室

修正记录:

2014.05.03 v1.0

方倍工作室 http://www.cnblogs.com/txw1958/

SDK 源码:

 /*
方倍工作室 http://www.cnblogs.com/txw1958/
CopyRight 2014 www.doucube.com All Rights Reserved
*/ class class_weixin_adv
{
var $appid = "";
var $appsecret = ""; //构造函数,获取Access Token
public function __construct($appid = NULL, $appsecret = NULL)
{
if($appid){
$this->appid = $appid;
}
if($appsecret){
$this->appsecret = $appsecret;
} //hardcode
$this->lasttime = 1395049256;
$this->access_token = "nRZvVpDU7LxcSi7GnG2LrUcmKbAECzRf0NyDBwKlng4nMPf88d34pkzdNcvhqm4clidLGAS18cN1RTSK60p49zIZY4aO13sF-eqsCs0xjlbad-lKVskk8T7gALQ5dIrgXbQQ_TAesSasjJ210vIqTQ"; if (time() > ($this->lasttime + 7200)){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
$res = $this->https_request($url);
$result = json_decode($res, true);
//save to Database or Memcache
$this->access_token = $result["access_token"];
$this->lasttime = time();
}
} //获取关注者列表
public function get_user_list($next_openid = NULL)
{
$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token."&next_openid=".$next_openid;
$res = $this->https_request($url);
return json_decode($res, true);
} //获取用户基本信息
public function get_user_info($openid)
{
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
$res = $this->https_request($url);
return json_decode($res, true);
} //创建菜单
public function create_menu($data)
{
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token;
$res = $this->https_request($url, $data);
return json_decode($res, true);
} //发送客服消息,已实现发送文本,其他类型可扩展
public function send_custom_message($touser, $type, $data)
{
$msg = array('touser' =>$touser);
switch($type)
{
case 'text':
$msg['msgtype'] = 'text';
$msg['text'] = array('content'=> urlencode($data));
break;
}
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
return $this->https_request($url, urldecode(json_encode($msg)));
} //生成参数二维码
public function create_qrcode($scene_type, $scene_id)
{
switch($scene_type)
{
case 'QR_LIMIT_SCENE': //永久
$data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';
break;
case 'QR_SCENE': //临时
$data = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';
break;
}
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->access_token;
$res = $this->https_request($url, $data);
$result = json_decode($res, true);
return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($result["ticket"]);
} //创建分组
public function create_group($name)
{
$data = '{"group": {"name": "'.$name.'"}}';
$url = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=".$this->access_token;
$res = $this->https_request($url, $data);
return json_decode($res, true);
} //移动用户分组
public function update_group($openid, $to_groupid)
{
$data = '{"openid":"'.$openid.'","to_groupid":'.$to_groupid.'}';
$url = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=".$this->access_token;
$res = $this->https_request($url, $data);
return json_decode($res, true);
} //上传多媒体文件
public function upload_media($type, $file)
{
$data = array("media" => "@".dirname(__FILE__).'\\'.$file);
$url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->access_token."&type=".$type;
$res = $this->https_request($url, $data);
return json_decode($res, true);
} //https请求(支持GET和POST)
protected function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}

调用方法:

初始化对象

$weixin = new class_weixin_adv("wx6222221b11111111", "3079cb22ad383ae7371d12aed1b2d0cc");

查看Access Token

var_dump($weixin->access_token);

创建二维码

var_dump($weixin->create_qrcode("QR_SCENE", "134324234"));

获取关注者列表

var_dump($weixin->get_user_list());

获取用户信息

$openid = "oLVPpjkttuZTbwDwN7vjHNlqsmPs";
var_dump($weixin->get_user_info($openid));

创建菜单

$data ='{"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":"服务热线"}]}]}';
var_dump($weixin->create_menu($data));

用户分组 方倍

var_dump($weixin->create_group("老师"));
var_dump($weixin->update_group($openid, "100"));

上传下载多媒体

var_dump($weixin->upload_media("image","pondbay.jpg"));

发送客服消息

var_dump($weixin->send_custom_message($openid, "text", "asdf"));

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

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

最新文章

  1. 删除txt文件每行第一(n)个空格前内容的方法
  2. IE10、IE11 ASP.Net 网站无法写入Cookie 问题
  3. 关于string,我今天科普的
  4. springmvc 接受特殊类型字段的处理方法
  5. ubuntu安装hexo博客
  6. Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive.
  7. shell学习之路:shell基础大全2
  8. Highcharts中文参考手册
  9. 权限框架 - shiro 自定义realm
  10. 游戏世界之Unity3D的基础认识
  11. struts1老古董配置
  12. jQuery控制TR的显示隐藏
  13. ArcMap自定义脚本工具制作
  14. [Angular 2] Inject Service
  15. Unity 扩展属性自定义绘制
  16. Apache指南:CGI动态页面
  17. Bzoj3473
  18. Cs Round#54 D Spanning Trees
  19. Json技术使用代码示例
  20. [Go] golang连接redis测试

热门文章

  1. 开发Google Material Design风格的WPF程序
  2. obd2 J1962M to DB9
  3. Visual Studio的工程结构解析
  4. dubbo知识点理解2
  5. Setup SS5 Socks Proxy
  6. Android如何检查对象的类型
  7. python接口自动化23-token参数关联登录(登录拉勾网)
  8. 《Android传感器高级编程》
  9. HTML:基本的标签
  10. DRP经验总结