PHP WeChat public number of randomly issued cash red envelopes function

Source: Internet
Author: User
Tags openid
This time for everyone to bring PHP public random cash red envelope function, PHP public number randomly issued cash red envelope function of the note, the following is the actual case, together to see.

The following is a reference to the development of cash red envelopes in the payment development document as shown in.

Cash red envelopes, is to pay the merchant platform to provide one of the marketing tools, online since the vast number of merchants and users love. Merchants can issue cash envelopes to paying users through the platform. After the user receives the red envelope, the money arrives the user to pay the change account, and the 0 Wallet's other funds has the same use export, if the user does not receive, the fund will return to the merchant's payment account after 24 hours.

The following is the code snippet I used to test the payment of red envelopes, for reference only.

/* Test Enterprise to send red envelopes to individuals */Public Function Weixin_red_packet () {//Request parameters//random string $data [' Nonce_str ']= $this->get_unique_value ()   ;   Signature $data [' sign ']= ';   Merchant number, enter your merchant number $data [' mch_id ']= "**********"; Merchant Order number, you can combine 28-bit merchant order numbers as required $data [' Mch_billno ']= $data [' mch_id '].date ("Ymd"). "   888888 ". Rand (1000,9999);   Public account AppID, enter their own public number AppID $data [' wxappid ']= ' ********* ';   Merchant name $data [' Send_name ']= "*******";   User OpenID, enter the user OpenID to send red envelopes $data [' Re_openid ']= "*********";   Payment amount $data [' Total_amount ']= "100";   Total number of red envelopes issued $data [' Total_num ']= "1";   Red Envelopes Blessing Language $data [' Wishing ']= "********";   IP address $data [' client_ip ']=$_server[' local_addr '];   Activity name $DATA [' Act_name ']= "*******";   Note $data [' Remark ']= ' no notes ';   Scene ID//$data [' send_id ']= "product_5";   Activity information//$data [' Risk_info ']= "";   Generate signature//data array to process//API key, enter your own appsecret $appsecret = "*********";   $data =array_filter ($data);   Ksort ($data);   $str = ""; foreach ($data as $k = = $v) {$str. = $k. " = ". $v."   & "; } $str. = "KEy= ". $appsecret;   $data [' Sign ']=strtoupper (MD5 ($STR)); /* Red envelope operation: 1. Convert the request data to XML 2. Send Request 3. Convert the request result to an array of 4. Enter the request information and the request result into the database 4. Determine if the communication succeeds 5.   Account Success *//Send Red envelopes interface Address $url = "Https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";   Converts the request data from an array into an XML $xml = $this->arraytoxml ($data);   Request Operation $res = $this->curl ($xml, $url);   Convert the request result from XML to an array $arr = $this->xmltoarray ($res);   Please request the information and request the result input into the database//$transfer [' Partner_trade_no ']= $data [' partner_trade_no '];     $transfer [' Request_data ']=serialize ($data);   $transfer [' Response_data ']=serialize ($arr);   if ($arr [' Return_code ']== "SUCCESS" && $arr [' Result_code ']== ' SUCCESS ') {//$transfer [' SUCCESS ']=1;   $transfer _res[' success ']=1;   }else{//$transfer [' Success ']=0;   $transfer _res[' success ']=0;   $transfer _res[' desc ']= $arr [' return_msg '];   }//$transfer [' Add_time ']=time ();   D ("Weixin_transfer")->add ($transfer); Output request result array echo "<pre>";  Print_r ($arr); Exit ("Weixin_red_packet");   }//Generate 32-bit unique random string private function Get_unique_value () {$str =uniqid (Mt_rand (), 1);   $str =SHA1 ($STR); return MD5 ($STR);   }//convert array to XML private function Arraytoxml ($arr) {$xml = "<xml>"; foreach ($arr as $k = = $v) {$xml. = "<". $k. " > ". $v." </". $k."   > ";   } $xml. = "</xml>"; return $xml;   }//Convert XML to an array private function Xmltoarray ($xml) {//disallow referencing of external XML entity Libxml_disable_entity_loader (TRUE);   $xmlstring =simplexml_load_string ($xml, "SimpleXMLElement", libxml_nocdata);   $arr =json_decode (Json_encode ($xmlstring), true); return $arr;   }//Curl operation Private Function curl ($param = "", $url) {$postUrl = $url;   $curlPost = $param;                      Initialize Curl $ch = Curl_init ();            Crawl specified Web page curl_setopt ($ch, Curlopt_url, $POSTURL);             Set HEADER curl_setopt ($ch, Curlopt_header, 0);         Requires the result to be a string and output to the screen curl_setopt ($ch, Curlopt_returntransfer, 1); POST Submission Method curl_setopt ($ch, Curlopt_post, 1);             Add the fields in the HTTP header (header) curl_setopt ($ch, Curlopt_postfields, $curlPost);       Terminates validation from the server curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);   curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE); This is the location of the certificate curl_setopt ($ch, CURLOPT_SSLCERT,GETCWD (). '    \cert\apiclient_cert.pem '); This is also the location of the certificate curl_setopt ($ch, CURLOPT_SSLKEY,GETCWD (). '    \cert\apiclient_key.pem ');                    Run Curl $data = curl_exec ($ch);   Turn off Curl Curl_close ($ch); return $data; }

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Curl with Csrf-token validation simulation submission Example

PHP+SPL Application Case Detailed

PHP implementation of load Balancing session common case list (with code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.