±¾ÎÄʵÀýΪ´o¼ÒÖÏiÁËPHP΢ÐŹ«ÖÚºÅ×Ô¶¯¢Ëͺi°uAPI´uÂe£¬*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼¡£¾ßÌaÈçÏ£º
Ìu³oºËÐĽӿڴuÂeÖÁÓÚÊý¾Ý×Ô¼ºÌiд£¬½Ó¿Ú²aÊԣϣË
wechat_packet.php
<!--?php
/**
* *¢Ëͺi°u½Ó¿Ú
* Created by PhpStorm.
* User: ADKi
* Date: 2016/4/25 0025
* Time: 15:25
*/
class wechat_packet{
private $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';//ÇeÇoURL
private $mch_id;//ÉÌ»§ºÅ
private $weixin_appid;//¹«ÖÚÕ˺Åappid
private $send_name;//ÉÌ»§Ãu³Æ
private $total_num = 1;//*¢Ëͺi°u×ÜÈËÊý
private $wishing;//ºi°u×£¸£Ói
private $client_ip;//µ÷ÓýӿڵĻuÆ÷IpµØÖ*
private $act_name;//»i¶¯Ãu³Æ
private $remark;//±¸×¢ÐÅÏ¢
private $nonce_str;//Ëae»u×Ö*u´®£¬²»³¤ÓÚ32λ
private $api_password;
private $arraytoxml;//Êý×eתxml
/**
* ¹«Ô¿
*/
private $public_key = "/api/wechat/cert/apiclient_cert.pem";
/**
* ˽Կ
*/
private $private_key = '/api/wechat/cert/apiclient_key.pem';
/**
* caÖ¤Êe
*/
private $rootca = 'api/wechat/cert/rootca.pem';
public function __construct()
{
//³oʼ»¯ºi°uÉeÖÃÐÅÏ¢
$this--->weixin_appid = C('wap_weixin_appid');
$this->mch_id = C('wechat_mch_id');
$this->send_name = C('wechat_send_name');
$this->wishing = C('wechat_wishing');
$this->act_name = C('wechat_act_name');
$this->client_ip = $_SERVER['SERVER_ADDR'];
$this->remark = C('wechat_remark');
$this->nonce_str = $this->create_nonce_str(32);
$this->api_password = C('wechat_api_password');
$inc_file = BASE_PATH.DS.'api'.DS.'wechat'.DS.'arraytoxml.php';
if(is_file($inc_file)) {
require($inc_file);
}
$this->arraytoxml = new ArrayToXML();
}
public function send_post($mch_billno,$re_openid,$total_amount){
$sign = $this->create_sign($mch_billno,$re_openid,$total_amount);
$send_array = array(
'nonce_str' => $this->nonce_str,
'mch_billno' => $mch_billno,
'mch_id' => $this->mch_id,
'wxappid' => $this->weixin_appid,
'send_name' => $this->send_name,
're_openid' => $re_openid,
'total_amount' => $total_amount,
'total_num' => $this->total_num,
'wishing' => $this->wishing,
'client_ip' => $this->client_ip,
'act_name' => $this->act_name,
'remark' => $this->remark,
'sign' => $sign,
);
$send_xml = $this->arraytoxml->toXml($send_array,'');
$data = $this->curl_post_ssl($this->url, $send_xml);
$data = $this->xmlToArray($data);
file_put_contents('adki',var_export($data,true),FILE_APPEND);
}
/*
ÇeÈ*±£ÄuµÄlibcurl°ae±¾ÊÇ*ñÖ§³ÖË«ÏoÈÏÖ¤£¬°ae±¾¸ßÓÚ7.20.1
*/
private function curl_post_ssl($url, $vars, $second=30,$aHeader=array()){
$ch = curl_init();
//³¬Ê±Ê±¼a
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//ÕaÀiÉeÖôuÀi£¬Èç¹uÓеĻ°
//curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//ÒÔÏÂÁ½ÖÖ*½Ê½ÐeÑ¡ÔñÒ»ÖÖ
//µÚÒ»ÖÖ*½*¨£¬cert Óe key *Ö±ðÊoÓÚÁ½¸o.pemÎļþ
//ĬÈϸñʽΪPEM£¬¿ÉÒÔ×¢ÊÍ
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT,getcwd().$this->public_key);
//ĬÈϸñʽΪPEM£¬¿ÉÒÔ×¢ÊÍ
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,getcwd().$this->private_key);
//caÖ¤Êe
curl_setopt($ch,CURLOPT_CAINFO,$this->rootca);
//µÚ¶þÖÖ*½Ê½£¬Á½¸oÎļþºÏ³ÉÒ»¸o.pemÎļþ
//curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n\n\n\n";
curl_close($ch);
return false;
}
}
//Éu³ÉÇ(C)Ãu
private function create_sign($mch_billno,$re_openid,$total_amount){
$string_array = array(
'act_name' => $this->act_name,
'client_ip' => $this->client_ip,
'mch_billno' => $mch_billno,
'mch_id' => $this->mch_id,
'nonce_str' => $this->nonce_str,
're_openid' => $re_openid,
'remark' => $this->remark,
'send_name' => $this->send_name,
'total_amount' => $total_amount,
'total_num' => $this->total_num,
'wishing' => $this->wishing,
'wxappid' => $this->weixin_appid,
);
foreach ($string_array as $key => $value){
if(!empty($value)){
$stringA .= "$key=$value";
if($key != 'wxappid'){
$stringA .= '&';
}
}
}
//ת³ÉUTF-8
$stringA = $this->gbkToUtf8($stringA);
$stringSignTemp="$stringA&key;=$this->api_password";
$sign = MD5($stringSignTemp);
$sign = strtoupper($sign);
return $sign;
}
//Éu³ÉËae»u×Ö*u´®
private function create_nonce_str($length){
$str = null;
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol)-1;
for($i=0;$i<$length;$i++){
$str.=$strPol[rand(0,$max)];//rand($min,$max)Éu³É½eÓÚminºÍmaxÁ½¸oÊýÖ®¼aµÄÒ»¸oËae»uÕuÊý
}
return $str;
}
/**
*×Ô¶¯ÅжϰÑgbk»ogb2312±aÂeµÄ×Ö*u´®×ªÎªutf8
*ÄÜ×Ô¶¯ÅжÏÊaÈe×Ö*u´®µÄ±aÂeÀa£¬Èç¹u±¾ÉiÊÇutf-8¾Í²»ÓÃת»»£¬*ñÔo¾Íת»»Îªutf-8µÄ×Ö*u´®
*Ö§³ÖµÄ×Ö*u±aÂeÀaÐÍÊÇ£ºutf-8,gbk,gb2312
*@$str:string ×Ö*u´®
*/
private function gbkToUtf8($str){
$charset = mb_detect_encoding($str,array('ASCII','UTF-8','GBK','GB2312'));
$charset = strtolower($charset);
if("utf-8" != $charset){
$str = iconv('UTF-8',$charset,$str);
}
return $str;
}
private function xmlToArray($postStr){
$msg = array();
$msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
return $msg;
}
}
Êý×eתxml:arraytoxml.php
<!--?php
/**
* Created by PhpStorm.
* User: ADKi
* Date: 2016/4/26 0026
* Time: 12:19
*/
class ArrayToXML
{
public function toXml($data){
$xml = '<xml-->';
foreach ($data as $key => $value){
if (is_numeric($value)){
$xml .= "<".$key.">".$value."<!--".$key."-->";
}else{
$xml .= "<".$key."><!--[CDATA[".$value."]]--><!--".$key."-->";
}
}
$xml .= '';
return $xml;
}
}
ÖØдÁËÒ»ÏÂPHPÏÂÃaeµÄ΢ÐÅAPI½Ó¿Ú£¬
΢Ðźi°uÖ§³Ö£¬JSAPIµÄ¶¯Ì¬²ÎÊý½Ó¿ÚÖ§³Ö
http://git.oschina.net/youkuiyuan/yky_test/blob/master/class/wxapi.class.php
΢ÐÅAPIÀa - Ôo¼Óºi°uÖ§³Ö
<?php
/********************************************************
* @author Kyler You <QQ:2444756311>
* @link http://mp.weixin.qq.com/wiki/home/index.html
* @version 2.0.1
* @uses $wxApi = new WxApi();
* @package ΢ÐÅAPI½Ó¿Ú ½Ðø»a¼ÌÐø½øÐиuÐÂ
********************************************************/
class WxApi {
const appId = "";
const appSecret = "";
const mchid = ""; //ÉÌ»§ºÅ
const privatekey = ""; //˽Կ
public $parameters = array();
public function __construct(){
}
/****************************************************
* ΢ÐÅÌa½»API*½*¨£¬*µ»Ø΢ÐÅÖ¸¶¨JSON
****************************************************/
public function wxHttpsRequest($url,$data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
/****************************************************
* ΢ÐÅ´øÖ¤ÊeÌa½»Êý¾Ý - ΢Ðźi°uʹÓÃ
****************************************************/
public function wxHttpsRequestPem($url, $vars, $second=30,$aHeader=array()){
$ch = curl_init();
//³¬Ê±Ê±¼a
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//ÕaÀiÉeÖôuÀi£¬Èç¹uÓеĻ°
//curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//ÒÔÏÂÁ½ÖÖ*½Ê½ÐeÑ¡ÔñÒ»ÖÖ
//µÚÒ»ÖÖ*½*¨£¬cert Óe key *Ö±ðÊoÓÚÁ½¸o.pemÎļþ
//ĬÈϸñʽΪPEM£¬¿ÉÒÔ×¢ÊÍ
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/apiclient_cert.pem');
//ĬÈϸñʽΪPEM£¬¿ÉÒÔ×¢ÊÍ
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/apiclient_key.pem');
curl_setopt($ch,CURLOPT_CAINFO,'PEM');
curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/rootca.pem');
//µÚ¶þÖÖ*½Ê½£¬Á½¸oÎļþºÏ³ÉÒ»¸o.pemÎļþ
//curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
}
/****************************************************
* ΢ÐÅ»ñÈ¡AccessToken *µ»ØÖ¸¶¨Î¢ÐŹ«ÖںŵÄatÐÅÏ¢
****************************************************/
public function wxAccessToken($appId = NULL , $appSecret = NULL){
$appId = is_null($appId) ? self::appId : $appId;
$appSecret = is_null($appSecret) ? self::appSecret : $appSecret;
//echo $appId,$appSecret;
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid;=".$appId."&secret;=".$appSecret;
$result = $this->wxHttpsRequest($url);
//print_r($result);
$jsoninfo = json_decode($result, true);
$access_token = $jsoninfo["access_token"];
return $access_token;
}
/****************************************************
* ΢ÐÅͨ¹ýOPENID»ñÈ¡Óû§ÐÅÏ¢£¬*µ»ØÊý×e
****************************************************/
public function wxGetUser($openId){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$wxAccessToken."&openid;=".$openId."〈=zh_CN";
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
}
/****************************************************
* ΢ÐÅͨ¹ýÖ¸¶¨Ä£°aÐÅÏ¢*¢Ë͸øÖ¸¶¨Óû§£¬*¢ËÍÍe³Éºo*µ»ØÖ¸¶¨JSONÊý¾Ý
****************************************************/
public function wxSendTemplate($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
return $result;
}
/****************************************************
* *¢ËÍ×Ô¶¨ÒaµÄÄ£°aÏuÏ¢
****************************************************/
public function wxSetSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE'){
$template = array(
'touser' => $touser,
'template_id' => $template_id,
'url' => $url,
'topcolor' => $topcolor,
'data' => $data
);
$jsonData = json_encode($template);
$result = $this->wxSendTemplate($jsonData);
return $result;
}
/****************************************************
* ΢ÐÅÉeÖÃOAUTHÌøתURL£¬*µ»Ø×Ö*u´®ÐÅÏ¢ - SCOPE = snsapi_base //Ñe֤ʱ²»*µ»ØÈ*ÈÏÒ³Ãae£¬Ö»ÄÜ»ñÈ¡OPENID
****************************************************/
public function wxOauthBase($redirectUrl,$state = "",$appId = NULL){
$appId = is_null($appId) ? self::appId : $appId;
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect;_uri=".$redirectUrl."&response;_type=code&scope;=snsapi_base&state;=".$state."#wechat_redirect";
return $url;
}
/****************************************************
* ΢ÐÅÉeÖÃOAUTHÌøתURL£¬*µ»Ø×Ö*u´®ÐÅÏ¢ - SCOPE = snsapi_userinfo //»ñÈ¡Óû§ÍeÕuÐÅÏ¢
****************************************************/
public function wxOauthUserinfo($redirectUrl,$state = "",$appId = NULL){
$appId = is_null($appId) ? self::appId : $appId;
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect;_uri=".$redirectUrl."&response;_type=code&scope;=snsapi_userinfo&state;=".$state."#wechat_redirect";
return $url;
}
/****************************************************
* ΢ÐÅOAUTHÌøתָ¶¨URL
****************************************************/
public function wxHeader($url){
header("location:".$url);
}
/****************************************************
* ΢ÐÅͨ¹ýOAUTH*µ»ØÒ³ÃaeÖлñÈ¡ATÐÅÏ¢
****************************************************/
public function wxOauthAccessToken($code,$appId = NULL , $appSecret = NULL){
$appId = is_null($appId) ? self::appId : $appId;
$appSecret = is_null($appSecret) ? self::appSecret : $appSecret;
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret;=".$appSecret."&code;=".$code."&grant;_type=authorization_code";
$result = $this->wxHttpsRequest($url);
//print_r($result);
$jsoninfo = json_decode($result, true);
//$access_token = $jsoninfo["access_token"];
return $jsoninfo;
}
/****************************************************
* ΢ÐÅͨ¹ýOAUTHµÄAccess_TokenµÄÐÅÏ¢»ñÈ¡µ±Ç°Óû§ÐÅÏ¢ // Ö»Ö´ÐÐÔÚsnsapi_userinfoģʽÔËÐÐ
****************************************************/
public function wxOauthUser($OauthAT,$openId){
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$OauthAT."&openid;=".$openId."〈=zh_CN";
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
}
/*****************************************************
* Éu³ÉËae»u×Ö*u´® - ×i³¤Îª32λ×Ö*u´®
*****************************************************/
public function wxNonceStr($length = 16, $type = FALSE) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
if($type == TRUE){
return strtoupper(md5(time() . $str));
}
else {
return $str;
}
}
/*******************************************************
* ΢ÐÅÉÌ»§¶(C)µ¥ºÅ - ×i³¤28λ×Ö*u´®
*******************************************************/
public function wxMchBillno($mchid = NULL) {
if(is_null($mchid)){
if(self::mchid == "" || is_null(self::mchid)){
$mchid = time();
}
else{
$mchid = self::mchid;
}
}
else{
$mchid = substr(addslashes($mchid),0,10);
}
return date("Ymd",time()).time().$mchid;
}
/*******************************************************
* ΢Пñʽ»¯Êý×e±a³É²ÎÊý¸ñʽ - Ö§³Öurl¼ÓÃÜ
*******************************************************/
public function wxSetParam($parameters){
if(is_array($parameters) && !empty($parameters)){
$this->parameters = $parameters;
return $this->parameters;
}
else{
return array();
}
}
/*******************************************************
* ΢Пñʽ»¯Êý×e±a³É²ÎÊý¸ñʽ - Ö§³Öurl¼ÓÃÜ
*******************************************************/
public function wxFormatArray($parameters = NULL, $urlencode = FALSE){
if(is_null($parameters)){
$parameters = $this->parameters;
}
$restr = "";//³oʼ»¯¿Õ
ksort($parameters);//ÅÅÐo²ÎÊý
foreach ($parameters as $k => $v){//Ñ»*¶¨ÖƲÎÊý
if (null != $v && "null" != $v && "sign" != $k) {
if($urlencode){//Èç¹u²ÎÊýÐeÒªÔo¼ÓURL¼ÓÃܾÍÔo¼Ó£¬²»ÐeÒªÔo²»ÐeÒª
$v = urlencode($v);
}
$restr .= $k . "=" . $v . "&";//*µ»ØÍeÕu×Ö*u´®
}
}
if (strlen($restr) > 0) {//Èç¹u´aeÔÚÊý¾ÝÔo½«×iºo¡°&¡±É¾³ý
$restr = substr($restr, 0, strlen($restr)-1);
}
return $restr;//*µ»Ø×Ö*u´®
}
/*******************************************************
* ΢ÐÅMD5Ç(C)ÃuÉu³ÉÆ÷ - ÐeÒª½«²ÎÊýÊý×eת»¯³ÉΪ×Ö*u´®[wxFormatArray*½*¨]
*******************************************************/
public function wxMd5Sign($content, $privatekey){
try {
if (is_null($key)) {
throw new Exception("²Æ¸¶Í¨Ç(C)Ãukey²»ÄÜΪ¿Õ£¡");
}
if (is_null($content)) {
throw new Exception("²Æ¸¶Í¨Ç(C)ÃuÄÚÈݲ»ÄÜΪ¿Õ");
}
$signStr = $content . "&key;=" . $key;
return strtoupper(md5($signStr));
}
catch (Exception $e)
{
die($e->getMessage());
}
}
/*******************************************************
* ΢ÐÅSha1Ç(C)ÃuÉu³ÉÆ÷ - ÐeÒª½«²ÎÊýÊý×eת»¯³ÉΪ×Ö*u´®[wxFormatArray*½*¨]
*******************************************************/
public function wxSha1Sign($content, $privatekey){
try {
if (is_null($key)) {
throw new Exception("²Æ¸¶Í¨Ç(C)Ãukey²»ÄÜΪ¿Õ£¡");
}
if (is_null($content)) {
throw new Exception("²Æ¸¶Í¨Ç(C)ÃuÄÚÈݲ»ÄÜΪ¿Õ");
}
$signStr = $content . "&key;=" . $key;
return strtoupper(sha1($signStr));
}
catch (Exception $e)
{
die($e->getMessage());
}
}
/*******************************************************
* ½«Êý×e½aÎoXML - ΢Ðźi°u½Ó¿Ú
*******************************************************/
public function wxArrayToXml($parameters = NULL){
if(is_null($parameters)){
$parameters = $this->parameters;
}
if(!is_array($parameters) || empty($parameters)){
die("²ÎÊý²»ÎªÊý×eÎÞ*¨½aÎo");
}
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val))
{
$xml.="<".$key.">".$val."</".$key.">";
}
else
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
$xml.="</xml>";
return $xml;
}
}
ºoÆÚ»¹ÊÇ»aÔo¼ÓÔÚÒ»ÆðµÄ°ÑÕa¸oCLASS×oÆðÀ´£¬ÍøÉÏ×ÊÔ´ºÜ¶a£¬µ«ÊǶ¼ÊÇÓÐÒ»¶¨»u´¡µÄÈËÈ¥¿´¿´¸Ä¸Ä¿ÉÒÔ£¬¶ÔÓÚûÓнӴ¥¸Õ¸Õ½Ó´¥µÄÐÂÊÖ»¹ÊÇÐeÒª¸øÓeÖ§³ÖµÄ¡£°iÖuÓû§ÂÅÂÅ˼Â*¡£
ÔÙÀ´Ò»*Ý£º
Ê×Ïȸø´o¼Ò¿´Ò»¿´Õa¸o±i¸ñ£º
¸u¾Ý΢П߼¶ºi°u½Ó¿Ú£¬¿ª¢PHP°ae±¾µÄAPI½Ó¿Ú£¬ÏÖÔÚ½øÐÐÖ÷Òª´uÂeÖÎo¡£
ºi°u½Ó¿Úµ÷ÓÃÇeÇo´uÂe£¬ËuÓÐÇeÇo²ÎÊýΪ±ØÌi²ÎÊýÓeÎĵµ¶ÔÓ¦£º
class Wxapi {
private $app_id = 'wxXXXXXXXXXXXX'; //¹«ÖÚÕ˺Åappid£¬Ê×ÏÈÉeÇeÓeÖ®ÅaÌ׵Ĺ«ÖÚÕ˺Å
private $app_secret = 'XXXXXXXXXXXXXXXXXXXXXXXX';//¹«ÖÚºÅsecret£¬Óû§»ñÈ¡Óû§ÊÚȨtoken
private $app_mchid = 'XXXXXXXX';//ÉÌ»§ºÅid
function __construct(){
//do sth here....
}
/**
* ΢ÐÅÖ§¸¶
* @param string $openid Óû§openid
*/
public function pay($re_openid)
{
include_once('WxHongBaoHelper.php');
$commonUtil = new CommonUtil();
$wxHongBaoHelper = new WxHongBaoHelper();
$wxHongBaoHelper->setParameter("nonce_str", $this->great_rand());//Ëae»u×Ö*u´®£¬Ø¢³¤ÓÚ 32 λ
$wxHongBaoHelper->setParameter("mch_billno", $this->app_mchid.date('YmdHis').rand(1000, 9999));//¶(C)µ¥ºÅ
$wxHongBaoHelper->setParameter("mch_id", $this->app_mchid);//ÉÌ»§ºÅ
$wxHongBaoHelper->setParameter("wxappid", $this->app_id);
$wxHongBaoHelper->setParameter("nick_name", 'ºi°u');//Ìa¹(C)*½Ãu³Æ
$wxHongBaoHelper->setParameter("send_name", 'ºi°u');//ºi°u*¢ËÍÕßÃu³Æ
$wxHongBaoHelper->setParameter("re_openid", $re_openid);//Ïa¶ÔÓÚÒ½Âo»¥Í¨µÄopenid
$wxHongBaoHelper->setParameter("total_amount", 100);//¸¶¿i½ð¶i£¬µ¥Î»*Ö
$wxHongBaoHelper->setParameter("min_value", 100);//×iСºi°u½ð¶i£¬µ¥Î»*Ö
$wxHongBaoHelper->setParameter("max_value", 100);//×i´oºi°u½ð¶i£¬µ¥Î»*Ö
$wxHongBaoHelper->setParameter("total_num", 1);//ºi°u…§*Å×ÜÈËÊý
$wxHongBaoHelper->setParameter("wishing", '¸ÐлÄu²ÎÓeºi°uÅÉ*¢»i¶¯£¬×£ÄuÐÂÄe¿iÀÖ£¡');//ºi°u×£¸£²i
$wxHongBaoHelper->setParameter("client_ip", '127.0.0.1');//µ÷ÓýӿڵĻuÆ÷ Ip µØÖ*
$wxHongBaoHelper->setParameter("act_name", 'ºi°u»i¶¯');//»iÛ½Ãu³Æ
$wxHongBaoHelper->setParameter("remark", '¿iÀ´ÇÀ£¡');//±¸×¢ÐÅÏ¢
$postXml = $wxHongBaoHelper->create_hongbao_xml();
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
$responseXml = $wxHongBaoHelper->curl_post_ssl($url, $postXml);
//ÓÃ×÷½a¹uµ÷ÊÔÊa³o
//echo htmlentities($responseXml,ENT_COMPAT,'UTF-8');
$responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
return $responseObj->return_code;
}
*»ñÈ¡Ëae»u×Öu´®½¨£º**
/**
* Éu³ÉËae»uÊý
*/
public function great_rand(){
$str = '1234567890abcdefghijklmnopqrstuvwxyz';
for($i=0;$i<30;$i++){
$j=rand(0,35);
$t1 .= $str[$j];
}
return $t1;
}
*Ç(C)ÃuËa¨£º
**
/**
ÀýÈ磺
appid£º wxd111665abv58f4f
mch_id£º 10000100
device_info£º 1000
Body£º test
nonce_str£º ibuaiVcKdpRxkhJA
µÚÒ»²½£º¶Ô²ÎÊý°´ÕÕ key=value µÄ¸ñʽ£¬²¢°´ÕÕ²ÎÊýÃu ASCII ×ÖµaÐoÅÅÐoÈçÏ£º
stringA="appid=wxd930ea5d5a258f4f&body;=test&device;_info=1000&mch;_i
d=10000100&nonce;_str=ibuaiVcKdpRxkhJA";
µÚ¶þ²½£ºÆ´½ÓÖ§¸¶ÃÜÔ¿£º
stringSignTemp="stringA&key;=192006250b4c09247ec02edce69f6a2d"
sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A
9CF3B7"
*/
protected function get_sign(){
define('PARTNERKEY',"QSRXXXXXXXXXXXXXXXXXXXXX");
try {
if (null == PARTNERKEY || "" == PARTNERKEY ) {
throw new SDKRuntimeException("ÃÜÔ¿²»ÄÜΪ¿Õ£¡" . "<br>");
}
if($this->check_sign_parameters() == false) { //¼i²eÉu³ÉÇ(C)Ãu²ÎÊý
throw new SDKRuntimeException("Éu³ÉÇ(C)Ãu²ÎÊýȱʧ£¡" . "<br>");
}
$commonUtil = new CommonUtil();
ksort($this->parameters);
$unSignParaString = $commonUtil->formatQueryParaMap($this->parameters, false);
$md5SignUtil = new MD5SignUtil();
return $md5SignUtil->sign($unSignParaString,$commonUtil->trimString(PARTNERKEY));
}catch (SDKRuntimeException $e)
{
die($e->errorMessage());
}
}
*CURLÇeÇoÒÔ¼°¢ËÍÖ¤Êe£º
**
function curl_post_ssl($url, $vars, $second=30,$aHeader=array())
{
$ch = curl_init();
//³¬Ê±Ê±¼a
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//ÕaÀiÉeÖôuÀi£¬Èç¹uÓеĻ°
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//cert Óe key *Ö±ðÊoÓÚÁ½¸o.pemÎļþ
//ÇeÈ*±£ÄuµÄlibcurl°ae±¾ÊÇ*ñÖ§³ÖË«ÏoÈÏÖ¤£¬°ae±¾¸ßÓÚ7.20.1
curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_cert.pem');
curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_key.pem');
curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'rootca.pem');
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
//echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
}
Èe¿ÚÎļþ£º
@require "pay.php";
//»ñÈ¡Óû§ÐÅÏ¢
$get = $_GET['param'];
$code = $_GET['code'];
//ÅжÏcodeÊÇ*ñ´aeÔÚ
if($get=='access_token' && !empty($code)){
$param['param'] = 'access_token';
$param['code'] = $code;
$packet = new Packet();
//»ñÈ¡Óû§openidÐÅÏ¢
$userinfo = $packet->_route('userinfo',$param);
if(empty($userinfo['openid'])){
exit("NOAUTH");
}
//µ÷È¡Ö§¸¶*½*¨
$packet->_route('wxpacket',array('openid'=>$userinfo['openid']));
}else{
$packet->_route('userinfo');
}
ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒѧϰPHP³ÌÐoÉe¼ÆÓÐËu°iÖu£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£