1. 微信企业号SDK

 class class_wxqiye
{
var $corpid = CorpID;
var $corpsecret = CorpSecret;
var $agentid = AgentID; //构造函数,获取Access Token
public function __construct($corpid = NULL, $corpsecret = NULL, $agentid = NULL)
{
if($corpid && $corpsecret){
$this->corpid = $corpid;
$this->corpsecret = $corpsecret;
$this->agent = $agentid;
} $con = mysql_connect(MYSQLHOST.':'.MYSQLPORT, MYSQLUSER, MYSQLPASSWORD);
mysql_select_db(MYSQLDATABASE, $con);
$result = mysql_query("SELECT * FROM `qyh_mngmgroup` WHERE `type` = 'access_token' AND `corpid` = '$this->corpid'");
while($row = mysql_fetch_array($result))
{
$this->access_token = $row['value'];
$this->expires_time = $row['expire'];
break;
}
if (time() > ($this->expires_time + 3600)){
$url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$this->corpid."&corpsecret=".$this->corpsecret;
$res = $this->http_request($url);
$result = json_decode($res, true);
$this->access_token = $result["access_token"];
$this->expires_time = time();
mysql_query("UPDATE `qyh_mngmgroup` SET `expire` = '$this->expires_time', `value` = '$this->access_token' WHERE `type` = 'access_token' AND `corpid` = '$this->corpid';");
} } /*
测试接口,获取微信服务器IP地址
*/
// $result = $weixin->get_callback_ip();
// var_dump(count($result['ip_list']));
public function get_callback_ip()
{
$url = "https://qyapi.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$this->access_token;
$res = $this->http_request($url);
return json_decode($res, true);
} /*
1. 通讯录
*/
//1.1 获取部门列表
// $result = $weixin->get_department_list('2');
public function get_department_list($id = 1)
{
$url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=".$this->access_token."&id=".strval($id);
$res = $this->http_request($url);
return json_decode($res, true);
} //1.2 创建部门
public function create_department($data)
{
$url = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=".$this->access_token;
$res = $this->http_request($url, $this->array_to_json($data));
return json_decode($res, true);
} //1.3 更新部门
public function update_department($data)
{
$url = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=".$this->access_token;
$res = $this->http_request($url, $this->array_to_json($data));
return json_decode($res, true);
} //1.4 删除部门
public function delete_department($id)
{
$url = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=".$this->access_token."&id=".strval($id);
$res = $this->http_request($url);
return json_decode($res, true);
} //1.5 获取部门成员
public function get_department_userlist($departmentid, $fetchchild = "1", $status = "0")
{
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=".$this->access_token."&department_id=".strval($departmentid)."&fetch_child=".strval($fetchchild)."&status=".strval($status);
$res = $this->http_request($url);
return json_decode($res, true);
} /*
* 2. 素材文件
*/
// 2.3 上传图片、语音、视频、文件等类型永久素材
public function add_permanent_material($type, $file)
{
if (PHP_OS == "Linux"){ //Linux
$data = array("media" => "@".dirname(__FILE__).'/'.$file);
}else{ //WINNT
$data = array("media" => "@".dirname(__FILE__).'\\'.$file);
}
$url = "https://qyapi.weixin.qq.com/cgi-bin/material/add_material?agentid=".$this->agentid."&type=".$type."&access_token=".$this->access_token;
$res = $this->http_request($url, $data);
return json_decode($res, true);
} /*
* 3. 企业号应用
*/
// 3.1 获取应用概况列表
public function get_agent_list()
{
$url = "https://qyapi.weixin.qq.com/cgi-bin/agent/list?access_token=".$this->access_token;
$res = $this->http_request($url, $data);
return json_decode($res, true);
} // 3.2 获取企业号应用
public function get_agent_info()
{
$url = "https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=".$this->$agentid."&access_token=".$this->access_token;
$res = $this->http_request($url);
return json_decode($res, true);
} /*
* 4. 发送消息
*/
public function send_message($type, $data, $touser = '@all', $safe = '1')
{
$msg = array();
$msg['touser'] = $touser;
$msg['agentid'] = $this->agentid;
$msg['msgtype'] = $type;
switch($type)
{
case 'text':
$msg[$type] = array('content'=>$data);
break;
case 'news':
$data2 = array();
foreach ($data as &$item) {
$item2 = array();
foreach ($item as $k => $v) {
$item2[strtolower($k)] = $v;
}
$data2[] = $item2;
}
$msg[$type] = array('articles'=>$data2);
break;
case 'music':
case 'image':
case 'voice':
case 'video':
case 'file':
$msg['media_id'] = $data;
break;
default:
$msg['text'] = array('content'=>"不支持的消息类型 ".$type);
break;
}
$url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".$this->access_token;
return $this->http_request($url, $this->array_to_json($msg));
} /*
* 5. 自定义菜单
*/
// 5.1 创建菜单
public function create_menu($button)
{
$data = $this->array_to_json(array('button' => $button));
$url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token."&agentid=".$this->agentid;
$res = $this->http_request($url, $data);
return json_decode($res, true);
} // 5.2 查询菜单
public function get_menu()
{
$url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?access_token=".$this->access_token."&agentid=".$this->agentid;
$res = $this->http_request($url);
return json_decode($res, true);
} /*
* 6. 身份验证
*/
// 6.1 企业获取code
public function oauth2_authorize($redirect_url, $scope = "snsapi_base", $state = "")
{
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->corpid."&redirect_uri=".urlencode($redirect_url)."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
return $url;
} // 6.2 根据code获取成员信息
public function oauth2_get_userinfo($code)
{
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$this->access_token."&code=".$code;
$res = $this->http_request($url);
return json_decode($res, true);
} // 6.3 userid转换成openid接口
public function convert_openid($userid)
{
$data = $this->array_to_json(array('userid' => $userid, 'agentid'=>$this->agentid));
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token=".$this->access_token;
$res = $this->http_request($url, $data);
return json_decode($res, true);
} // 6.4 成员登录授权[企业内部开发者]
public function oauth2_login_page($redirect_url, $state = "", $usertype = "admin")
{
$url = "https://qy.weixin.qq.com/cgi-bin/loginpage?corp_id=".$this->corpid."&redirect_uri=".urlencode($redirect_url)."&state=".$state."&usertype=".$usertype;
return $url;
} // 6.5 获取企业号登录用户信息[成员登录授权]
public function oauth2_get_login_info($auth_code)
{
$data = $this->array_to_json(array('auth_code' => $auth_code));
$url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_login_info?access_token=".$this->access_token;
$res = $this->http_request($url, $data);
return json_decode($res, true);
} // 6.6 获取登录企业号官网的url[单点登录授权]
public function oauth2_get_login_url($login_ticket, $target = 'agent_setting')
{
$msg = array();
$msg['login_ticket'] = $login_ticket;
$msg['target'] = $target;
$msg['agentid'] = $this->agentid;
$url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_login_url?access_token=".$this->access_token;
return $this->http_request($url, $this->array_to_json($msg));
} //JS SDK
/*
* PART4 JS SDK 签名
* PHP仅用于获得签名包,需要配合js一起使用
*/
//生成长度16的随机字符串
public function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
} //获得JS API的ticket
private function getJsApiTicket()
{
/*
//1. 数据库形式,与Access Token共用同一张表,mysql在创建微信类时初始化
$result = mysql_query("SELECT * FROM `qyh_token` WHERE `type` = 'jsapi_ticket'");
$result = mysql_query("SELECT * FROM `qyh_token` WHERE `type` = 'jsapi_ticket' AND `corpid` = '$this->corpid'");
while($row = mysql_fetch_array($result))
{
$this->jsapi_ticket = $row['value'];
$this->jsapi_expire = $row['expire'];
break;
}
if (time() > ($this->jsapi_expire + 3600)){
$url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=".$this->access_token;
$res = $this->http_request($url);
$result = json_decode($res, true);
$this->jsapi_ticket = $result["ticket"];
$this->jsapi_expire = time();
mysql_query("UPDATE `qyh_mngmgroup` SET `expire` = '".$this->jsapi_expire."', `value` = '".$this->jsapi_ticket."' WHERE `type` = 'jsapi_ticket';");
mysql_query("UPDATE `qyh_mngmgroup` SET `expire` = '$this->jsapi_expire', `value` = '$this->access_token' WHERE `type` = 'access_token' AND `corpid` = '$this->corpid';");
}
return $this->jsapi_ticket;
*/ $result = mysql_query("SELECT * FROM `qyh_mngmgroup` WHERE `type` = 'jsapi_ticket'");
while($row = mysql_fetch_array($result))
{
$this->jsapi_ticket = $row['value'];
$this->jsapi_expire = $row['expire'];
break;
}
if ((!isset($this->jsapi_expire)) || (time() > ($this->jsapi_expire + 3600))){
$url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=".$this->access_token;
$res = $this->http_request($url);
$result = json_decode($res, true);
var_dump($result);
$this->jsapi_ticket = $result["ticket"];
$this->jsapi_expire = time();
mysql_query("UPDATE `qyh_mngmgroup` SET `expire` = '$this->jsapi_expire', `value` = '$this->jsapi_ticket' WHERE `type` = 'jsapi_ticket';");
} return $this->jsapi_ticket;
} //获得签名包
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->corpid,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
} //多级数组转JSON(兼容中文、数字、英文、布尔型)(使用递归出现编码错误 !!! )
protected function array_to_json($array)
{
foreach ($array as $k => &$v) {
if (is_array($v)){
foreach ($v as $k1 => &$v1) {
if (is_array($v1)){
foreach ($v1 as $k2 => &$v2) {
if (is_array($v2)){
foreach ($v2 as $k3 => &$v3) {
if (is_array($v3)){
foreach ($v3 as $k4 => &$v4) {
$v3[$k4] = (is_string($v4))?urlencode($v4):$v4;
}
}else{
$v2[$k3] = (is_string($v3))?urlencode($v3):$v3;
}
}
}else{
$v1[$k2] = (is_string($v2))?urlencode($v2):$v2;
}
}
}else{
$v[$k1] = (is_string($v1))?urlencode($v1):$v1;
}
}
// $this->array_to_json($v);
}else{
$array[$k] = (is_string($v))?urlencode($v):$v;
}
}
return urldecode(json_encode($array));
} //HTTP请求(支持HTTP/HTTPS,支持GET/POST)
protected function http_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, TRUE);
$output = curl_exec($curl);
curl_close($curl);
return $output;
} //日志记录
private function logger($log_content)
{
if(isset($_SERVER['HTTP_APPNAME'])){ //SAE
sae_set_display_errors(false);
sae_debug($log_content);
sae_set_display_errors(true);
}else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
$max_size = 500000;
$log_filename = "log.xml";
if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
file_put_contents($log_filename, date('Y-m-d H:i:s').$log_content."\r\n", FILE_APPEND);
}
}
}

