217 lines
6.0 KiB
PHP
217 lines
6.0 KiB
PHP
<?PHP
|
||
/**
|
||
* 微信接口公共函数类
|
||
*/
|
||
|
||
|
||
/**
|
||
* 普通模式 获取access_token
|
||
*/
|
||
function get_access_token($appid,$secret){
|
||
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
|
||
$data = https_request($url);
|
||
if($data){
|
||
$result = json_decode($data, true);// 默认false,为Object,若是True,为Array
|
||
return $result['access_token'];
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
//获取微信的access_token
|
||
/*
|
||
* 返回:
|
||
* $return = array(
|
||
* 'access_token'=>'token值',
|
||
* 'expires_in'=>'有效时间(秒)',
|
||
* 't'=>'有效时间'
|
||
* );
|
||
*/
|
||
function get_access_token2(){
|
||
|
||
$f = new zdeFile();
|
||
$info = $f->readAll(WEBPATH_DIR."cache/access_token.php");
|
||
if($info){//如果有读到数据,而且数据还有效,则直接返回
|
||
$info = json_decode($info,true);
|
||
if(isset($info['t']) && $info['t']>time()-60){
|
||
return $info;
|
||
}
|
||
}
|
||
//获取token url
|
||
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$GLOBALS["appid"]."&secret=".$GLOBALS["secret"];
|
||
//发送http请求得到json流
|
||
$res = https_request($url);
|
||
$req_data = json_decode($res, true);
|
||
if(!empty($req_data['access_token'])){
|
||
$req_data['t'] = time() + $req_data['expires_in'];
|
||
//保存数据
|
||
$f->write(WEBPATH_DIR."cache/access_token.php",json_encode($req_data),false);
|
||
return $req_data;
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 授权模式 获取的token
|
||
*/
|
||
function get_sns_token($appid,$secret,$code){
|
||
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
|
||
//sys_log_write_content( $url ,"error_log","url_error");
|
||
$result = https_request($url);
|
||
if($result){
|
||
//sys_log_write_content( 'OPENID成功'.$result.__FILE__.__LINE__ ,"error_log","openid_ok");
|
||
$json_obj = json_decode($result,true);
|
||
return $json_obj;
|
||
}else{
|
||
sys_log_write_content( 'OPENID出错'.$result.__FILE__.__LINE__ ,"error_log","openid_error");
|
||
return false;
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 获取已关注公众号的全部用户的openid
|
||
*/
|
||
function get_user_sub($token){
|
||
$data = https_request("https://api.weixin.qq.com/cgi-bin/user/get?access_token=$token");
|
||
if($data){
|
||
$result = json_decode($data, true);// 默认false,为Object,若是True,为Array
|
||
foreach( $result['data']['openid'] as $k=>$v) {
|
||
$user_sub[] = $v;
|
||
}
|
||
return $user_sub;
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取已关注公众号的用户信息
|
||
*/
|
||
function get_sub_user($token,$openid){
|
||
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token."&openid=".$openid;
|
||
$data = https_request($url);
|
||
if($data){
|
||
$result = json_decode($data, true);// 默认false,为Object,若是True,为Array
|
||
return $result;
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取授权登录的用户信息
|
||
*/
|
||
function get_sns_user($token,$openid){
|
||
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid;
|
||
$data = https_request($url);
|
||
if($data){
|
||
sys_log_write_content( 'get_sns_user成功'.$data.__FILE__.__LINE__ ,"error_log","get_sns_user_ok");
|
||
$result = json_decode($data, true);// 默认false,为Object,若是True,为Array
|
||
return $result;
|
||
}else{
|
||
sys_log_write_content( 'get_sns_user出错'.$data.__FILE__.__LINE__ ,"error_log","get_sns_user_error");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 是否已关注了公众号
|
||
*/
|
||
function is_att_wx($appid,$secret,$openid){
|
||
$access_token = get_access_token($appid,$secret);
|
||
if(!$access_token){
|
||
return false;
|
||
}
|
||
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$openid;
|
||
$result = https_request($url);
|
||
$data = json_decode($result,true);
|
||
if($data){
|
||
return $data;
|
||
//$data["subscribe"] = $data['subscribe']==0?0:1;
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取米大师 sig 签名
|
||
*/
|
||
function get_offer_sig($obj,$secret,$key,$url){
|
||
foreach ($obj as $k => $v){
|
||
$arr[$k] = $v;
|
||
}
|
||
ksort($arr);
|
||
$string = format_biz_query_para_map($arr, false);
|
||
$string_sign = $string.'&org_loc='.$url.'&method=POST&secret='.$secret;
|
||
return $sig = hash_hmac('sha256',$string_sign,$key);//HMAC-SHA256签名
|
||
}
|
||
|
||
/**
|
||
* 获取米大师 mp_sig 签名
|
||
*/
|
||
function get_offer_mp_sig($obj,$session_key,$key,$url){
|
||
foreach ($obj as $k => $v){
|
||
$arr[$k] = $v;
|
||
}
|
||
ksort($arr);
|
||
$string = format_biz_query_para_map($arr, false);
|
||
$string_sign = $string.'&org_loc='.$url.'&method=POST&session_key='.$session_key;
|
||
return $sig = hash_hmac('sha256',$string_sign,$key);//HMAC-SHA256签名
|
||
}
|
||
|
||
/**
|
||
* 作用:格式化参数,签名过程需要使用
|
||
*/
|
||
function format_biz_query_para_map($paraMap, $urlencode){
|
||
$buff = "";
|
||
ksort($paraMap);
|
||
foreach ($paraMap as $k => $v){
|
||
if($urlencode){
|
||
$v = urlencode($v);
|
||
}
|
||
//$buff .= strtolower($k) . "=" . $v . "&";
|
||
$buff .= $k . "=" . $v . "&";
|
||
}
|
||
$reqPar;
|
||
if (strlen($buff) > 0) {
|
||
$reqPar = substr($buff, 0, strlen($buff)-1);
|
||
}
|
||
return $reqPar;
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 检验数据的真实性,并且获取解密后的明文.
|
||
* @param $encryptedData string 加密的用户数据
|
||
* @param $iv string 与用户数据一同返回的初始向量
|
||
* @param $sessionKey string 用户登陆后从微信返回的session_key
|
||
*
|
||
* @return 解密后的数据
|
||
*/
|
||
function wx_decryptData( $encryptedData, $iv,$sessionKey){
|
||
if (strlen($sessionKey) != 24) {
|
||
return false;
|
||
}
|
||
$aesKey=base64_decode($sessionKey);
|
||
|
||
if (strlen($iv) != 24) {
|
||
return false;
|
||
}
|
||
$aesIV=base64_decode($iv);
|
||
$aesCipher=base64_decode($encryptedData);
|
||
$result= openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
|
||
$dataObj=json_decode($result,true);
|
||
if( empty($dataObj) ){
|
||
return false;
|
||
}
|
||
if( $dataObj['watermark']['appid'] != $GLOBALS["appid"] ) {
|
||
return false;
|
||
}
|
||
return $dataObj;
|
||
} |