PHP WeChat public account automatically sends red packet API, php public red packet api_PHP tutorial

Source: Internet
Author: User
Tags learn php programming openid
The PHP public account automatically sends the red packet API and the php public red packet api. PHP public account auto-send red packet API. php public red packet api this article provides an example of the PHP public account auto-send red packet API code for your reference. The following describes the PHP public account auto-send red packet API and 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

 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 _ openid' => $ 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_export ($ data, true), FILE_APPEND);}/* Ensure that your libcurl version supports two-way authentication, version 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 proxy here, if yes // 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. 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 (). $ thi S-> private_key); // ca certificate curl_setopt ($ ch, CURLOPT_CAINFO, $ this-> rootca); // The second method is to 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 _ nam E '=> $ this-> send_name, 'total _ amount' => $ 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

 ';  foreach ($data as $key => $value){   if (is_numeric($value)){    $xml .= "<".$key.">".$value."
 ";   }else{    $xml .= "<".$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.

Examples in this article share the API code for automatically sending red packets to the PHP public account, and share it with you for your reference. The details are as follows...

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.