2. 调用方法

/*
提供获取企业号应用、设置企业号应用、获取应用列表的接口。 首页
开始开发
✔主动调用
✔回调模式
认证接口
身份验证
✔OAuth验证接口
✔userid与openid互换接口
✔成员登录授权
✔单点登录授权
资源接口
管理企业号应用
✔获取企业号应用
✔设置企业号应用
✔获取应用概况列表
自定义菜单
✔创建应用菜单
✔删除菜单
✔获取菜单列表
管理通讯录
成员关注企业号
✔管理部门
✔管理成员
管理标签
异步任务接口
管理素材文件 ✔可上传
能力接口
✔发消息
✔接收消息与事件
微信JS-SDK接口 未做,做一个试一下
会话服务 没啥用
客服服务 没哈用
企业号微信支付 和服务号一样,只需要userid转openid,已有用例
摇一摇周边 暂无权限
SaaS套件接口
第三方应用授权
通讯录权限体系
管理后台单点登录
*/ /*
//微信企业号API接口大全
require_once('qiye.class.php');
$weixin = new class_wxqiye();
*/ // $weixin = new class_wxqiye(); //1. 通讯录
//1.1 获取部门列表
// $result = $weixin->get_department_list('2'); //1.2 创建部门
// $data = array('name' => "战略部",
// 'parentid' => "1",
// 'order' => "1",
// // 'id' => "10",
// );
// $result = $weixin->create_department($data); //1.5 获取部门成员
// $result = $weixin->get_department_userlist("7", "1", "0"); //2. 素材
//2.5 上传图片、语音、视频、文件素材
// $file = "fangbei.jpg";
// $result = $weixin->add_permanent_material("6", "image", $file);
// $file = "butterfly.mp4";
// $result = $weixin->add_permanent_material("6", "video", $file);
// $file = "cert.zip";
// $result = $weixin->add_permanent_material("6", "file", $file); //3. 企业号应用
// $result = $weixin->get_agent_list();
// $result = $weixin->get_agent_info('6'); //4. 发送消息
// 发送文本消息
// $result = $weixin->send_message('text', "没有查到到订单记录");
// 发送图文消息
// $data[] = array("Title"=>"多图文1标题", "Description"=>"多图文1描述", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
// $data[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
// $data[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
// $result = $weixin->send_message("news", $data); //5. 自定义菜单
// $button = array(array('name' => "点击跳转",
// 'sub_button' => array(
// array('type' => "click",
// 'name' => "点击推事件",
// 'key' => "COMPANY"
// ),
// array('type' => "view",
// 'name' => "跳转URL",
// 'url' => "http://www.baidu.com/"
// ),
// )
// ),
// array('name' => "扫码发图",
// 'sub_button' => array(
// array('type' => "scancode_waitmsg",
// 'name' => "扫码带提示",
// 'key' => "rselfmenu_2_1"
// ),
// array('type' => "scancode_push",
// 'name' => "扫码推事件",
// 'key' => "rselfmenu_2_2"
// ),
// array('type' => "pic_sysphoto",
// 'name' => "系统拍照发图",
// 'key' => "rselfmenu_2_3"
// ),
// array('type' => "pic_photo_or_album",
// 'name' => "拍照或相册发图",
// 'key' => "rselfmenu_2_3"
// ),
// array('type' => "pic_weixin",
// 'name' => "微信相册发图",
// 'key' => "rselfmenu_2_5"
// )
// )
// ),
// array('type' => "location_select",
// 'name' => "发送位置",
// 'key' => "rselfmenu_3_0",
// )
// );
// $result = $weixin->create_menu($button);
// // $result = $weixin->get_menu();
// var_dump($result); /*
//网页授权获得userid和openid
if (!isset($_GET["code"])){
$redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$jumpurl = $weixin->oauth2_authorize($redirect_url, "snsapi_base", "123");
Header("Location: $jumpurl");
}else{
$userinfo = $weixin->oauth2_get_userinfo($_GET["code"]);
// array(2) { ["DeviceId"]=> string(32) "be434bab16db86bd40995edf1194ef3b" ["UserId"]=> string(6) "taojin" }
// array(2) { ["DeviceId"]=> string(32) "e0a66223f897a826717fc134930afe85" ["OpenId"]=> string(28) "o_UE0s2L8mVlEAhlScmE0dfjJojg" }
if (!isset($userinfo["OpenId"])){
$openinfo = $weixin->convert_openid($userinfo["UserId"]);
// array(4) { ["errcode"]=> int(0) ["errmsg"]=> string(2) "ok" ["openid"]=> string(28) "oxzslwktZytLTM7zwlU3QODJNzQ0" ["appid"]=> string(18) "wxcc23554f93463d7c" }
$openid = $openinfo["openid"];
}else{
$openid = $userinfo["OpenId"];
}
} //获取企业号登录用户信息[成员登录授权]
if (!isset($_GET["auth_code"])){
$redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$jumpurl = $weixin->oauth2_login_page($redirect_url, "123", "member");
Header("Location: $jumpurl");
}else{
$userinfo = $weixin->oauth2_get_login_info($_GET["auth_code"]);
} */

