<?php
/*
get_user_UserId 用户名
get_user_mobile 用户详细信息
send_custom_message 应用发送消息 未完成
http_request HTTP请求(支持HTTP/HTTPS,支持GET/POST)
*/
define('APPID', "填写企业ID");//企业微信企业ID
define('APPSECRET', "填写应用Secret"); //应用Secret
define('AGENTID', "填写应用AgentId"); //应用AgentId
class class_weixin
{
var $appid = APPID;
var $appsecret = APPSECRET;
var $agentid = AGENTID;
//获取Access Token
public function __construct($appid = NULL, $appsecret = NULL)
{
if($appid && $appsecret){
$this->appid = $appid;
$this->appsecret = $appsecret;
}
//Access Token有效期7200 必须存入memcache缓存,多次频繁请求微信会拦截
if (isset($_SERVER['HTTP_APPNAME'])){
$mem = memcache_init();
}else {
$mem = new Memcache;
$mem->connect('localhost', 11211) or die ("Could not connect");
}
$this->access_token = $mem->get($this->appid);
if (!isset($this->access_token) || empty($this->access_token)){
$url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.$this->appid.'&corpsecret='.$this->appsecret;
$res = $this->http_request($url);
$result = json_decode($res, true);
$this->access_token = $result["access_token"];
$mem->set($this->appid, $this->access_token, 0, 7200);
}
}
//获取企业微信用户ID
public function get_user_UserId( $code)
{
$url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token='.$this->access_token.'&code='.$code.'&state=web_login@sucaiku';
$res = $this->http_request($url);
return json_decode($res, true);
}
//获取企业微信用户基本信息
public function get_user_mobile( $UserId)
{
$url = 'https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token='.$this->access_token.'&userid='.$UserId;
$res = $this->http_request($url);
return json_decode($res, true);
}
public function get_user_openid( $UserId)
{
$url = 'https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token='.$this->access_token;
$uid='{"userid": "'.$UserId.'"}';
$res = $this->http_request($url,$uid);
return $res;
}
//发送消息
public function send_custom_message($touser,$type,$data)
{
$msg = array('touser' =>$touser);
$msg['msgtype'] = $type;
switch($type)
{
//固定消息类型为text
case 'text':
$msg[$type] = array('content'=>urlencode($data));
break;
default:
$msg['text'] = array('content'=>urlencode("不支持的消息类型 ".$type));
break;
}
$msg = array('agentid' =>$agentid);
$url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".$this->access_token;
return $this->http_request($url, urldecode(json_encode($msg)));
/*
$content[] = array("Title"=>"域名到期通知", "Description"=>"到期域名:wpf5400.com"."\n"."到期时间:2019-10-27 10:37:58");
$result = $weixin->send_custom_message("WangPengFei", "news", $content);
*/
}
//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, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}