PHP implementation of WeChat public number development of micro-letter Red Envelope method

Source: Internet
Author: User
Tags openid
This article mainly introduced the PHP public number development of the red envelope implementation method, combined with the case form analysis of PHP implementation of the public red envelope delivery function of the implementation of ideas, steps and specific operating skills, the need for friends can refer to the next

This paper introduces the implementation method of red envelopes in PHP public number development. Share to everyone for your reference, as follows:

These days encountered a customer to give them a public platform to add cash red envelopes function, is a two times the development of the function, handy Baidu, originally not complex. began to develop the function. The development process and requirements are now posted to share:

A. Requirements:

Fans click on their company's order on the customer's public platform, then return five yuan to the order and send it to the number on the order.

Two. Development ideas:

1: The first to get attention to this fan Openid,openid is to focus on a public ID, so that you can locate this person is the operator of the order.

2: Send the XML data request server.

Code has two PHP files

1.oauth2.php

<?php$code=$_get[' code '; $state =$_get[' state '; $appid = ' XXXX '; $appsecret = ' XXXXXXXX ';//if (Empty ($code)) $this- >error (' authorization failed '); $token _url= ' https://api.weixin.qq.com/sns/oauth2/access_token?appid= '. $appid ' &secret= '. $ Appsecret. ' &code= '. $code. ' &grant_type=authorization_code '; $token =json_decode (file_get_contents ($token _url)); if (Isset ($token Errcode) {echo ' 

2.hongbao.php

<?php//xxxxx.    It is necessary for developers to fill in the content, pay attention to not leak//from the SESSION to get to OpenID; $openid =$_session["OpenID"]; if (empty ($openid)) {header (' location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxx& Redirect_uri=http://www. xxxxxxx.com/oauth2.php&respose_type=code&scope=snsapi_base&state=xxxx&connect_redirect=1#    Wechat_redirect ');  }}//key functions Public Function Weixin_red_packet () {//request parameter//random string $data [' Nonce_str ']= $this->get_unique_value ();  Merchant number, enter your merchant number $data [' mch_id ']= "XXXXXXX"; Merchant Order number, you can combine 28-bit merchant order numbers as required $data [' Mch_billno ']= $data [' mch_id '].date ("Ymd"). "  XXXXXX ". Rand (1000,9999);  Public account AppID, enter their own public number AppID $data [' wxappid ']= ' XXXXXXX ';  Merchant name $data [' Send_name ']= "XXXXX";  User OpenID, enter the user OpenID session_start () to be sent red envelopes;  $data [' Re_openid ']=$_session["OpenID"];  Payment amount $data [' Total_amount ']= "XXXX";  The total number of red envelopes issued $data [' Total_num ']= "XXXX";  Red Packet Blessing Language $data [' Wishing ']= "XXXX";  IP address $data [' client_ip ']=$_server[' local_addr '];  Activity name $DATA [' Act_name ']= "XXXXX"; //Remarks $data [' remark ']= "XXXXX"; Generate signature//data array to process the//API key, enter your own k in the merchant number k $appsecret = "xxxxxxxxxxxxxx";  $data =array_filter ($data);  Ksort ($data);  $str = ""; foreach ($data as $k = = $v) {$str. = $k. " = ". $v."  & ";  } $str. = "key=". $appsecret;  $data [' Sign ']=strtoupper (MD5 ($STR));   /* Red Envelopes operation process: 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 succeeded 5. Determine if the transfer was successful  *///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);}  Generates a 32-bit unique random string private function Get_unique_value () {$str =uniqid (Mt_rand (), 1);  $str =SHA1 ($STR); return MD5 ($STR);}  Converts an array to xmlprivate function Arraytoxml ($arr) {$xml = "<xml>"; foreach ($arr as $k = = $v) {$xml. = "<". $k. " > ". $v." </". $k."  > ";  } $xml. = "</xml>"; return $xml;} Convert XML to array private function Xmltoarray ($xml) {//Disallow reference to 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;}  For 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); The certificate is placed under the Cert folder in the root directory of the Web site curl_setopt ($ch, Curlopt_sslcert,dirname (__file__).        Directory_separator. ' Cert '.    Directory_separator. ' Apiclient_cert.pem '); curl_setopt ($ch, Curlopt_sslkey,dirname (__file__).        Directory_separator. ' Cert '.    Directory_separator. ' Apiient_key.pem '); curl_setopt ($cH,curlopt_cainfo,dirname (__file__).        Directory_separator. ' Cert '.  Directory_separator. ' Rootca.pem ');  Run Curl $data = curl_exec ($ch);  Turn off Curl Curl_close ($ch); return $data;}? >

Related recommendations:

JS implementation of Red envelope random algorithm (with code)

PHP using parabolic model to implement red envelope generation algorithm program source code

PHP implementation Red envelope code

Related Article

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.