微信企业号SDK的更多相关文章

  1. 微信C# SDK

    微信C# SDK # 模块功能 DLL 1 基础库 Senparc.Weixin.dll 2 微信公众号 / 微信支付 / JSSDK / 摇周边 / 等等 Senparc.Weixin.MP.dll ...

  2. 关于微信企业号操作api的sdk封装 (.net)

    做微信的第三方开发业半年多了 现在献上微信企业号的操作sdk(包括源码) 本人水平有限 望大家多提意见 下载地址献上:下载

  3. 微信企业号JS SDK

    微信企业号JS SDK <?php define('CorpID', "wx82e2c31215d9a5a7"); define('CorpSecret', "&q ...

  4. 微信企业号消息接口PHP SDK

    微信企业号消息接口PHP SDK及Demo <?php /* 方倍工作室 http://www.fangbei.org/ CopyRight 2015 All Rights Reserved * ...

  5. C#开发微信门户及应用(16)-微信企业号的配置和使用

    在本系列随笔的前面,主要就是介绍微信公众号的门户应用开发,最近把整个微信框架进行了扩展补充,增加了最新的企业号的API封装和开发,后续主要介绍如何利用C#进行微信企业号的开发工作,本篇作为微信企业号的 ...

  6. JAVA版开源微信管家—JeeWx捷微3.2版本发布,支持微信公众号,微信企业号,支付窗、小程序

    JeeWx捷微3.2微信企业号升级版本发布^_^ JeeWx捷微V3.2——多触点管理平台(支持微信公众号,微信企业号,支付窗.小程序)   JeeWx捷微V3.2.0版本引入了更多新特性,支持微信公 ...

  7. 微信开发SDK支持小程序 ,Jeewx-Api 1.3.1 版本发布

    JEEWX-API 是一款JAVA版的微信开发SDK,支持微信公众号.小程序.微信企业号.支付宝生活号SDK和微博SDK.你可以基于她 快速的傻瓜化的进行微信开发.支付窗开发和微博开发. 基于jeew ...

  8. 微信企业号 获取AccessToken

    目录 1. AccessToken介绍 2. 示例代码 1. AccessToken介绍 1.1 什么是AccessToken AccessToken即访问凭证,业务服务器每次主动调用企业号接口时需要 ...

  9. 微信企业号开发(1)WebAPI在回调模式中的URL验证

    微信回调模式的官方文档. 开发语言:C#(微信相关功能代码可以从官网下载) 首先,必须要明确几个参数,这几个参数在微信企业号中,每次调用都会使用到. 1.msg_signature:签名(已加密,加密 ...

随机推荐

  1. 2019-11-27-WPF-全屏透明窗口

    原文:2019-11-27-WPF-全屏透明窗口 title author date CreateTime categories WPF 全屏透明窗口 lindexi 2019-11-27 09:22 ...

  2. Qt播放音视频文件报错DirectShowPlayerService::doRender: Unresolved error code 0x80040266或DirectShowPlayerService::doSetUrlSource: Unresolved error code 0x80004005 ()

    使用QMediaPlayer和QVideoWidget QHBoxLayout *m_layout=newQHBoxLayout(this); QMediaPlayer *m_player = new ...

  3. mac下chm文件打开乱码解决

    菜单栏点击---显示---文本编码(选择编码格式)

  4. ztree选父不选子选子不选父

    var setting = { data: { simpleData: { enable: true } } ,check: { enable: true , chkStyle: 'checkbox' ...

  5. CTF必备技能丨Linux Pwn入门教程——ShellCode

    这是一套Linux Pwn入门教程系列,作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的一些题目和文章整理出一份相对完整的Linux Pwn教程. 课程回顾>> Linu ...

  6. apicloud触底加载的简单实现

    直接上干货 api.addEventListener({ name: 'scrolltobottom', extra: { threshold: 0 } }, function(ret, err) { ...

  7. 磁盘I/O 监控 iostat

    iostat -cdxm 2 5 dm-4 如果没有这个命令,需要安装sysstat 包. Usage: iostat [ options ] [ <interval> [ <cou ...

  8. Linux---文件压缩与解压缩命令

    压缩格式: zip.gz.bz2.tar .tar.gz.tar.bz2.tar.xz.xz.z 最常用的是.tar.gz格式和.tar.bz2格式 1.zip格式 命令  说明  zip 压缩文件名 ...

  9. 如何防止XSS攻击?

    来自: https://www.freebuf.com/articles/web/185654.html 前端安全 随着互联网的高速发展,信息安全问题已经成为企业最为关注的焦点之一,而前端又是引发企业 ...

  10. yield_from

    python3.3 新加的yield from 语句 1.yield from def my_chain(*args, **kwargs): for my_iterable in args: yiel ...