国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP版微信公眾平臺紅包API

2024-05-04 23:33:45
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP版微信公眾平臺微信API類,目前主要實現了微信紅包接口,陸續會繼續進行更新,非常的實用,這里推薦給小伙伴們,有需要的朋友可以參考下。

重寫了一下PHP下面的微信API接口,

微信紅包支持,JSAPI的動態參數接口支持

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

微信API類 - 增加紅包支持

  1. <?php 
  2. /******************************************************** 
  3. * @author Kyler You <QQ:2444756311> 
  4. * @link http://mp.weixin.qq.com/wiki/home/index.html 
  5. * @version 2.0.1 
  6. * @uses $wxApi = new WxApi(); 
  7. * @package 微信API接口 陸續會繼續進行更新 
  8. ********************************************************/ 
  9.  
  10. class WxApi { 
  11. const appId = ""
  12. const appSecret = "";  
  13. const mchid = ""//商戶號 
  14. const privatekey = ""//私鑰 
  15. public $parameters = array(); 
  16.  
  17. public function __construct(){ 
  18.  
  19.  
  20. /**************************************************** 
  21. * 微信提交API方法,返回微信指定JSON 
  22. ****************************************************/ 
  23.  
  24. public function wxHttpsRequest($url,$data = null){ 
  25. $curl = curl_init(); 
  26. curl_setopt($curl, CURLOPT_URL, $url); 
  27. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
  28. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 
  29. if (!emptyempty($data)){ 
  30. curl_setopt($curl, CURLOPT_POST, 1); 
  31. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
  32. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  33. $output = curl_exec($curl); 
  34. curl_close($curl); 
  35. return $output
  36.  
  37. /**************************************************** 
  38. * 微信帶證書提交數據 - 微信紅包使用 
  39. ****************************************************/ 
  40.  
  41. public function wxHttpsRequestPem($url$vars$second=30,$aHeader=array()){ 
  42. $ch = curl_init(); 
  43. //超時時間 
  44. curl_setopt($ch,CURLOPT_TIMEOUT,$second); 
  45. curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); 
  46. //這里設置代理,如果有的話 
  47. //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98'); 
  48. //curl_setopt($ch,CURLOPT_PROXYPORT, 8080); 
  49. curl_setopt($ch,CURLOPT_URL,$url); 
  50. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); 
  51. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); 
  52.  
  53. //以下兩種方式需選擇一種 
  54.  
  55. //第一種方法,cert 與 key 分別屬于兩個.pem文件 
  56. //默認格式為PEM,可以注釋 
  57. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); 
  58. curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/apiclient_cert.pem'); 
  59. //默認格式為PEM,可以注釋 
  60. curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); 
  61. curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/apiclient_key.pem'); 
  62.  
  63. curl_setopt($ch,CURLOPT_CAINFO,'PEM'); 
  64. curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/rootca.pem'); 
  65.  
  66. //第二種方式,兩個文件合成一個.pem文件 
  67. //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem'); 
  68.  
  69. ifcount($aHeader) >= 1 ){ 
  70. curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader); 
  71.  
  72. curl_setopt($ch,CURLOPT_POST, 1); 
  73. curl_setopt($ch,CURLOPT_POSTFIELDS,$vars); 
  74. $data = curl_exec($ch); 
  75. if($data){ 
  76. curl_close($ch); 
  77. return $data
  78. else {  
  79. $error = curl_errno($ch); 
  80. echo "call faild, errorCode:$error/n";  
  81. curl_close($ch); 
  82. return false; 
  83.  
  84. /**************************************************** 
  85. * 微信獲取AccessToken 返回指定微信公眾號的at信息 
  86. ****************************************************/ 
  87.  
  88. public function wxAccessToken($appId = NULL , $appSecret = NULL){ 
  89. $appId = is_null($appId) ? self::appId : $appId
  90. $appSecret = is_null($appSecret) ? self::appSecret : $appSecret
  91. //echo $appId,$appSecret; 
  92. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret
  93. $result = $this->wxHttpsRequest($url); 
  94. //print_r($result); 
  95. $jsoninfo = json_decode($result, true); 
  96. $access_token = $jsoninfo["access_token"]; 
  97. return $access_token
  98.  
  99. /**************************************************** 
  100. * 微信通過OPENID獲取用戶信息,返回數組 
  101. ****************************************************/ 
  102.  
  103. public function wxGetUser($openId){ 
  104. $wxAccessToken = $this->wxAccessToken(); 
  105. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$wxAccessToken."&openid=".$openId."&lang=zh_CN"
  106. $result = $this->wxHttpsRequest($url); 
  107. $jsoninfo = json_decode($result, true); 
  108. return $jsoninfo
  109.  
  110. /**************************************************** 
  111. * 微信通過指定模板信息發送給指定用戶,發送完成后返回指定JSON數據 
  112. ****************************************************/ 
  113.  
  114. public function wxSendTemplate($jsonData){ 
  115. $wxAccessToken = $this->wxAccessToken(); 
  116. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$wxAccessToken
  117. $result = $this->wxHttpsRequest($url,$jsonData); 
  118. return $result
  119.  
  120. /**************************************************** 
  121. * 發送自定義的模板消息 
  122. ****************************************************/ 
  123.  
  124. public function wxSetSend($touser$template_id$url$data$topcolor = '#7B68EE'){ 
  125. $template = array
  126. 'touser' => $touser
  127. 'template_id' => $template_id
  128. 'url' => $url
  129. 'topcolor' => $topcolor
  130. 'data' => $data 
  131. ); 
  132. $jsonData = json_encode($template); 
  133. $result = $this->wxSendTemplate($jsonData); 
  134. return $result
  135.  
  136. /**************************************************** 
  137. * 微信設置OAUTH跳轉URL,返回字符串信息 - SCOPE = snsapi_base //驗證時不返回確認頁面,只能獲取OPENID 
  138. ****************************************************/ 
  139.  
  140. public function wxOauthBase($redirectUrl,$state = "",$appId = NULL){ 
  141. $appId = is_null($appId) ? self::appId : $appId
  142. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_base&state=".$state."#wechat_redirect"
  143. return $url
  144.  
  145. /**************************************************** 
  146. * 微信設置OAUTH跳轉URL,返回字符串信息 - SCOPE = snsapi_userinfo //獲取用戶完整信息 
  147. ****************************************************/ 
  148.  
  149. public function wxOauthUserinfo($redirectUrl,$state = "",$appId = NULL){ 
  150. $appId = is_null($appId) ? self::appId : $appId
  151. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect"
  152. return $url
  153.  
  154. /**************************************************** 
  155. * 微信OAUTH跳轉指定URL 
  156. ****************************************************/ 
  157.  
  158. public function wxHeader($url){ 
  159. header("location:".$url); 
  160.  
  161. /**************************************************** 
  162. * 微信通過OAUTH返回頁面中獲取AT信息 
  163. ****************************************************/ 
  164.  
  165. public function wxOauthAccessToken($code,$appId = NULL , $appSecret = NULL){ 
  166. $appId = is_null($appId) ? self::appId : $appId
  167. $appSecret = is_null($appSecret) ? self::appSecret : $appSecret
  168. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code"
  169. $result = $this->wxHttpsRequest($url); 
  170. //print_r($result); 
  171. $jsoninfo = json_decode($result, true); 
  172. //$access_token = $jsoninfo["access_token"]; 
  173. return $jsoninfo;  
  174.  
  175. /**************************************************** 
  176. * 微信通過OAUTH的Access_Token的信息獲取當前用戶信息 // 只執行在snsapi_userinfo模式運行 
  177. ****************************************************/ 
  178.  
  179. public function wxOauthUser($OauthAT,$openId){ 
  180. $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$OauthAT."&openid=".$openId."&lang=zh_CN"
  181. $result = $this->wxHttpsRequest($url); 
  182. $jsoninfo = json_decode($result, true); 
  183. return $jsoninfo;  
  184.  
  185. /***************************************************** 
  186. * 生成隨機字符串 - 最長為32位字符串 
  187. *****************************************************/ 
  188. public function wxNonceStr($length = 16, $type = FALSE) { 
  189. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  190. $str = ""
  191. for ($i = 0; $i < $length$i++) { 
  192. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 
  193. if($type == TRUE){ 
  194. return strtoupper(md5(time() . $str)); 
  195. else { 
  196. return $str
  197.  
  198. /******************************************************* 
  199. * 微信商戶訂單號 - 最長28位字符串 
  200. *******************************************************/ 
  201.  
  202. public function wxMchBillno($mchid = NULL) { 
  203. if(is_null($mchid)){ 
  204. if(self::mchid == "" || is_null(self::mchid)){ 
  205. $mchid = time(); 
  206. else
  207. $mchid = self::mchid; 
  208. else
  209. $mchid = substr(addslashes($mchid),0,10); 
  210. return date("Ymd",time()).time().$mchid
  211.  
  212. /******************************************************* 
  213. * 微信格式化數組變成參數格式 - 支持url加密 
  214. *******************************************************/ 
  215.  
  216. public function wxSetParam($parameters){ 
  217. if(is_array($parameters) && !emptyempty($parameters)){ 
  218. $this->parameters = $parameters
  219. return $this->parameters; 
  220. else
  221. return array(); 
  222.  
  223. /******************************************************* 
  224. * 微信格式化數組變成參數格式 - 支持url加密 
  225. *******************************************************/ 
  226.  
  227. public function wxFormatArray($parameters = NULL, $urlencode = FALSE){ 
  228. if(is_null($parameters)){ 
  229. $parameters = $this->parameters; 
  230. $restr = "";//初始化空 
  231. ksort($parameters);//排序參數 
  232. foreach ($parameters as $k => $v){//循環定制參數 
  233. if (null != $v && "null" != $v && "sign" != $k) { 
  234. if($urlencode){//如果參數需要增加URL加密就增加,不需要則不需要 
  235. $v = urlencode($v); 
  236. $restr .= $k . "=" . $v . "&";//返回完整字符串 
  237. if (strlen($restr) > 0) {//如果存在數據則將最后“&”刪除 
  238. $restr = substr($restr, 0, strlen($restr)-1); 
  239. return $restr;//返回字符串 
  240.  
  241. /******************************************************* 
  242. * 微信MD5簽名生成器 - 需要將參數數組轉化成為字符串[wxFormatArray方法] 
  243. *******************************************************/ 
  244. public function wxMd5Sign($content$privatekey){ 
  245. try { 
  246. if (is_null($key)) { 
  247. throw new Exception("財付通簽名key不能為空!"); 
  248. if (is_null($content)) { 
  249. throw new Exception("財付通簽名內容不能為空"); 
  250. $signStr = $content . "&key=" . $key
  251. return strtoupper(md5($signStr)); 
  252. catch (Exception $e
  253. die($e->getMessage()); 
  254.  
  255. /******************************************************* 
  256. * 微信Sha1簽名生成器 - 需要將參數數組轉化成為字符串[wxFormatArray方法] 
  257. *******************************************************/ 
  258. public function wxSha1Sign($content$privatekey){ 
  259. try { 
  260. if (is_null($key)) { 
  261. throw new Exception("財付通簽名key不能為空!"); 
  262. if (is_null($content)) { 
  263. throw new Exception("財付通簽名內容不能為空"); 
  264. $signStr = $content . "&key=" . $key
  265. return strtoupper(sha1($signStr)); 
  266. catch (Exception $e
  267. die($e->getMessage()); 
  268.  
  269. /******************************************************* 
  270. * 將數組解析XML - 微信紅包接口 
  271. *******************************************************/ 
  272.  
  273. public function wxArrayToXml($parameters = NULL){ 
  274. if(is_null($parameters)){ 
  275. $parameters = $this->parameters; 
  276.  
  277. if(!is_array($parameters) || emptyempty($parameters)){ 
  278. die("參數不為數組無法解析"); 
  279.  
  280. $xml = "<xml>"
  281. foreach ($arr as $key=>$val
  282. if (is_numeric($val)) 
  283. $xml.="<".$key.">".$val."</".$key.">";  
  284. else 
  285. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";  
  286. $xml.="</xml>"
  287. return $xml;  
  288.  


后期還是會增加在一起的把這個CLASS做起來,網上資源很多,但是都是有一定基礎的人去看看改改可以,對于沒有接觸剛剛接觸的新手還是需要給予支持的。幫助用戶屢屢思路。

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吐鲁番市| 内乡县| 陈巴尔虎旗| 临沭县| 宁国市| 东乌珠穆沁旗| 五寨县| 永川市| 黄龙县| 泌阳县| 重庆市| 江川县| 彭山县| 桦川县| 平度市| 巴林左旗| 昌平区| 汕头市| 修水县| 延吉市| 望谟县| 神池县| 通江县| 梁河县| 米易县| 舟曲县| 棋牌| 巴里| 乐清市| 开封县| 竹山县| 西藏| 永登县| 保山市| 台南市| 鹰潭市| 肇州县| 古交市| 门头沟区| 西平县| 察雅县|