PHP WeChat public account automatically sends red packet API, php public red packet api

Source: Internet
Author: User
Tags learn php programming

PHP public account automatically sends red packet API, php public red packet api

This example shares the API code for automatically sending red packets to the PHP public account for your reference. The details are as follows:

Paste the core interface code and enter the data yourself. The interface test is OK.
Wechat_packet.php

<! --? Php/*** interface for sending Red Packets * Created by PhpStorm. * User: ADKi * Date: 2016/4/25 0025 * Time: */class wechat_packet {private $ url = 'https: // your private $ mch_id; // merchant ID private $ weixin_appid; // public account appid private $ send_name; // merchant name private $ total_num = 1; // total number of sent Red Packets private $ wishing; // private $ client_ip; // the IP address of the machine that calls the interface, private $ act_name; // activity name, privat E $ remark; // remarks private $ nonce_str; // random string, no longer than 32-bit private $ api_password; private $ arraytoxml; // convert the array to xml/*** Public Key */private $ public_key = "/api/wechat/cert/apiclient_cert.pem "; /*** private key */private $ private_key = '/api/wechat/cert/apiclient_key.pem '; /*** ca certificate */private $ rootca = 'api/wechat/cert/rootca. pem'; public function _ construct () {// initialize the red packet settings $ this ---> weixin_appid = C ('wap _ weixin_appid '); $ This-> mch_id = C ('wechat _ mch_id '); $ this-> send_name = C ('wechat _ send_name '); $ this-> wishing = C ('wechat _ wishing '); $ this-> act_name = C ('wechat _ act_name '); $ this-> client_ip = $ _ SERVER ['server _ ADDR ']; $ this-> remark = C ('wechat _ remark '); $ this-> nonce_str = $ this-> create_nonce_str (32); $ this-> api_password = C ('wechat _ api_password '); $ inc_file = BASE_PATH.DS. 'api '. DS. 'wechat '. DS. 'arraytoxml. php'; if (Is_file ($ inc_file) {require ($ inc_file) ;}$ this-> arraytoxml = new ArrayToXML ();} public function send_post ($ mch_billno, $ re_openid, $ total_amount) {$ sign = $ this-> create_sign ($ mch_billno, $ re_openid, $ total_amount); $ send_array = array ('nonce _ str' => $ this-> nonce_str, 'mch _ billno' =>$ mch_billno, 'mch _ id' =>$ this-> mch_id, 'wxappid '=>$ this-> weixin_appid, 'Send _ name' => $ this-> send_name,'re _ ope Nid '=> $ re_openid, 'total _ amount' => $ total_amount, 'total _ num' => $ this-> total_num, 'wishing' => $ this-> wishing, 'client _ ip' => $ this-> client_ip, 'Act _ name' => $ this-> act_name, 'remark' => $ this-> remark, 'sign' => $ sign,); $ send_xml = $ this-> arraytoxml-> toXml ($ send_array ,''); $ data = $ this-> curl_post_ssl ($ this-> url, $ send_xml); $ data = $ this-> xmlToArray ($ data); file_put_contents ('adki ', var_expo Rt ($ data, true), FILE_APPEND);}/* Make sure that your libcurl version supports two-way authentication. The version is later than 7.20.1 */private 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); // set the proxy here. If so, // curl_setopt ($ ch, CURLOPT_PROXY, '10. 206.30.98 '); // curl_setopt ($ ch, CURLOPT_PROXYPORT, 8080); curl_setopt ($ ch, CURLOPT_URL, $ url ); Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false); // select one of the following two methods: // the first method, cert and key belong to two types respectively. pem file // The default format is PEM. you can comment curl_setopt ($ ch, CURLOPT_SSLCERTTYPE, 'pem'); curl_setopt ($ ch, CURLOPT_SSLCERT, getcwd (). $ this-> public_key); // The default format is PEM. you can comment curl_setopt ($ ch, CURLOPT_SSLKEYTYPE, 'pem'); curl_setopt ($ ch, CURLOPT_SSLKEY, getcwd (). $ this-> private_key); // ca certificate curl_setopt ($ Ch, CURLOPT_CAINFO, $ this-> rootca); // Method 2: merge two files into one. pem file // curl_setopt ($ ch, CURLOPT_SSLCERT, getcwd (). '/all. 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 ;}// generate the private function create_sign ($ mch_billno, $ re_openid, $ total_amount) {$ string_array = array ('act _ name' => $ this-> act_name, 'client _ ip' => $ this-> client_ip, 'mch _ billno' =>$ mch_billno, 'mch _ id' =>$ this-> mch_id, 'nonce _ str' =>$ this-> nonce_str,'re _ openid' => $ re_openid, 'remark' => $ this-> remark, 'Send _ name' => $ this-> send_name, 'total _ amou Nt '=> $ total_amount, 'total _ num' => $ this-> total_num, 'wishing' => $ this-> wishing, 'wxappid '=> $ this-> weixin_appid,); foreach ($ string_array as $ key => $ value) {if (! Empty ($ value) {$ stringA. = "$ key = $ value"; if ($ key! = 'Wxappid ') {$ stringA. = '&' ;}}// convert to UTF-8 $ stringA = $ this-> gbkToUtf8 ($ stringA ); $ stringSignTemp = "$ stringA & key = $ this-> api_password"; $ sign = MD5 ($ stringSignTemp); $ sign = strtoupper ($ sign); return $ sign ;} // generate the random string private function create_nonce_str ($ length) {$ str = null; $ strPol = "regular"; $ max = strlen ($ strPol)-1; for ($ I = 0; $ I <$ length; $ I ++) {$ str. = $ strPol [rand (0, $ max)]; // rand ($ min, $ max) generate a random integer between min and max} return $ str ;} /*** automatically converts gbk or gb2312 encoded strings into utf8. * The encoding class of the input string is automatically determined. If it is UTF-8, no conversion is required, otherwise, the UTF-8 character string * supports UTF-8, gbk, gb2312 * @ $ str: string */private function gbkToUtf8 ($ str) {$ charset = mb_detect_encoding ($ str, array ('ascii ', 'utf-8', 'gbk', 'gb2312'); $ charset = strtolower ($ charset ); if ("UTF-8 "! = $ Charset) {$ str = iconv ('utf-8', $ charset, $ str);} return $ str;} private function xmlToArray ($ postStr) {$ msg = array (); $ msg = (array) simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); return $ msg ;}}

Array to xml: arraytoxml. php

<!--?php/** * Created by PhpStorm. * User: ADKi * Date: 2016/4/26 0026 * Time: 12:19 */class ArrayToXML{ public function toXml($data){  $xml = '<xml-->';  foreach ($data as $key => $value){   if (is_numeric($value)){    $xml .= "<".$key.">".$value."<!--".$key."-->";   }else{    $xml .= "<".$key."><!--[CDATA[".$value."]]--><!--".$key."-->";   }  }  $xml .= '';  return $xml; }}

The above is all the content of this article. I hope it will help you learn PHP programming, and I hope you can support more help.

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.