亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > PHP > 正文

微信API接口大全

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

本文給大家介紹的是個人總結的一些微信API接口,包括微信支付、微信紅包、微信卡券、微信小店等,十分的全面,有需要的小伙伴可以參考下。

微信入口綁定,微信事件處理,微信API全部操作包含在這些文件中。

微信支付、微信紅包、微信卡券、微信小店。

1. [代碼]index.php

 

 
  1. <?php 
  2. include_once 'lib.inc.php'
  3.  
  4. $wcObj = new WeChat("YOUKUIYUAN"); 
  5. $wcObj->wcValid(); 

2. [代碼]微信入口類

 

 
  1. <?php 
  2. /** 
  3. * Description of wechat 
  4. * 
  5. * @author Administrator 
  6. */ 
  7. class WeChat extends WxApi{ 
  8. public $token = ""
  9. //put your code here 
  10. public function __construct($token = "") { 
  11. parent::__construct(); 
  12. $this->token = $token
  13.  
  14. public function wcCheckSignature(){ 
  15. try{ 
  16. if (emptyempty($this->token)) { 
  17. throw new Exception('TOKEN is not defined!'); 
  18.  
  19. $signature = $_GET["signature"]; 
  20. $timestamp = $_GET["timestamp"]; 
  21. $nonce = $_GET["nonce"]; 
  22.  
  23. $token = $this->token; 
  24. $tmpArr = array($token$timestamp$nonce); 
  25. // use SORT_STRING rule 
  26. sort($tmpArr, SORT_STRING); 
  27. $tmpStr = implode( $tmpArr ); 
  28. $tmpStr = sha1( $tmpStr ); 
  29.  
  30. if$tmpStr == $signature ){ 
  31. return true; 
  32. }else
  33. return false; 
  34. }  
  35. catch (Exception $e) { 
  36. echo 'Message: ' .$e->getMessage(); 
  37.  
  38. public function wcValid(){ 
  39. $echoStr = isset($_GET["echostr"]) && !emptyempty($_GET["echostr"]) ? addslashes($_GET["echostr"]) : NULL; 
  40. if(is_null($echoStr)){ 
  41. $this->wcMsg(); 
  42. else
  43. //valid signature , option 
  44. if($this->wcCheckSignature()){ 
  45. echo $echoStr
  46. exit
  47. else
  48. exit(); 
  49.  
  50. public function wcMsg(){ 
  51. //get post data, May be due to the different environments 
  52. $postStr = isset($GLOBALS["HTTP_RAW_POST_DATA"]) && !emptyempty($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : ""
  53. if(!emptyempty($postStr)){ 
  54. libxml_disable_entity_loader(true); 
  55. $postObj = simplexml_load_string($postStr'SimpleXMLElement', LIBXML_NOCDATA); 
  56. $this->zcLog(TRUE,$postObj); 
  57.  
  58. $fromUsername = $postObj->FromUserName; 
  59. $toUsername = $postObj->ToUserName; 
  60. $MsgType = $postObj->MsgType; 
  61.  
  62. if($MsgType == 'event'){//執行事件相應 
  63. $Event = $postObj->Event; 
  64. switch ($Event) { 
  65. case 'subscribe'://關注 
  66. break
  67. case 'unsubscribe'://取消關注 
  68. break
  69. case 'SCAN'://掃描 
  70. break
  71. case 'LOCATION'://地址 
  72. break
  73. case 'CLICK'://點擊時間 
  74. break
  75. case 'VIEW'://跳轉 
  76. break
  77. case 'card_pass_check'://卡券審核通過 
  78. break
  79. case 'card_not_pass_check'://卡券審核失敗 
  80. break
  81. case 'user_get_card'://用戶領取卡券 
  82. break
  83. case 'user_del_card'://用戶刪除卡券 
  84. break
  85. case 'user_view_card'://用戶瀏覽會員卡 
  86. break
  87. case 'user_consume_card'://用戶核銷卡券 
  88. break
  89. case 'merchant_order'://微小店用戶下單付款 
  90. break
  91. default
  92. break
  93. else
  94. switch ($MsgType) { 
  95. case 'text'://文本格式 
  96. break
  97. case 'image'://圖片格式 
  98. break
  99. case 'voice'://聲音 
  100. break
  101. case 'video'://視頻 
  102. break
  103. case 'shortvideo'://小視頻 
  104. break
  105. case 'location'://上傳地理位置 
  106. break
  107. case 'link'://鏈接相應 
  108. break
  109. default
  110. break
  111. }  
  112.  
  113. //////////////////////////////////////////////////////////////////// 
  114. $keyword = trim($postObj->Content); 
  115. $time = time(); 
  116. $textTpl = "<xml> 
  117. <ToUserName><![CDATA[%s]]></ToUserName> 
  118. <FromUserName><![CDATA[%s]]></FromUserName> 
  119. <CreateTime>%s</CreateTime> 
  120. <MsgType><![CDATA[%s]]></MsgType> 
  121. <Content><![CDATA[%s]]></Content> 
  122. <FuncFlag>0</FuncFlag> 
  123. </xml>";  
  124. if(!emptyempty$keyword )){ 
  125. $msgType = "text"
  126. $contentStr = "Welcome to wechat world!"
  127. $resultStr = sprintf($textTpl$fromUsername$toUsername$time$msgType$contentStr); 
  128. echo $resultStr
  129. else
  130. echo "Input something..."
  131. //////////////////////////////////////////////////////////////////// 
  132. else
  133. echo "暫時沒有任何信息!"
  134. exit
  135.  
  136. //日志LOG 
  137. public function zcLog($errcode , $errmsg){ 
  138. $this->returnAy = array(); 
  139. $this->returnAy['errcode'] = $errcode
  140. $this->returnAy['errmsg'] = $errmsg
  141. $this->returnAy['errtime'] = date("Y-m-d H:i:s",time()); 
  142. $logfile = fopen("logfile_".date("Ymd",time()).".txt""a+"); 
  143. $txt = json_encode($this->returnAy)."/n"
  144. fwrite($logfile$txt); 
  145. fclose($logfile); 
  146. //return $this->returnAy; 
  147.  

3. [代碼]微信操作類 - 更新了自定義菜單部分

 

 
  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 appId = ""
  14. const appSecret = ""
  15. //const mchid = ""; //商戶號 
  16. //const privatekey = ""; //私鑰 
  17. public $parameters = array(); 
  18.  
  19. public function __construct(){ 
  20.  
  21.  
  22. /**************************************************** 
  23. * 微信提交API方法,返回微信指定JSON 
  24. ****************************************************/ 
  25.  
  26. public function wxHttpsRequest($url,$data = null){ 
  27. $curl = curl_init(); 
  28. curl_setopt($curl, CURLOPT_URL, $url); 
  29. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
  30. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 
  31. if (!emptyempty($data)){ 
  32. curl_setopt($curl, CURLOPT_POST, 1); 
  33. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
  34. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  35. $output = curl_exec($curl); 
  36. curl_close($curl); 
  37. return $output
  38.  
  39. /**************************************************** 
  40. * 微信帶證書提交數據 - 微信紅包使用 
  41. ****************************************************/ 
  42.  
  43. public function wxHttpsRequestPem($url$vars$second=30,$aHeader=array()){ 
  44. $ch = curl_init(); 
  45. //超時時間 
  46. curl_setopt($ch,CURLOPT_TIMEOUT,$second); 
  47. curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); 
  48. //這里設置代理,如果有的話 
  49. //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98'); 
  50. //curl_setopt($ch,CURLOPT_PROXYPORT, 8080); 
  51. curl_setopt($ch,CURLOPT_URL,$url); 
  52. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); 
  53. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); 
  54.  
  55. //以下兩種方式需選擇一種 
  56.  
  57. //第一種方法,cert 與 key 分別屬于兩個.pem文件 
  58. //默認格式為PEM,可以注釋 
  59. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); 
  60. curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/apiclient_cert.pem'); 
  61. //默認格式為PEM,可以注釋 
  62. curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); 
  63. curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/apiclient_key.pem'); 
  64.  
  65. curl_setopt($ch,CURLOPT_CAINFO,'PEM'); 
  66. curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/rootca.pem'); 
  67.  
  68. //第二種方式,兩個文件合成一個.pem文件 
  69. //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem'); 
  70.  
  71. ifcount($aHeader) >= 1 ){ 
  72. curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader); 
  73.  
  74. curl_setopt($ch,CURLOPT_POST, 1); 
  75. curl_setopt($ch,CURLOPT_POSTFIELDS,$vars); 
  76. $data = curl_exec($ch); 
  77. if($data){ 
  78. curl_close($ch); 
  79. return $data
  80. else {  
  81. $error = curl_errno($ch); 
  82. echo "call faild, errorCode:$error/n";  
  83. curl_close($ch); 
  84. return false; 
  85.  
  86. /**************************************************** 
  87. * 微信獲取AccessToken 返回指定微信公眾號的at信息 
  88. ****************************************************/ 
  89.  
  90. public function wxAccessToken($appId = NULL , $appSecret = NULL){ 
  91. $appId = is_null($appId) ? self::appId : $appId
  92. $appSecret = is_null($appSecret) ? self::appSecret : $appSecret
  93.  
  94. $data = json_decode(file_get_contents("access_token.json")); 
  95. if ($data->expire_time < time()) { 
  96. //echo $appId,$appSecret; 
  97. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret
  98. $result = $this->wxHttpsRequest($url); 
  99. //print_r($result); 
  100. $jsoninfo = json_decode($result, true); 
  101. $access_token = $jsoninfo["access_token"]; 
  102. if ($access_token) { 
  103. $data->expire_time = time() + 7000; 
  104. $data->access_token = $access_token
  105. $fp = fopen("access_token.json""w"); 
  106. fwrite($fp, json_encode($data)); 
  107. fclose($fp); 
  108. else { 
  109. $access_token = $data->access_token; 
  110. return $access_token
  111.  
  112. /**************************************************** 
  113. * 微信獲取AccessToken 返回指定微信公眾號的at信息 
  114. ****************************************************/ 
  115.  
  116. public function wxJsApiTicket($appId = NULL , $appSecret = NULL){ 
  117. $appId = is_null($appId) ? self::appId : $appId
  118. $appSecret = is_null($appSecret) ? self::appSecret : $appSecret
  119.  
  120. $data = json_decode(file_get_contents("jsapi_ticket.json")); 
  121. if ($data->expire_time < time()) {  
  122. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$this->wxAccessToken(); 
  123. $result = $this->wxHttpsRequest($url); 
  124. $jsoninfo = json_decode($result, true); 
  125. $ticket = $jsoninfo['ticket']; 
  126. if ($ticket) { 
  127. $data->expire_time = time() + 7000; 
  128. $data->jsapi_ticket = $ticket
  129. $fp = fopen("jsapi_ticket.json""w"); 
  130. fwrite($fp, json_encode($data)); 
  131. fclose($fp); 
  132. else { 
  133. $ticket = $data->jsapi_ticket; 
  134. return $ticket
  135.  
  136. /**************************************************** 
  137. * 微信通過OPENID獲取用戶信息,返回數組 
  138. ****************************************************/ 
  139.  
  140. public function wxGetUser($openId){ 
  141. $wxAccessToken = $this->wxAccessToken(); 
  142. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$wxAccessToken."&openid=".$openId."&lang=zh_CN"
  143. $result = $this->wxHttpsRequest($url); 
  144. $jsoninfo = json_decode($result, true); 
  145. return $jsoninfo
  146. }  
  147.  
  148. /**************************************************** 
  149. * 微信生成二維碼ticket 
  150. ****************************************************/ 
  151.  
  152. public function wxQrCodeTicket($jsonData){ 
  153. $wxAccessToken = $this->wxAccessToken(); 
  154. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$wxAccessToken
  155. $result = $this->wxHttpsRequest($url,$jsonData); 
  156. return $result
  157.  
  158. /**************************************************** 
  159. * 微信通過ticket生成二維碼 
  160. ****************************************************/ 
  161. public function wxQrCode($ticket){ 
  162. $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode($ticket); 
  163. return $url
  164.  
  165. /**************************************************** 
  166. * 發送自定義的模板消息 
  167. ****************************************************/ 
  168.  
  169. public function wxSetSend($touser$template_id$url$data$topcolor = '#7B68EE'){ 
  170. $template = array
  171. 'touser' => $touser
  172. 'template_id' => $template_id
  173. 'url' => $url
  174. 'topcolor' => $topcolor
  175. 'data' => $data 
  176. ); 
  177. $jsonData = json_encode($template); 
  178. $result = $this->wxSendTemplate($jsonData); 
  179. return $result
  180.  
  181. /**************************************************** 
  182. * 微信設置OAUTH跳轉URL,返回字符串信息 - SCOPE = snsapi_base //驗證時不返回確認頁面,只能獲取OPENID 
  183. ****************************************************/ 
  184.  
  185. public function wxOauthBase($redirectUrl,$state = "",$appId = NULL){ 
  186. $appId = is_null($appId) ? self::appId : $appId
  187. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_base&state=".$state."#wechat_redirect"
  188. return $url
  189.  
  190. /**************************************************** 
  191. * 微信設置OAUTH跳轉URL,返回字符串信息 - SCOPE = snsapi_userinfo //獲取用戶完整信息 
  192. ****************************************************/ 
  193.  
  194. public function wxOauthUserinfo($redirectUrl,$state = "",$appId = NULL){ 
  195. $appId = is_null($appId) ? self::appId : $appId
  196. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect"
  197. return $url
  198.  
  199. /**************************************************** 
  200. * 微信OAUTH跳轉指定URL 
  201. ****************************************************/ 
  202.  
  203. public function wxHeader($url){ 
  204. header("location:".$url); 
  205.  
  206. /**************************************************** 
  207. * 微信通過OAUTH返回頁面中獲取AT信息 
  208. ****************************************************/ 
  209.  
  210. public function wxOauthAccessToken($code,$appId = NULL , $appSecret = NULL){ 
  211. $appId = is_null($appId) ? self::appId : $appId
  212. $appSecret = is_null($appSecret) ? self::appSecret : $appSecret
  213. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code"
  214. $result = $this->wxHttpsRequest($url); 
  215. //print_r($result); 
  216. $jsoninfo = json_decode($result, true); 
  217. //$access_token = $jsoninfo["access_token"]; 
  218. return $jsoninfo;  
  219.  
  220. /**************************************************** 
  221. * 微信通過OAUTH的Access_Token的信息獲取當前用戶信息 // 只執行在snsapi_userinfo模式運行 
  222. ****************************************************/ 
  223.  
  224. public function wxOauthUser($OauthAT,$openId){ 
  225. $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$OauthAT."&openid=".$openId."&lang=zh_CN"
  226. $result = $this->wxHttpsRequest($url); 
  227. $jsoninfo = json_decode($result, true); 
  228. return $jsoninfo;  
  229.  
  230. /**************************************************** 
  231. * 創建自定義菜單 
  232. ****************************************************/ 
  233.  
  234. public function wxMenuCreate($jsonData){ 
  235. $wxAccessToken = $this->wxAccessToken(); 
  236. $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $wxAccessToken
  237. $result = $this->wxHttpsRequest($url,$jsonData); 
  238. $jsoninfo = json_decode($result, true); 
  239. return $jsoninfo;  
  240.  
  241. /**************************************************** 
  242. * 獲取自定義菜單 
  243. ****************************************************/ 
  244.  
  245. public function wxMenuGet(){ 
  246. $wxAccessToken = $this->wxAccessToken(); 
  247. $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" . $wxAccessToken
  248. $result = $this->wxHttpsRequest($url); 
  249. $jsoninfo = json_decode($result, true); 
  250. return $jsoninfo
  251.  
  252. /**************************************************** 
  253. * 刪除自定義菜單 
  254. ****************************************************/ 
  255.  
  256. public function wxMenuDelete(){ 
  257. $wxAccessToken = $this->wxAccessToken(); 
  258. $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . $wxAccessToken
  259. $result = $this->wxHttpsRequest($url); 
  260. $jsoninfo = json_decode($result, true); 
  261. return $jsoninfo
  262.  
  263. /**************************************************** 
  264. * 獲取第三方自定義菜單 
  265. ****************************************************/ 
  266.  
  267. public function wxMenuGetInfo(){ 
  268. $wxAccessToken = $this->wxAccessToken(); 
  269. $url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" . $wxAccessToken
  270. $result = $this->wxHttpsRequest($url); 
  271. $jsoninfo = json_decode($result, true); 
  272. return $jsoninfo
  273.  
  274. /***************************************************** 
  275. * 生成隨機字符串 - 最長為32位字符串 
  276. *****************************************************/ 
  277. public function wxNonceStr($length = 16, $type = FALSE) { 
  278. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  279. $str = ""
  280. for ($i = 0; $i < $length$i++) { 
  281. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 
  282. if($type == TRUE){ 
  283. return strtoupper(md5(time() . $str)); 
  284. else { 
  285. return $str
  286.  
  287. /******************************************************* 
  288. * 微信商戶訂單號 - 最長28位字符串 
  289. *******************************************************/ 
  290.  
  291. public function wxMchBillno($mchid = NULL) { 
  292. if(is_null($mchid)){ 
  293. if(self::mchid == "" || is_null(self::mchid)){ 
  294. $mchid = time(); 
  295. else
  296. $mchid = self::mchid; 
  297. else
  298. $mchid = substr(addslashes($mchid),0,10); 
  299. return date("Ymd",time()).time().$mchid
  300.  
  301. /******************************************************* 
  302. * 微信格式化數組變成參數格式 - 支持url加密 
  303. *******************************************************/ 
  304.  
  305. public function wxSetParam($parameters){ 
  306. if(is_array($parameters) && !emptyempty($parameters)){ 
  307. $this->parameters = $parameters
  308. return $this->parameters; 
  309. else
  310. return array(); 
  311.  
  312. /******************************************************* 
  313. * 微信格式化數組變成參數格式 - 支持url加密 
  314. *******************************************************/ 
  315.  
  316. public function wxFormatArray($parameters = NULL, $urlencode = FALSE){ 
  317. if(is_null($parameters)){ 
  318. $parameters = $this->parameters; 
  319. $restr = "";//初始化空 
  320. ksort($parameters);//排序參數 
  321. foreach ($parameters as $k => $v){//循環定制參數 
  322. if (null != $v && "null" != $v && "sign" != $k) { 
  323. if($urlencode){//如果參數需要增加URL加密就增加,不需要則不需要 
  324. $v = urlencode($v); 
  325. $restr .= $k . "=" . $v . "&";//返回完整字符串 
  326. if (strlen($restr) > 0) {//如果存在數據則將最后“&”刪除 
  327. $restr = substr($restr, 0, strlen($restr)-1); 
  328. return $restr;//返回字符串 
  329.  
  330. /******************************************************* 
  331. * 微信MD5簽名生成器 - 需要將參數數組轉化成為字符串[wxFormatArray方法] 
  332. *******************************************************/ 
  333. public function wxMd5Sign($content$privatekey){ 
  334. try { 
  335. if (is_null($privatekey)) { 
  336. throw new Exception("財付通簽名key不能為空!"); 
  337. if (is_null($content)) { 
  338. throw new Exception("財付通簽名內容不能為空"); 
  339. $signStr = $content . "&key=" . $privatekey
  340. return strtoupper(md5($signStr)); 
  341. catch (Exception $e
  342. die($e->getMessage()); 
  343.  
  344. /******************************************************* 
  345. * 微信Sha1簽名生成器 - 需要將參數數組轉化成為字符串[wxFormatArray方法] 
  346. *******************************************************/ 
  347. public function wxSha1Sign($content){ 
  348. try { 
  349. if (is_null($content)) { 
  350. throw new Exception("簽名內容不能為空"); 
  351. //$signStr = $content; 
  352. return sha1($content); 
  353. catch (Exception $e
  354. die($e->getMessage()); 
  355.  
  356. /******************************************************* 
  357. * 微信jsApi整合方法 - 通過調用此方法獲得jsapi數據 
  358. *******************************************************/ 
  359. public function wxJsapiPackage(){ 
  360. $jsapi_ticket = $this->wxJsApiTicket(); 
  361.  
  362. // 注意 URL 一定要動態獲取,不能 hardcode. 
  363. $protocol = (!emptyempty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"
  364. $url = $protocol.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]; 
  365.  
  366. $timestamp = time(); 
  367. $nonceStr = $this->wxNonceStr(); 
  368.  
  369. $signPackage = array
  370. "jsapi_ticket" => $jsapi_ticket
  371. "nonceStr" => $nonceStr
  372. "timestamp" => $timestamp
  373. "url" => $url 
  374. );  
  375.  
  376. // 這里參數的順序要按照 key 值 ASCII 碼升序排序 
  377. $rawString = "jsapi_ticket=$jsapi_ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"
  378.  
  379. //$rawString = $this->wxFormatArray($signPackage); 
  380. $signature = $this->wxSha1Sign($rawString); 
  381.  
  382. $signPackage['signature'] = $signature
  383. $signPackage['rawString'] = $rawString
  384. $signPackage['appId'] = self::appId; 
  385.  
  386. return $signPackage
  387.  
  388.  
  389. /******************************************************* 
  390. * 將數組解析XML - 微信紅包接口 
  391. *******************************************************/ 
  392. public function wxArrayToXml($parameters = NULL){ 
  393. if(is_null($parameters)){ 
  394. $parameters = $this->parameters; 
  395.  
  396. if(!is_array($parameters) || emptyempty($parameters)){ 
  397. die("參數不為數組無法解析"); 
  398.  
  399. $xml = "<xml>"
  400. foreach ($arr as $key=>$val
  401. if (is_numeric($val)) 
  402. $xml.="<".$key.">".$val."</".$key.">";  
  403. else 
  404. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";  
  405. $xml.="</xml>"
  406. return $xml;  
  407.  
  408. /******************************************************* 
  409. * 微信卡券:上傳LOGO - 需要改寫動態功能 
  410. *******************************************************/ 
  411. public function wxCardUpdateImg() { 
  412. $wxAccessToken = $this->wxAccessToken(); 
  413. //$data['access_token'] = $wxAccessToken; 
  414. $data['buffer'] = '@D://workspace//htdocs//yky_test//logo.jpg'
  415. $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$wxAccessToken
  416. $result = $this->wxHttpsRequest($url,$data); 
  417. $jsoninfo = json_decode($result, true); 
  418. return $jsoninfo
  419. //array(1) { ["url"]=> string(121) "http://mmbiz.qpic.cn/mmbiz/ibuYxPHqeXePNTW4ATKyias1Cf3zTKiars9PFPzF1k5icvXD7xW0kXUAxHDzkEPd9micCMCN0dcTJfW6Tnm93MiaAfRQ/0" }  
  420.  
  421. /******************************************************* 
  422. * 微信卡券:獲取顏色 
  423. *******************************************************/ 
  424. public function wxCardColor(){ 
  425. $wxAccessToken = $this->wxAccessToken(); 
  426. $url = "https://api.weixin.qq.com/card/getcolors?access_token=".$wxAccessToken
  427. $result = $this->wxHttpsRequest($url); 
  428. $jsoninfo = json_decode($result, true); 
  429. return $jsoninfo
  430.  
  431. /******************************************************* 
  432. * 微信卡券:創建卡券 
  433. *******************************************************/ 
  434. public function wxCardCreated($jsonData) { 
  435. $wxAccessToken = $this->wxAccessToken(); 
  436. $url = "https://api.weixin.qq.com/card/create?access_token=" . $wxAccessToken
  437. $result = $this->wxHttpsRequest($url,$jsonData); 
  438. $jsoninfo = json_decode($result, true); 
  439. return $jsoninfo
  440.  
  441. /******************************************************* 
  442. * 微信卡券:JSAPI 卡券Package - 基礎參數沒有附帶任何值 - 再生產環境中需要根據實際情況進行修改 
  443. *******************************************************/ 
  444. public function wxCardPackage($cardId){ 
  445. $timestamp = time(); 
  446. $api_ticket = $this->wxJsApiTicket(); 
  447. $cardId = $cardId
  448. $arrays = array($api_ticket,$timestamp,$cardId); 
  449. sort($arrays); 
  450. $string = sha1(implode("",$arrays)); 
  451.  
  452. $resultArray['card_id'] = $cardId
  453. $resultArray['card_ext'] = array(); 
  454. $resultArray['card_ext']['openid'] = 'oOmn4s9MiwqHSNNvPn0dBtU23toA'
  455. $resultArray['card_ext']['timestamp'] = $timestamp
  456. $resultArray['card_ext']['signature'] = $string
  457.  
  458. return $resultArray
  459.  
  460.  

4. [代碼]微信JSAPI

 

 
  1. <?php 
  2. require_once 'lib.inc.php'
  3. $wx = new WxApi(); 
  4. //通過網頁獲取openid 
  5. //if(!isset($_GET['code'])){ 
  6. // header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=".WxApi::appId."&redirect_uri=http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."&response_type=code&scope=snsapi_base&state=1#wechat_redirect"); 
  7. //} 
  8. //else{ 
  9. // $CODE = $_GET['code']; 
  10. // $Info = $wx->wxOauthAccessToken($CODE); 
  11. //print_r($Info); 
  12. // $openId = $Info['openid'];  
  13. //} 
  14. //////////////////////////////////////////// 
  15.  
  16. $signPackage = $wx->wxJsapiPackage(); 
  17. //print_r($signPackage); 
  18. $kqInfo = $wx->wxCardPackage(""); 
  19. $listInfo = $wx->wxCardListPackage(); 
  20. ?> 
  21. <html> 
  22. <head> 
  23. <title>JSAPI接口測試</title> 
  24. <meta charset="UTF-8"
  25. <meta name="viewport" content="width=device-width, initial-scale=1.0"
  26.  
  27. <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script> 
  28. <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> 
  29. </head> 
  30. <body> 
  31. <div> 
  32. <input type="button" id="batchAddCard" name="batchAddCard" value="添加卡券" /><br /> 
  33. <input type="button" id="openCard" name="openCard" value="拉起卡券庫" /><br /> 
  34. <input type="button" id="ShareTimeLine" name="ShareTimeLine" value="分享朋友圈" /><br /> 
  35. <div id="showInfo"
  36.  
  37. </div> 
  38. </div> 
  39.  
  40. <script> 
  41. wx.config({ 
  42. debug: false, 
  43. appId: '<?php echo $signPackage["appId"];?>'
  44. timestamp: <?php echo $signPackage["timestamp"];?>, 
  45. nonceStr: '<?php echo $signPackage["nonceStr"];?>'
  46. signature: '<?php echo $signPackage["signature"];?>'
  47. jsApiList: [ 
  48. // 所有要調用的 API 都要加到這個列表中 
  49. 'onMenuShareTimeline'
  50. 'onMenuShareAppMessage'
  51. 'addCard'
  52. 'openCard' 
  53. }); 
  54.  
  55. wx.ready(function () { 
  56. // 在這里調用 API 
  57. wx.onMenuShareAppMessage({ 
  58. title: '互聯網之子'
  59. desc: '在長大的過程中,我才慢慢發現,我身邊的所有事,別人跟我說的所有事,那些所謂本來如此,注定如此的事,它們其實沒有非得如此,事情是可以改變的。更重要的是,有些事既然錯了,那就該做出改變。'
  60. link: 'http://movie.douban.com/subject/25785114/'
  61. imgUrl: 'http://demo.open.weixin.qq.com/jssdk/images/p2166127561.jpg'
  62. trigger: function (res) { 
  63. // 不要嘗試在trigger中使用ajax異步請求修改本次分享的內容,因為客戶端分享操作是一個同步操作,這時候使用ajax的回包會還沒有返回 
  64. alert('用戶點擊發送給朋友'); 
  65. }, 
  66. success: function (res) { 
  67. alert('已分享'); 
  68. }, 
  69. cancel: function (res) { 
  70. alert('已取消'); 
  71. }, 
  72. fail: function (res) { 
  73. alert(JSON.stringify(res)); 
  74. }); 
  75.  
  76. document.querySelector('#ShareTimeLine').onclick = function () { 
  77. wx.onMenuShareTimeline({ 
  78. title: '互聯網之子'
  79. link: 'http://movie.douban.com/subject/25785114/'
  80. imgUrl: 'http://demo.open.weixin.qq.com/jssdk/images/p2166127561.jpg'
  81. trigger: function (res) { 
  82. // 不要嘗試在trigger中使用ajax異步請求修改本次分享的內容,因為客戶端分享操作是一個同步操作,這時候使用ajax的回包會還沒有返回 
  83. alert('用戶點擊分享到朋友圈'); 
  84. }, 
  85. success: function (res) { 
  86. alert('已分享'); 
  87. }, 
  88. cancel: function (res) { 
  89. alert('已取消'); 
  90. }, 
  91. fail: function (res) { 
  92. alert(JSON.stringify(res)); 
  93. }); 
  94. };  
  95.  
  96. document.querySelector('#batchAddCard').onclick = function () { 
  97. wx.addCard({ 
  98. cardList: [ 
  99. cardId: 'p7G0Cj_1HGF2nijO4sTlVTzawFhI'
  100. cardExt: '{"timestamp":"<?php echo $kqInfo['cardExt']['timestamp'];?>", "signature":"<?php echo $kqInfo['cardExt']['signature'];?>"}' 
  101. ], 
  102. success: function (res) { 
  103. var cardList = res.cardList; // 添加的卡券列表信息 
  104. alert(cardList); 
  105. }, 
  106. cancel: function (res) { 
  107. alert('已取消'); 
  108. }, 
  109. fail: function (res) { 
  110. alert(JSON.stringify(res)); 
  111. }); 
  112. }; 
  113.  
  114. var shareData = { 
  115. title: '微信JS-SDK Demo'
  116. desc: '微信JS-SDK,幫助第三方為用戶提供更優質的移動web服務'
  117. link: 'http://demo.open.weixin.qq.com/jssdk/'
  118. imgUrl: 'http://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRt8Qia4lv7k3M9J1SKqKCImxJCt7j9rHYicKDI45jRPBxdzdyREWnk0ia0N5TMnMfth7SdxtzMvVgXg/0' 
  119. }; 
  120.  
  121. wx.onMenuShareAppMessage(shareData); 
  122.  
  123. wx.onMenuShareTimeline(shareData); 
  124. }); 
  125.  
  126. var readyFunc = function onBridgeReady() { 
  127. // 綁定關注事件 
  128. document.querySelector('#openCard').addEventListener('click'
  129. function(e) { 
  130. WeixinJSBridge.invoke('chooseCard', { 
  131. "app_id""<?php echo $listInfo['app_id']?>"
  132. "location_id "''
  133. "sign_type""SHA1"
  134. "card_sign""<?php echo $listInfo['card_sign']?>"
  135. "card_id""<?php echo $listInfo['card_id']?>"
  136. "card_type""<?php echo $listInfo['card_type']?>"
  137. "time_stamp""<?php echo $listInfo['time_stamp']?>"
  138. "nonce_str""<?php echo $listInfo['nonce_str']?>" 
  139. }, 
  140. function(res) { 
  141. alert(res.err_msg + res.choose_card_info); 
  142. $("#showInfo").emptyempty().append(res.err_msg + res.choose_card_info); 
  143. }); 
  144. }); 
  145.  
  146. if (typeof WeixinJSBridge === "undefined") { 
  147. document.addEventListener('WeixinJSBridgeReady', readyFunc, false); 
  148. else { 
  149. readyFunc(); 
  150.  
  151. </script> 
  152. </body> 
  153. </html> 

5. [代碼]創建卡券

 

 
  1. $kqinfo = array("card" => array()); 
  2. $kqinfo['card']['card_type'] = 'GENERAL_COUPON'
  3. $kqinfo['card']['general_coupon'] = array('base_info' => array(), 'default_detail' => array()); 
  4. $kqinfo['card']['general_coupon']['base_info']['logo_url'] = 'URL'
  5. $kqinfo['card']['general_coupon']['base_info']['code_type'] = 'CODE_TYPE_QRCODE'
  6. $kqinfo['card']['general_coupon']['base_info']['brand_name'] = ''
  7. $kqinfo['card']['general_coupon']['base_info']['title'] = '測試卡券'
  8. $kqinfo['card']['general_coupon']['base_info']['color'] = 'Color030'
  9. $kqinfo['card']['general_coupon']['base_info']['notice'] = '測試測試測試'
  10. $kqinfo['card']['general_coupon']['base_info']['description'] = '這是一張優惠券'
  11. $kqinfo['card']['general_coupon']['base_info']['date_info']['type'] = 1; 
  12. $kqinfo['card']['general_coupon']['base_info']['date_info']['begin_timestamp'] = time(); 
  13. $kqinfo['card']['general_coupon']['base_info']['date_info']['end_timestamp'] = time() + 100 * 24 * 3600; 
  14. $kqinfo['card']['general_coupon']['base_info']['sku']['quantity'] = 100000; 
  15. $kqinfo['card']['general_coupon']['default_detail'] = '測試數據/n測試數據/n測試數據'
  16.  
  17. //var_dump($kqinfo); 
  18. //$kqinfo = json_encode($kqinfo); 
  19. $kqinfo = C::enJson($kqinfo); 
  20.  
  21. //print_r( $kqinfo); 
  22. //$resultData = $wx->wxCardCreated($kqinfo); 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲少妇中文在线| 久久6免费高清热精品| 亚洲国产美女久久久久| 精品久久久免费| 日韩精品极品视频| 热草久综合在线| 欧美精品videosex性欧美| 一区二区在线免费视频| 亚洲色图美腿丝袜| 亚洲第一区中文99精品| 欧美一区二区三区四区在线| 久久在线精品视频| 成人福利免费观看| 国产成人极品视频| 国产精品成人v| 亚洲男人的天堂在线播放| 久久精品免费电影| 欧美日韩国产999| 成人信息集中地欧美| 精品国产一区二区在线| 国产精品久久国产精品99gif| 理论片在线不卡免费观看| 亚洲男人av在线| 亚洲欧美日韩网| 欧美性生交xxxxxdddd| 亚洲视频国产视频| 国产一区二区三区在线观看视频| 欧美电影免费观看网站| 成人精品网站在线观看| 91在线精品播放| 夜夜躁日日躁狠狠久久88av| 91成人国产在线观看| 日韩成人在线视频观看| 欧美激情精品久久久久久黑人| 91系列在线播放| 久久成人精品一区二区三区| 成人欧美一区二区三区在线| 色综合伊人色综合网| 欧美激情中文字幕乱码免费| 国产一区二区三区在线播放免费观看| 欧美精品aaa| 久久久久久欧美| 欧亚精品在线观看| 欧美一区亚洲一区| 色系列之999| 国产中文日韩欧美| 亚洲国产小视频| 日韩精品视频在线| 久久久精品2019中文字幕神马| 欧美一区二区大胆人体摄影专业网站| 日韩亚洲国产中文字幕| 欧美精品中文字幕一区| 成人黄色短视频在线观看| 欧美在线视频一区二区| 色综合天天狠天天透天天伊人| 精品偷拍各种wc美女嘘嘘| 国产精品美乳一区二区免费| 国产精品爱啪在线线免费观看| 国产v综合v亚洲欧美久久| 亚洲国产精品嫩草影院久久| 欧美日韩国产丝袜另类| 国产午夜一区二区| 日韩中文字幕在线| 欧美日韩免费区域视频在线观看| 久久视频在线免费观看| 欧美丝袜一区二区| 国外成人免费在线播放| 久久成年人免费电影| 久久久久久久影院| 激情成人在线视频| 亚洲高清福利视频| 久久精品色欧美aⅴ一区二区| 97久久国产精品| 中文字幕亚洲无线码a| 国内外成人免费激情在线视频| 国产欧美一区二区三区久久| 亚洲欧美中文字幕在线一区| 成人情趣片在线观看免费| 国产欧美在线观看| 不卡av日日日| 久久久人成影片一区二区三区观看| 欧美日韩亚洲激情| 国产视频综合在线| 亚洲精品不卡在线| 国产精品小说在线| 久久久久国产视频| 亚洲国产欧美久久| 91免费在线视频网站| 深夜福利日韩在线看| 成人在线观看视频网站| 久久久久久高潮国产精品视| 欧美日韩在线视频一区二区| 国产精品1234| 97视频在线观看免费高清完整版在线观看| 亚洲国产高清自拍| 97国产精品免费视频| 国产日韩中文字幕在线| 欧美成人久久久| 欧美午夜片欧美片在线观看| 最近2019年好看中文字幕视频| 91国产视频在线| 午夜剧场成人观在线视频免费观看| 欧美亚洲免费电影| 性日韩欧美在线视频| 成人免费视频在线观看超级碰| 亚洲精品福利在线观看| 国产精品视频白浆免费视频| 日本在线观看天堂男亚洲| 欧美中文字幕在线视频| 日日噜噜噜夜夜爽亚洲精品| 欧美激情在线观看| 久久精品在线播放| 日韩精品日韩在线观看| 91高潮在线观看| 欧美黄色片在线观看| 奇米4444一区二区三区| 亚洲欧洲激情在线| 欧美一级在线亚洲天堂| 欧美第一页在线| 国产啪精品视频网站| 国产va免费精品高清在线| 国产日产久久高清欧美一区| 欧美精品久久一区二区| 精品久久久国产| 国产精品久久久久久久7电影| 亚洲免费伊人电影在线观看av| 最近的2019中文字幕免费一页| 欧美精品国产精品日韩精品| 欧美重口另类videos人妖| 欧美激情久久久久| 欧美丰满老妇厨房牲生活| 中文字幕日韩有码| 欧美日韩在线视频首页| 欧美性极品xxxx娇小| 中文字幕亚洲精品| 2025国产精品视频| 日韩在线观看免费高清完整版| 欧美日韩一区二区精品| 亚洲网在线观看| 国产精品日韩精品| 国产精品狼人色视频一区| 黑人巨大精品欧美一区二区三区| 在线观看中文字幕亚洲| 亚洲偷熟乱区亚洲香蕉av| 一本色道久久88综合日韩精品| 在线视频欧美性高潮| 亚洲综合自拍一区| 久久免费国产视频| 97国产成人精品视频| 国产精品视频在线播放| 国产精品私拍pans大尺度在线| 97国产在线视频| 国产日韩亚洲欧美| 插插插亚洲综合网| 亚洲欧美日韩一区二区三区在线| 久久久久亚洲精品国产| 亚洲精品国产电影| xxxxxxxxx欧美| 国模私拍一区二区三区| 久久久噜噜噜久久中文字免| 精品久久久久久亚洲精品| 91麻豆国产语对白在线观看| 一本色道久久88精品综合| 国产一区二区三区在线播放免费观看|