PHP version of WeChat public platform red envelope API, php public red envelope api_PHP tutorial

Source: Internet
Author: User
Tags oauth openid
PHP public platform red envelope API, php public red envelope api. The PHP version of the public platform red envelope API, php public red envelope api to rewrite the PHP below API, red envelope support, JSAPI dynamic parameter interface support git. oschina. netyo PHP public platform red envelope API, php public red envelope api

I have rewritten the API interfaces in PHP,

Support for red packets and support for dynamic parameter interfaces of JSAPI

Http://git.oschina.net/youkuiyuan/yky_test/blob/master/class/wxapi.class.php

API-added support for Red Packets

<? Php /************************************** * ****************** @ author Kyler You
 
  
* @ Link http://mp.weixin.qq.com/wiki/home/index.html * @ Version 2.0.1 * @ uses $ wxApi = new WxApi (); * @ package the API interface will be updated one after another ****************************** * ***********************/class WxApi {const appId = ""; const appSecret = ""; const mchid = ""; // merchant ID const privatekey = ""; // private key public $ parameters = array (); public function _ construct () {}/************************************* ***************** submit the API method, returns the specified JSON *************************** * ***********************/Public function wxHttpsRequest ($ url, $ data = null) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, success, FALSE); curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, FALSE ); if (! Empty ($ data) {curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data);} curl_setopt ($ curl, expires, 1 ); $ output = curl_exec ($ curl); curl_close ($ curl); return $ output ;} /*************************************** ******************** * *****************************/public function wxHttpsRequestPem ($ url, $ vars, $ second = 30, $ aHea Der = 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 are respectively In two. pem file // the default format is PEM. you can comment curl_setopt ($ ch, CURLOPT_SSLCERTTYPE, 'pem'); curl_setopt ($ ch, CURLOPT_SSLCERT, getcwd (). '/apiclient_cert.pem'); // the default format is PEM. you can comment curl_setopt ($ ch, CURLOPT_SSLKEYTYPE, 'pem'); curl_setopt ($ ch, CURLOPT_SSLKEY, getcwd (). '/apiclient_key.pem'); curl_setopt ($ ch, CURLOPT_CAINFO, 'pem'); curl_setopt ($ ch, CURLOPT_CAINFO, getcwd (). '/rootca. pem '); // 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 ;}} /************************************** * ************* Obtain the AccessToken and return the at information of the specified public account **************** * *********************************/public function wxAccessToken ($ appId = NULL, $ appSecret = NULL) {$ appId = is_null ($ appId )? Self: appId: $ appId; $ appSecret = is_null ($ appSecret )? Self: appSecret: $ appSecret; // echo $ appId, $ appSecret; $ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= ". $ AppId. "& secret = ". $ appSecret; $ result = $ this-> wxHttpsRequest ($ url); // print_r ($ result); $ jsoninfo = json_decode ($ result, true ); $ access_token = $ jsoninfo ["access_token"]; return $ access_token ;} /*************************************** * ************* use OPENID to obtain user information, returns an array ************************************** * *************/public function wxGetUser ($ openId) {$ wxAccessToken = $ this-> wxAccessToken (); $ url =" https://api.weixin.qq.com/cgi-bin/user/info?access_token= ". $ WxAccessToken. "& openid = ". $ openId. "& lang = zh_CN"; $ result = $ this-> wxHttpsRequest ($ url); $ jsoninfo = json_decode ($ result, true); return $ jsoninfo ;} /*************************************** * ************** send the specified template information to the specified user, after sending the data, return the specified JSON data ********************************* * *****************/public function wxSendTemplate ($ jsonData) {$ wxAccessToken = $ this-> wxAccessToken (); $ url =" https://api.weixin.qq.com/cgi-bin/message/template/send?access_token= ". $ WxAccessToken; $ result = $ this-> wxHttpsRequest ($ url, $ jsonData); return $ result ;} /*************************************** ******************** * *****************************/public function wxSetSend ($ touser, $ template_id, $ url, $ data, $ topcolor = '# 7B68EE') {$ template = array ('touser' => $ touser, 'Template _ id' => $ template_id, 'URL' => $ url, 'topcolor' => $ topcolor, 'Data' => $ data); $ jsonData = json_encode ($ template); $ result = $ this-> wxSendTemplate ($ jsonData); return $ result ;} /*************************************** * ************* set the OAUTH jump URL, returns string information-SCOPE = snsapi_base // The confirmation page is not returned during verification, only the OPENID ************************************* * *************/public function wxOauthBase ($ redirectUrl, $ state = "", $ appId = NULL) {$ appId = is_null ($ appId )? Self: appId: $ appId; $ url =" https://open.weixin.qq.com/connect/oauth2/authorize?appid= ". $ AppId. "& redirect_uri = ". $ redirectUrl. "& response_type = code & scope = snsapi_base & state = ". $ state. "# wechat_redirect"; return $ url ;} /*************************************** * ************* set the OAUTH jump URL, returns the string information-SCOPE = snsapi_userinfo // obtain the complete user information *************************** * **********************/public function wxOauthUserinfo ($ redirectUrl, $ state = "", $ appId = NULL) {$ appId = is_null ($ appId )? Self: appId: $ appId; $ url =" https://open.weixin.qq.com/connect/oauth2/authorize?appid= ". $ AppId. "& redirect_uri = ". $ redirectUrl. "& response_type = code & scope = snsapi_userinfo & state = ". $ state. "# wechat_redirect"; return $ url ;} /*************************************** ********************** * ****************************/public function wxHeader ($ url) {header ("location :". $ url );} /*************************************** * ************ get AT information through the OAUTH return page *********** **************************************** */Public function wxOauthAccessToken ($ code, $ appId = NULL, $ appSecret = NULL) {$ appId = is_null ($ appId )? Self: appId: $ appId; $ appSecret = is_null ($ appSecret )? Self: appSecret: $ appSecret; $ url =" https://api.weixin.qq.com/sns/oauth2/access_token?appid= ". $ AppId. "& secret = ". $ appSecret. "& code = ". $ code. "& grant_type = authorization_code"; $ result = $ this-> wxHttpsRequest ($ url); // print_r ($ result); $ jsoninfo = json_decode ($ result, true ); // $ access_token = $ jsoninfo ["access_token"]; return $ jsoninfo ;} /*************************************** * ************ get the current user information through the OAUTH Access_Token information. // only run in snsapi_userinfo mode ******** **************************************** * ***/public function wxOauthUser ($ OauthAT, $ openId) {$ url =" https://api.weixin.qq.com/sns/userinfo?access_token= ". $ OauthAT. "& openid = ". $ openId. "& lang = zh_CN"; $ result = $ this-> wxHttpsRequest ($ url); $ jsoninfo = json_decode ($ result, true); return $ jsoninfo ;} /*************************************** * ************* generate a random string-a string of up to 32 characters **************** * ***********************************/public function wxNonceStr ($ length = 16, $ type = FALSE) {$ chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234 56789 "; $ str =" "; for ($ I = 0; $ I <$ length; $ I ++) {$ str. = substr ($ chars, mt_rand (0, strlen ($ chars)-1), 1);} if ($ type = TRUE) {return strtoupper (md5 (time (). $ str);} else {return $ str ;}} /*************************************** **************** merchant order number-a maximum of 28 character strings **************** ***************************************/ public function wxMchBillno ($ mchid = NULL) {if (is_null ($ mchid) {if (Self: mchid = "" | is_null (self: mchid) {$ mchid = time ();} else {$ mchid = self: mchid ;}} else {$ mchid = substr (addslashes ($ mchid), 0, 10);} return date ("Ymd", time ()). time (). $ mchid ;} /*************************************** * *************** format the array into a parameter format-supports url encryption ************** **************************************** */public function wxSetParam ($ parameters) {if (is_array ($ parameters )&&! Empty ($ parameters) {$ this-> parameters = $ parameters; return $ this-> parameters;} else {return array ();}} /*************************************** * *************** format the array into a parameter format-supports url encryption ************** **************************************** */public function wxFormatArray ($ parameters = NULL, $ urlencode = FALSE) {if (is_null ($ parameters) {$ parameters = $ this-> parameters;} $ restr = ""; // Initialize an empty ksor T ($ parameters); // The sorting parameter foreach ($ parameters as $ k => $ v) {// The parameter if (null! = $ V & "null "! = $ V & "sign "! = $ K) {if ($ urlencode) {// Add URL encryption if the parameter is required. $ v = urlencode ($ v) is not required if no URL encryption is required.} $ restr. = $ k. "= ". $ v. "&"; // returns the complete string} if (strlen ($ restr)> 0) {// if data exists, the last "&" will be deleted $ restr = substr ($ restr, 0, strlen ($ restr)-1);} return $ restr; // return string }/********************************** * ******************* MD5 signature generator-the parameter array needs to be converted into a string [wxFormatArray method] **** **************************************** * ***********/public func Tion wxMd5Sign ($ content, $ privatekey) {try {if (is_null ($ key) {throw new Exception ("Caifutong signature key cannot be blank! ");} If (is_null ($ content) {throw new Exception (" Caifu signature content cannot be blank ");} $ signStr = $ content. "& key = ". $ key; return strtoupper (md5 ($ signStr);} catch (Exception $ e) {die ($ e-> getMessage ());}} /*************************************** * *************** Sha1 signature generator-the parameter array needs to be converted into a string [wxFormatArray method] ********* **************************************** * *****/public function wxSha1Sign ($ content, $ privatekey) {Try {if (is_null ($ key) {throw new Exception ("The Caifu signature key cannot be blank! ");} If (is_null ($ content) {throw new Exception (" Caifu signature content cannot be blank ");} $ signStr = $ content. "& key = ". $ key; return strtoupper (sha1 ($ signStr);} catch (Exception $ e) {die ($ e-> getMessage ());}} /*************************************** **************** parse the array into XML-red envelope interface **************** ***************************************/ public function wxArrayToXml ($ parameters = NULL) {if (is_null ($ parameters )){ $ Parameters = $ this-> parameters;} if (! Is_array ($ parameters) | empty ($ parameters) {die ("The parameter is not an array and cannot be parsed");} $ xml ="
  
   
"; Foreach ($ arr as $ key => $ val) {if (is_numeric ($ val) {$ xml. = "<". $ key. "> ". $ val."
   ";} Else $ xml. =" <". $ key.">".$val."
   ";}$ Xml. ="
  "; Return $ xml ;}}
 

This CLASS will be added together in the future. There are a lot of resources on the Internet, but there is a certain foundation for people to look at it and change it, support is still required for new users who are not new to Alibaba Cloud. It helps users to develop their own ideas.

The above is all the content of this article. I hope you will like it.

Rewrite the PHP below the API interface, red envelope support, JSAPI dynamic parameter interface support http://git.oschina.net/yo...

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.