PHP micro-letter public number automatically send red envelopes api_php Example

Source: Internet
Author: User

This article examples for you to share the PHP micro-letter public number automatically send red envelopes API code, to share with you for your reference. Specifically as follows:

Sticking out the core interface code as for the data to fill out yourself, interface test OK
wechat_packet.php

<!--? php/** * Send Red Envelope interface * Created by Phpstorm. * User:adki * DATE:2016/4/25 0025 * time:15:25/class wechat_packet{private $url = ' https://api.mch.weixin.qq.c Om/mmpaymkttransfers/sendredpack '//Request URL private $mch _id;//merchant number private $weixin _appid;//public account AppID private $send _name //Merchant Name private $total _num = 1;//Send the total number of red envelopes private $wishing; The blessing of the red envelopes private $client _ip;//the machine IP address of the calling interface is private $act _name;/
 /Activity name Private $remark//Memo Information Private $nonce _str;//random string, not longer than 32 private $api _password;
 private $arraytoxml;//array to XML/** * Public key/private $public _key = "/api/wechat/cert/apiclient_cert.pem";
 /** * Private $private _key = '/api/wechat/cert/apiclient_key.pem ';
 /** * CA Certificate * * Private $ROOTCA = ' Api/wechat/cert/rootca.pem ';
  Public Function __construct () {//initializing red envelope setting information $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 _ope
  Nid, $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->wish ing, ' 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);
  /* Please ensure that your Libcurl version supports two-way authentication, the version is higher than 7.20.1 */Private Function Curl_post_ssl ($url, $vars, $second =30, $aHeader =array ()) {
  $ch = Curl_init ();
  Timeout time curl_setopt ($ch, Curlopt_timeout, $second);
  curl_setopt ($ch, Curlopt_returntransfer, 1);
  Here set the agent, if any,//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);
  The following two ways to select a//first method, cert and key belong to two. Pem File//The default format is PEM, you can annotate curl_setopt ($ch, Curlopt_sslcerttype, ' Pem ');
  curl_setopt ($ch, CURLOPT_SSLCERT,GETCWD (). $this->public_key); The default format is PEM, which can be annotated curl_setopt ($ch, Curlopt_Sslkeytype, ' PEM ');
  curl_setopt ($ch, CURLOPT_SSLKEY,GETCWD (). $this->private_key);
  CA certificate curl_setopt ($ch, Curlopt_cainfo, $this->ROOTCA); The second way, two files synthesize a. 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\n\n\n";
   Curl_close ($ch);
  return false; }//Generate signature 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 ' => $t his->mch_id, ' nonce_str ' => $this->nonce_str, ' Re_openid ' => $re _openid, ' remark ' => $this->rem Ark, ' Send_name ' => $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 random string Private function Create_nonce_str ($length) {$str = null;
  $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $max = strlen ($strPol)-1;
 For ($i =0 $i < $length; $i + +) {$str. = $strPol [rand (0, $max)];//rand ($min, $max) generates a random integer between min and max two numbers} return $str;
  /** * Automatically judge the GBK or gb2312 encoded string into UTF8 * can automatically determine the input string encoding class, if itself is utf-8 without conversion, otherwise converted to Utf-8 String * Supported character encoding type is: utf-8,gbk,gb2312
*@ $STR: String Strings */ 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 the entire content of this article, I hope that you learn PHP programming help, but also hope that we support the cloud habitat community.

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.