function weixin_qr_action(){
//$wapUrl = $this->config['sy_weburl'].'/index.php?m=media&c=weixin_change' ;
include(APP_PATH.'api/weixin/lib/WxPay.Config.php');
//注意url要轉碼urlencode
$wapUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.WxPayConfig::APPID.'&redirect_uri='.urlencode($this->config['sy_weburl'].'/index.php?m=media&c=weixin_change').'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
include_once LIB_PATH."yunqrcode.class.php";
YunQrcode::generatePng2($wapUrl,4);
}
function weixin_change_action(){
include(APP_PATH.'api/weixin/weixin.php');
$code = $_GET['code'] ;
$weixin_obj = new weixin();
$res = $weixin_obj->get_user_info($code);
$data = array();
$data['openid'] = $res['openid'];
$data['amount'] = 150;
$data['re_user_name'] = $res['nickname'];
$data['partner_trade_no'] = 'HAG00888610';
$re = $weixin_obj->pay_for_customer($data);
if($re['result_code'] == 'SUCCESS'){
echo '<script>alert("取現成功,已經成功打入你的微信零錢")</script>';
}else{
echo '<script>alert("'.$re['err_code_des'].'")</script>';
}
}
php
/** web
* 獲取用戶信息
*
@author zcb
*
@copyright 2016-03-18
**/
function get_user_info($code){
//獲取access_token
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.WxPayConfig::APPID.'&secret='.WxPayConfig::APPSECRET.'&code='.$code.'&grant_type=authorization_code';
$res = $this->https_request( $token_url ) ;
$token = json_decode( $res);
if(isset($token->errcode)){
return array('msg'=>'獲取access_token失敗','data'=>$token->errcode);
}
//獲取用戶信息
$access_token_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token->access_token.'&openid='.$token->openid;
$user_info = $this->https_request($access_token_url);
return json_decode($user_info,true);
}
/**
* 獲取公衆號全部的人的openid
*
@author zcb
*
@copyright 2016-03-18
**/
function get_user_openid(){
$res = $this->get_user_access_token();
if( $res ){
$jsoninfo = json_decode($res, true);
$access_token = $jsoninfo["access_token"];
$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$access_token";
$result = $this->https_request($url);
$jsoninfo = json_decode($result,true); // 默認false,爲Object,如果True,爲Array
$data = $jsoninfo->data;
$arr = $data->openid; // 得到全部用戶的Openid
}
return $jsoninfo ;
}
/**
* 獲取用戶的access_token
*
@author zcb
* @copyright 2016-03-18
**/
function get_user_access_token(){
$appsecret = WxPayConfig::APPSECRET ;
$appid = WxPayConfig::APPID ;
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$res = $this->https_request($url) ;
return $res ;
}
function https_request($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
curl_close($curl);
return $data;
}
/**
* 企業打款
* @author zcb
* @copyright 2016-03-18
*
**/
function pay_for_customer($data){
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
$data['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];
$data['check_name'] = 'NO_CHECK';
$data['desc'] = '客戶提現';
$data['mch_appid'] = WxPayConfig::APPID ;
$data['mchid'] = WxPayConfig::MCHID ;
$data['nonce_str'] = $this->create_rand_string();
$stringA = $this->ToUrlParams($data,false);
$stringSingTemp = $stringA.'&key='.WxPayConfig::KEY ;
$sign = strtoupper(md5($stringSingTemp));
$data['sign'] = $sign ;
$postXml = $this->ToXml($data) ;
$res = $this->postXmlCurl($postXml, $url,true);
$data = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
file_put_contents('weixin.txt',var_export($res,true));
return $data;
}
/**
* 隨機字符串
* @author zcb
* @copyright 2016-03-18
**/
function create_rand_string( $length = 32 ){
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$str = '' ;
for( $i = 0 ; $i < $length ; $i++){
$str .=substr($chars, mt_rand(0, strlen($chars)-1),1);
}
return $str ;
}
/**
* 格式化參數格式化成url參數
*/
public function ToUrlParams($paramap,$urlencode)
{
$buff = "";
ksort($paramap);
foreach ($paramap as $k => $v)
{
if($k != "sign" && $v != null && $v !='null'){
if($urlencode){
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
}
$reqpar = '' ;
if(strlen($buff) > 0 ){
$reqpar = substr($buff, 0,strlen($buff)-1);
}
return $reqpar;
}
/**
* 把數據組裝成xml
* @author zcb
* @copyright
**/
public function ToXml( $data )
{
$xml = "<xml>";
foreach ( $data as $key => $val )
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
/**
* 以post方式提交xml到對應的接口url
*
* @param string $xml 須要post的xml數據
* @param string $url url
* @param bool $useCert 是否須要證書,默認不須要
* @param int $second url執行超時時間,默認30s
* @throws WxPayException
*/
function postXmlCurl($xml, $url, $useCert = false, $second = 30){
$ch = curl_init();
//設置超時
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//嚴格校驗
//要求結果爲字符串且輸出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//若是有配置代理這裏就設置代理
// if(WxPayConfig::CURL_PROXY_HOST != "0.0.0.0"
// && WxPayConfig::CURL_PROXY_PORT != 0){
// curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST);
// curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT);
// }
//設置header
curl_setopt($ch, CURLOPT_HEADER, false);
if($useCert == true){
//設置證書
//使用證書:cert 與 key 分別屬於兩個.pem文件
// curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_CAINFO, PATH.'/cert'.DIRECTORY_SEPARATOR.'rootca.pem');
curl_setopt($ch,CURLOPT_SSLCERT, PATH.'/cert'.DIRECTORY_SEPARATOR.'apiclient_cert.pem');
// curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, PATH.'/cert'.DIRECTORY_SEPARATOR.'apiclient_key.pem');
}
//post提交方式
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//運行curl
$data = curl_exec($ch);
//返回結果
if($data){
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
throw new WxPayException("curl出錯,錯誤碼:$error");
}
}
} json
//發放現金紅包 api
function cash_money($data){
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
$money = 150 ;
$sender = '好招工';
$data['wxappid'] = WxPayConfig::APPID;
$data['mch_id'] = WxPayConfig::MCHID;
$data['mch_billno'] = WxPayConfig::MCHID.date('YmdHis').rand(1000,9999);
$data['client_ip'] = $_SERVER['REMOTE_ADDR'];
$data['total_amount'] = $money;
$data['min_value'] = $money;
$data['max_value'] = $money;
$data['total_num'] = 1;
$data['nick_name'] = $sender;
$data['send_name'] = $sender;
$data['wishing'] = '感謝關注好找工';
$data['act_name'] = $sender.'紅包';
$data['remark'] = $sender.'紅包';
//$data['re_openid'] ='';
$data['nonce_str'] = $this->create_rand_string();
$stringA = $this->ToUrlParams($data,false);
$stringSingTemp = $stringA.'&key='.WxPayConfig::KEY ;
$sign = strtoupper(md5($stringSingTemp));
$data['sign'] = $sign ;
$postXml = $this->ToXml($data) ;
$res = $this->postXmlCurl($postXml, $url,true);
$data = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
file_put_contents('weixin.txt',var_export($res,true));
return $data;
} 微信