PHP
·
发表于 5年以前
·
阅读量:8298
Ê×Ïȸø´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');
}
ÒÔÉϾÍÊǹØÓÚPHP΢Ðźi°uAPI½Ó¿ÚµÄÏeϸ´uÂe£¬*ÖÏi¸ø´o¼Ò£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu¡£