獲取微信使用者資訊和提現的使用者類
阿新 • • 發佈:2018-12-19
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; } }