Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize Wechat withdrawal function in PHP

Shulou Source: shulou.com Published: 2022-06-02 09:42:43 09月27日 Update

PHP how to achieve Wechat withdrawal function, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article hope you can solve this problem.

The method of encapsulating and withdrawing

Function tixian ($money) {$appid = "#"; / / merchant account appid $secret = "#"; / / api password $mch_id = "#"; / / merchant number $mch_no = "#"; $openid= "123456789"; / / authorized user openid $arr = array (); $arr ['mch_appid'] = $appid $arr ['mchid'] = $mch_id; $arr [' nonce_str'] = ugv::randomid (20); / / random string, no longer than 32 bits $arr ['partner_trade_no'] =' 1298016501'. Date ("Ymd"). Rand (10000, 90000). Rand (10000, 90000); / / merchant order number $arr ['openid'] = $openid; $arr [' check_name'] = 'NO_CHECK';// verify whether the user's real name is verified, and the amount of payment $arr [' amount'] = $money;// is not verified here. The unit is $desc = "# withdrawal"; $arr ['desc'] = $desc / / description information $arr ['spbill_create_ip'] =' 192.168.0.1 signature traversal / get the server's ip / / encapsulated algorithm $notify = new Notify_pub (); $notify- > weixin_app_config = array (); $notify- > weixin_app_config ['KEY'] = $mch_no; $arr [' sign'] = $notify- > getSign ($arr); / / signature $var = $notify- > arrayToXml ($arr) $xml = $this- > curl_post_ssl ('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', $var, 30, array (), 1); $rdata = simplexml_load_string ($xml,' SimpleXMLElement', LIBXML_NOCDATA); $return_code = (string) $rdata- > return_code; $result_code = (string) $rdata- > result_code; $return_code = trim (strtoupper ($return_code)); $result_code = trim (strtoupper ($result_code)) If ($return_code = = 'SUCCESS' & & $result_code =' SUCCESS') {$isrr = array ('con'= >' ok', 'error' = > 0,);} else {$returnmsg = (string) $rdata- > return_msg; $isrr = array (' error' = > 1, 'errmsg' = > $returnmsg,);} return json_encode ($isrr);}

Curl_post_ssl () used

Function curl_post_ssl ($url, $vars, $second = 30, $aHeader = array ()) {$isdir = "/ cert/"; / / Certificate location $ch = curl_init (); / / initialize curl curl_setopt ($ch, CURLOPT_TIMEOUT, $second); / / set the maximum number of seconds to execute curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); / / require the result to be a string and output to curl_setopt on the screen ($ch, CURLOPT_URL, $url) / / crawl the specified web page curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false); / / terminate verification curl_setopt from the server ($ch, CURLOPT_SSL_VERIFYHOST, false); / / curl_setopt ($ch, CURLOPT_SSLCERTTYPE, 'PEM'); / / Certificate type curl_setopt ($ch, CURLOPT_SSLCERT, $isdir. 'apiclient_cert.pem'); / / Certificate location curl_setopt ($ch, CURLOPT_SSLKEYTYPE,' PEM'); / / encryption type of private key specified in CURLOPT_SSLKEY curl_setopt ($ch, CURLOPT_SSLKEY, $isdir. 'apiclient_key.pem'); / / Certificate location curl_setopt ($ch, CURLOPT_CAINFO,' PEM'); curl_setopt ($ch, CURLOPT_CAINFO, $isdir. 'rootca.pem'); if (count ($aHeader) > = 1) {curl_setopt ($ch, CURLOPT_HTTPHEADER, $aHeader); / / set header} curl_setopt ($ch, CURLOPT_POST, 1); / / post submission method curl_setopt ($ch, CURLOPT_POSTFIELDS, $vars); / / all data is sent $data = curl_exec ($ch) using the "POST" operation in the HTTP protocol / / execute if ($data) {curl_close ($ch); return $data;} else {$error = curl_errno ($ch); echo "call faild, errorCode:$error\ n"; curl_close ($ch); return false;}}

For more information about the signature algorithm, please refer to the official documentation of Wechat

Simply demonstrate the signature algorithm:

/ / organize the data to be sent into $dataksort ($data); / sort / / use the format of URL key-value pairs (that is, key1=value1&key2=value2 … ) spliced into a string $str='';foreach ($data as $k = > $v) {$str.=$k.'='.$v.'&';} / / spliced API key $str.='key='.$secrect;$data ['sign'] = md5 ($str); / / encryption

Convert the array to xml format (simple method):

/ / traversal array method function arraytoxml ($data) {$str=''; foreach ($data as $k = > $v) {$str.=''.$v.'';} $str.=''; return $str;}

Convert the xml format to an array:

Function xmltoarray ($xml) {/ / prohibit references to external xml entities libxml_disable_entity_loader (true); $xmlstring = simplexml_load_string ($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode (json_encode ($xmlstring), true); return $val;}

Let's take a look at the withdrawal class encapsulated by ThinkPHP5.

Tags: User certificate name data merchant method encapsulation character array format step authentication location string real name interface algorithm encryption service authentication Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB vpn MySQL Docker