Get the App
SLTechnology News&Howtos  ›  Development  › 

Code Analysis of PHP WeChat red packet API Interface

Shulou Source: shulou.com Published: 2022-06-03 09:12:23 09月28日 Update

This article mainly explains "the code analysis of PHP WeChat red packet API interface". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the code analysis of PHP WeChat red packet API interface".

First of all, let's take a look at this form:

According to the Wechat advanced red packet interface, the PHP version of the API interface is developed, and now the main code is analyzed.

The red packet API calls the request code. All request parameters are required and correspond to the document:

Class Wxapi {private $app_id = 'wxXXXXXXXXXXXX'; / / Public account appid, first apply for the matching public account private $app_secret =' XXXXXXXXXXXXXXXXXXXXXXXX';// official account secret, and the user obtains user authorization token private $app_mchid = 'XXXXXXXX';// merchant number id function _ _ construct () {/ / do sth here.... } / * * WeChat Pay * @ param string $openid user openid * / public function pay ($re_openid) {include_once ('WxHongBaoHelper.php'); $commonUtil = new CommonUtil (); $wxHongBaoHelper = new WxHongBaoHelper (); $wxHongBaoHelper- > setParameter ("nonce_str", $this- > great_rand ()); / / Random string, longer than 32-bit $wxHongBaoHelper- > setParameter ("mch_billno", $this- > app_mchid.date (' YmdHis'). Rand (1000, 9999)) / / order number $wxHongBaoHelper- > setParameter ("mch_id", $this- > app_mchid); / / merchant number $wxHongBaoHelper- > setParameter ("wxappid", $this- > app_id); $wxHongBaoHelper- > setParameter ("nick_name", "red packet"); / / provider name $wxHongBaoHelper- > setParameter ("send_name", "red packet"); / / name of the sender of the red packet $wxHongBaoHelper- > setParameter ("re_openid", $re_openid) / / relative to openid $wxHongBaoHelper- > setParameter ("total_amount", 100); / / payment amount, unit $wxHongBaoHelper- > setParameter ("min_value", 100); / / minimum red packet amount, unit $wxHongBaoHelper- > setParameter ("max_value", 100); / / maximum red packet amount, unit $wxHongBaoHelper- > setParameter ("total_num", 1) / / the total number of people with red packets $wxHongBaoHelper- > setParameter ("wishing", 'Thank you for participating in the red packet distribution activities, Happy New year!') ; / / Red packet blessing surprise $wxHongBaoHelper- > setParameter ("client_ip", '127.0.0.1'); / / the Ip address of the machine calling the API $wxHongBaoHelper- > setParameter ("act_name", "red packet activity"); / / Live name $wxHongBaoHelper- > setParameter ("remark", 'come and grab!') / / remarks $postXml = $wxHongBaoHelper- > create_hongbao_xml (); $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; $responseXml = $wxHongBaoHelper- > curl_post_ssl ($url, $postXml); / / used as result debug output / / echo htmlentities ($responseXml,ENT_COMPAT,'UTF-8'); $responseObj = simplexml_load_string ($responseXml,' SimpleXMLElement', LIBXML_NOCDATA); return $responseObj- > return_code;}

Get the random string method:

/ * generate a random number * / public function great_rand () {$str = '1234567890abcdefghijklmnopqrstuvwxyzhuang; for ($isignsignalling parameters () = = false) {/ / check the generation signature parameter throw new SDKRuntimeException ("generating signature parameter is missing!" . "

");} $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 request and send certificate:

Function curl_post_ssl ($url, $vars, $second=30,$aHeader=array ()) {$ch = curl_init (); / / timeout curl_setopt ($ch,CURLOPT_TIMEOUT,$second); curl_setopt ($ch,CURLOPT_RETURNTRANSFER, 1); / / proxy is set here, if any curl_setopt ($ch,CURLOPT_URL,$url); curl_setopt ($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt ($ch,CURLOPT_SSL_VERIFYHOST,false) / / cert and key belong to two .pem files / / make sure that your version of libcurl supports two-way authentication, which is higher than 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;}}

Entry file:

@ require "pay.php"; / / get user information $get= $_ GET ['param']; $code = $_ GET [' code']; / / determine whether if ($get=='access_token' & &! empty ($code)) exists in code {$param ['param'] =' access_token'; $param ['code'] = $code; $packet = new Packet (); / / get user openid information $userinfo = $packet- > _ route (' userinfo',$param) If (empty ($userinfo ['openid'])) {exit ("NOAUTH");} / call the payment method $packet- > _ route (' wxpacket',array ('openid'= > $userinfo [' openid']));} else {$packet- > _ route ('userinfo') } Thank you for your reading. The above is the content of "Code Analysis of PHP WeChat red packet API Interface". After the study of this article, I believe you have a deeper understanding of the code analysis of PHP WeChat red packet API interface, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Red packet interface code parameter analysis user information public unit name version amount learning payment generation content merchant character string key Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB vpn OPPO Reno Huawei