<?
namespace
app\wechat;
class
Wechat
{
const
AGENTID = 3;
const
CORPID =
"wx5d0183ad90c95d8b"
;
const
CORPSECRET =
"KTHAkkVl4mX4Jr_g89d3PXajYupsUcJFvGWQ1K6ZMagTPOh4kiNMfBLFoDr12DVh"
;
const
SCOPE =
"snsapi_base"
;
const
STATE =
"123"
;
public
function
wxauto(
$jumpurl
){
$corpid
= self::CORPID;
$scope
= self::SCOPE;
$state
= self::STATE;
$url
='https:
&redirect_uri='.urlencode(
$jumpurl
).'
&response_type=code&scope='.
$scope
.'&state='.
$state
.'#wechat_redirect';
header(
"Location:"
.
$url
);
exit
;
}
public
function
getPush(
$userid
,
$agentid
,
$message
){
$userinfo
=
$this
->getToken();
$access_token
=
$userinfo
['access_token'];
$sendmsg_url
=
"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="
.
$access_token
;
$data
="{\"touser\":\"
$userid
\",\"msgtype\":\"text\",\"agentid\":
$agentid
,\"text\":
{\
"content\":\"$message\"},\"safe\":0}"
;
$res
=
$this
->curlPost(
$sendmsg_url
,
$data
);
$errmsg
=json_decode(
$res
)->errmsg;
}
public
function
getToken(){
$corpid
= self::CORPID;
$corpsecret
= self::CORPSECRET;
$Url
=
"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="
.
$corpid
.
"&corpsecret="
.
$corpsecret
;
$res
=
$this
->curlPost(
$Url
);
$access_token
=json_decode(
$res
)->access_token;
$userinfo
=
array
();
$userinfo
['access_token']=
$access_token
;
return
$userinfo
;
}
public
function
curlPost(
$url
,
$data
=
""
){
$ch
= curl_init();
$opt
=
array
(
CURLOPT_URL =>
$url
,
CURLOPT_HEADER => 0,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS =>
$data
,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 20
);
$ssl
=
substr
(
$url
,0,8) ==
"https://"
? TRUE : FALSE;
if
(
$ssl
){
$opt
[CURLOPT_SSL_VERIFYHOST] = 2;
$opt
[CURLOPT_SSL_VERIFYPEER] = FALSE;
}
curl_setopt_array(
$ch
,
$opt
);
$data
= curl_exec(
$ch
);
curl_close(
$ch
);
return
$data
;
}
###########**********增加获取微信openid***********#########################
}
?>