class weixin{
//1.检测是否微信浏览器
function isWeiXinBrowser() {
$agent = $_SERVER ['HTTP_USER_AGENT'];
if (!strpos($agent, "icroMessenger")){
return false;
}
return true;
}
//1.获取微信code
function get_wx_code($redirect_url,$type='base',$parameter=''){
$redirect_url =urlencode($redirect_url);
//1基本信息,其它需要授权,更多高级信息
$scope = $type == 'base' ? 'snsapi_base' : 'snsapi_userinfo';
//重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
//进行重定向操作
$get_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.WX_APPID.'&redirect_uri='.$redirect_url.'&response_type=code&scope='.$scope.'&state='.$parameter;
header("Location:".$get_url);
die();
}
//2.获取微信access_token,如果是第一次获取,则需要传送_code,否则是从缓存中获取
function get_wx_access_token($code){
$get_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.WX_APPID.'&secret='.WX_APPSECRET.'&code='.$code.'&grant_type=authorization_code';
$response=$this->curl_get($get_url);
$res_arr=json_decode($response,true);
return $res_arr;
}
//2.1
function get_public_access_token()
{
$memcache = new Memcache;
global $CFG;
$memcache->connect($CFG['memcache_host'], $CFG['memcache_port']) or die('err_');
$weixin_access_token='';
$weixin_access_token_key="weixin_access_token";
if($memcache->get($weixin_access_token_key))
{
$weixin_access_token=$memcache->get($weixin_access_token_key);
}
else
{
$get_url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.WX_APPID.'&secret='.WX_APPSECRET;
$response=$this->curl_post($get_url);
$res_arr=json_decode($response,true);
if(isset($res_arr['access_token']) && isset($res_arr['expires_in']))
{
$weixin_access_token=$res_arr['access_token'];
$memcache->set($weixin_access_token_key,$res_arr['access_token'],false,$res_arr['expires_in']);
}
}
$memcache->close();
return $weixin_access_token;
}
//2.2.
function get_wx_userinfo($access_token,$openid)
{
$get_url='https://api.weixin.qq.com/cgi-bin/user/info?ACCESS_TOKEN='.$access_token.'&OPENID='.$openid;
$response=$this->curl_post($get_url);
$res_arr=json_decode($response,true);
return $res_arr;
}
/**
* 会员提现
* @param string $openid 用户openID
* @param string $trade_no 单号
* @param string $money 金额
* @param string $desc 描述
* @return string XML 结构的字符串
*/
function company_pay_member($openid,$trade_no,$money,$desc){
global $DT_IP;
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
$data = array(
'mch_appid' => WX_OPEN_APPID,
'mchid' => WX_MCHID,
'nonce_str' => random(32),
'partner_trade_no' => $trade_no, //商户订单号,需要唯一
'openid' => $openid,
'check_name'=> 'NO_CHECK', //NO_CHECK不强制校验真实姓名, FORCE_CHECK:强制
//'re_user_name' => 'jorsh', //收款人姓名
'amount' => $money * 100, //付款金额单位为分
'desc' => $desc,
'spbill_create_ip' => $DT_IP
);
$data['sign'] = $this->makeSign($data);
$xmldata = $this->array2xml($data);
$res = $this->curl_post($url,$xmldata,true);
$tag=array('status'=>0,'msg'=>'Can t connect the server');
if(!$res){return $tag;}
//file_put_contents('D:\EmpireServer\web\info.log',$res,FILE_APPEND);
$res_arr = $this->xml2array($res);
if($res_arr['return_code']=='SUCCESS' && $res_arr['result_code']=='SUCCESS')
{
$tag['status']=1;
$tag['res_arr']=$res_arr;
}
else
{
if(strval($res_arr['return_code']) == 'FAIL'){
$tag['msg']=strval($res_arr['return_msg']);
return $tag;
}
if(strval($res_arr['result_code']) == 'FAIL'){
$tag['msg']=strval($res_arr['err_code_des']);
return $tag;
}
}
return $tag;
}
//将一个数组转换为 XML 结构的字符串
function array2xml($arr, $level = 1) {
$s = $level == 1 ? "<xml>" : '';
foreach($arr as $tagname => $value) {
if (is_numeric($tagname)) {
$tagname = $value['TagName'];
unset($value['TagName']);
}
if(!is_array($value)) {
$s .= "<{$tagname}>".$value."</{$tagname}>";
} else {
$s .= "<{$tagname}>" . $this->array2xml($value, $level + 1)."</{$tagname}>";
}
}
$s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
return $level == 1 ? $s."</xml>" : $s;
}
//将xml转为array
function xml2array($xml){
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $result;
}
//生成签名
function makeSign($data){
$data=array_filter($data);
ksort($data);
$str=urldecode(http_build_query($data));
$sign = strtoupper(md5($str."&key=".WX_APPKEY));
return $sign;
}
//crul请求
function curl_post($url,$xmldata='',$cert=false){
$curl = curl_init();
curl_setopt($curl,CURLOPT_TIMEOUT,30);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($curl,CURLOPT_POST,true);
if($cert)
{
curl_setopt($curl,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($curl,CURLOPT_SSLCERT,WX_API_CERT);
curl_setopt($curl,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($curl,CURLOPT_SSLKEY,WX_API_KEY);
}
if($xmldata)
{
curl_setopt($curl,CURLOPT_POSTFIELDS,$xmldata);
}
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
//crul请求
function curl_get($url){
$curl = curl_init();
curl_setopt($curl,CURLOPT_TIMEOUT,30);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($curl,CURLOPT_POST,false);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
}
获取微信用户信息和提现的用户类
猜你喜欢
转载自blog.csdn.net/zhangchb/article/details/81381368
今日推荐
周排行