WeChat red envelope interface API-expand the common interface API of WeChat public platform (PHP version)-PHP source code

Source: Internet
Author: User
Tags oauth openid
APIs for red packet interfaces-APIs for common APIs for public platforms (PHP version), red packet support, and dynamic parameter interfaces for JSAPI

 = 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 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 funct Ion 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. = "". $ val. "";} else $ xml. = "";} $ xml. = ""; return $ xml ;}}

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.