php 官方微信接口大全:微信支付、微信红包、微信摇一摇、微信小店的完整代码

前端之家收集整理的这篇文章主要介绍了php 官方微信接口大全:微信支付、微信红包、微信摇一摇、微信小店的完整代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣PHP 官方微信接口大全:微信支付、微信红包、微信摇一摇、微信小店的完整代码的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。<br>
微信入口绑定,微信事件处理,微信API全部操作包含在这些文件中。
内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券接口/微信小店接口/JSAPI
  1. /**
  2. * @param
  3. * @author 编程之家 jb51.cc jb51.cc
  4. **/
  5. class WxApi {
  6. const appId = "";
  7. const appSecret = "";
  8. const mchid = ""; //商户号
  9. const privatekey = ""; //私钥
  10. public $parameters = array();
  11. public $jsApiTicket = NULL;
  12. public $jsApiTime = NULL;
  13. public function __construct(){
  14. }
  15. /****************************************************
  16. * 微信提交API方法,返回微信指定JSON
  17. ****************************************************/
  18. public function wxHttpsRequest($url,$data = null){
  19. $curl = curl_init();
  20. curl_setopt($curl,CURLOPT_URL,$url);
  21. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
  22. curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);
  23. if (!empty($data)){
  24. curl_setopt($curl,CURLOPT_POST,1);
  25. curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
  26. }
  27. curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
  28. $output = curl_exec($curl);
  29. curl_close($curl);
  30. return $output;
  31. }
  32. /****************************************************
  33. * 微信带证书提交数据 - 微信红包使用
  34. ****************************************************/
  35. public function wxHttpsRequestPem($url,$vars,$second=30,$aHeader=array()){
  36. $ch = curl_init();
  37. //超时时间
  38. curl_setopt($ch,CURLOPT_TIMEOUT,$second);
  39. curl_setopt($ch,1);
  40. //这里设置代理,如果有的话
  41. //curl_setopt($ch,CURLOPT_PROXY,'10.206.30.98');
  42. //curl_setopt($ch,CURLOPT_PROXYPORT,8080);
  43. curl_setopt($ch,$url);
  44. curl_setopt($ch,false);
  45. curl_setopt($ch,false);
  46. //以下两种方式需选择一种
  47. //第一种方法,cert 与 key 分别属于两个.pem文件
  48. //默认格式为PEM,可以注释
  49. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  50. curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/apiclient_cert.pem');
  51. //默认格式为PEM,可以注释
  52. curl_setopt($ch,CURLOPT_SSLKEYTYPE,CURLOPT_SSLKEY,getcwd().'/apiclient_key.pem');
  53. curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/rootca.pem');
  54. //第二种方式,两个文件合成一个.pem文件
  55. //curl_setopt($ch,getcwd().'/all.pem');
  56. if( count($aHeader) >= 1 ){
  57. curl_setopt($ch,CURLOPT_HTTPHEADER,$aHeader);
  58. }
  59. curl_setopt($ch,1);
  60. curl_setopt($ch,$vars);
  61. $data = curl_exec($ch);
  62. if($data){
  63. curl_close($ch);
  64. return $data;
  65. }
  66. else {
  67. $error = curl_errno($ch);
  68. echo "call faild,errorCode:$error\n";
  69. curl_close($ch);
  70. return false;
  71. }
  72. }
  73. /****************************************************
  74. * 微信获取AccessToken 返回指定微信公众号的at信息
  75. ****************************************************/
  76. public function wxAccessToken($appId = NULL,$appSecret = NULL){
  77. $appId = is_null($appId) ? self::appId : $appId;
  78. $appSecret = is_null($appSecret) ? self::appSecret : $appSecret;
  79. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
  80. $result = $this->wxHttpsRequest($url);
  81. //print_r($result);
  82. $jsoninfo = json_decode($result,true);
  83. $access_token = $jsoninfo["access_token"];
  84. return $access_token;
  85. }
  86. /****************************************************
  87. * 微信获取ApiTicket 返回指定微信公众号的at信息
  88. ****************************************************/
  89. public function wxJsApiTicket($appId = NULL,$appSecret = NULL){
  90. $appId = is_null($appId) ? self::appId : $appId;
  91. $appSecret = is_null($appSecret) ? self::appSecret : $appSecret;
  92. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$this->wxAccessToken();
  93. $result = $this->wxHttpsRequest($url);
  94. $jsoninfo = json_decode($result,true);
  95. $ticket = $jsoninfo['ticket'];
  96. //echo $ticket . "<br />";
  97. return $ticket;
  98. }
  99. public function wxVerifyJsApiTicket($appId = NULL,$appSecret = NULL){
  100. if(!empty($this->jsApiTime) && intval($this->jsApiTime) > time() && !empty($this->jsApiTicket)){
  101. $ticket = $this->jsApiTicket;
  102. }
  103. else{
  104. $ticket = $this->wxJsApiTicket($appId,$appSecret);
  105. $this->jsApiTicket = $ticket;
  106. $this->jsApiTime = time() + 7200;
  107. }
  108. return $ticket;
  109. }
  110. /****************************************************
  111. * 微信通过OPENID获取用户信息,返回数组
  112. ****************************************************/
  113. public function wxGetUser($openId){
  114. $wxAccessToken = $this->wxAccessToken();
  115. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$wxAccessToken."&openid=".$openId."&lang=zh_CN";
  116. $result = $this->wxHttpsRequest($url);
  117. $jsoninfo = json_decode($result,true);
  118. return $jsoninfo;
  119. }
  120. /****************************************************
  121. * 微信生成二维码ticket
  122. ****************************************************/
  123. public function wxQrCodeTicket($jsonData){
  124. $wxAccessToken = $this->wxAccessToken();
  125. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$wxAccessToken;
  126. $result = $this->wxHttpsRequest($url,$jsonData);
  127. return $result;
  128. }
  129. /****************************************************
  130. * 微信通过ticket生成二维码
  131. ****************************************************/
  132. public function wxQrCode($ticket){
  133. $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode($ticket);
  134. return $url;
  135. }
  136. /****************************************************
  137. * 微信通过指定模板信息发送给指定用户,发送完成后返回指定JSON数据
  138. ****************************************************/
  139. public function wxSendTemplate($jsonData){
  140. $wxAccessToken = $this->wxAccessToken();
  141. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$wxAccessToken;
  142. $result = $this->wxHttpsRequest($url,$jsonData);
  143. return $result;
  144. }
  145. /****************************************************
  146. * 发送自定义的模板消息
  147. ****************************************************/
  148. public function wxSetSend($touser,$template_id,$url,$data,$topcolor = '#7B68EE'){
  149. $template = array(
  150. 'touser' => $touser,'template_id' => $template_id,'url' => $url,'topcolor' => $topcolor,'data' => $data
  151. );
  152. $jsonData = urldecode(json_encode($template));
  153. echo $jsonData;
  154. $result = $this->wxSendTemplate($jsonData);
  155. return $result;
  156. }
  157. /****************************************************
  158. * 微信设置OAUTH跳转URL,返回字符串信息 - SCOPE = snsapi_base //验证时不返回确认页面,只能获取OPENID
  159. ****************************************************/
  160. public function wxOauthBase($redirectUrl,$state = "",$appId = NULL){
  161. $appId = is_null($appId) ? self::appId : $appId;
  162. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_base&state=".$state."#wechat_redirect";
  163. return $url;
  164. }
  165. /****************************************************
  166. * 微信设置OAUTH跳转URL,返回字符串信息 - SCOPE = snsapi_userinfo //获取用户完整信息
  167. ****************************************************/
  168. public function wxOauthUserinfo($redirectUrl,$appId = NULL){
  169. $appId = is_null($appId) ? self::appId : $appId;
  170. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect";
  171. return $url;
  172. }
  173. /****************************************************
  174. * 微信OAUTH跳转指定URL
  175. ****************************************************/
  176. public function wxHeader($url){
  177. header("location:".$url);
  178. }
  179. /****************************************************
  180. * 微信通过OAUTH返回页面获取AT信息
  181. ****************************************************/
  182. public function wxOauthAccessToken($code,$appId = NULL,$appSecret = NULL){
  183. $appId = is_null($appId) ? self::appId : $appId;
  184. $appSecret = is_null($appSecret) ? self::appSecret : $appSecret;
  185. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code";
  186. $result = $this->wxHttpsRequest($url);
  187. //print_r($result);
  188. $jsoninfo = json_decode($result,true);
  189. //$access_token = $jsoninfo["access_token"];
  190. return $jsoninfo;
  191. }
  192. /****************************************************
  193. * 微信通过OAUTH的Access_Token的信息获取当前用户信息 // 只执行在snsapi_userinfo模式运行
  194. ****************************************************/
  195. public function wxOauthUser($OauthAT,$openId){
  196. $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$OauthAT."&openid=".$openId."&lang=zh_CN";
  197. $result = $this->wxHttpsRequest($url);
  198. $jsoninfo = json_decode($result,true);
  199. return $jsoninfo;
  200. }
  201. /****************************************************
  202. * 创建自定义菜单
  203. ****************************************************/
  204. public function wxMenuCreate($jsonData){
  205. $wxAccessToken = $this->wxAccessToken();
  206. $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $wxAccessToken;
  207. $result = $this->wxHttpsRequest($url,$jsonData);
  208. $jsoninfo = json_decode($result,true);
  209. return $jsoninfo;
  210. }
  211. /****************************************************
  212. * 获取自定义菜单
  213. ****************************************************/
  214. public function wxMenuGet(){
  215. $wxAccessToken = $this->wxAccessToken();
  216. $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" . $wxAccessToken;
  217. $result = $this->wxHttpsRequest($url);
  218. $jsoninfo = json_decode($result,true);
  219. return $jsoninfo;
  220. }
  221. /****************************************************
  222. * 删除自定义菜单
  223. ****************************************************/
  224. public function wxMenuDelete(){
  225. $wxAccessToken = $this->wxAccessToken();
  226. $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . $wxAccessToken;
  227. $result = $this->wxHttpsRequest($url);
  228. $jsoninfo = json_decode($result,true);
  229. return $jsoninfo;
  230. }
  231. /****************************************************
  232. * 获取第三方自定义菜单
  233. ****************************************************/
  234. public function wxMenuGetInfo(){
  235. $wxAccessToken = $this->wxAccessToken();
  236. $url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" . $wxAccessToken;
  237. $result = $this->wxHttpsRequest($url);
  238. $jsoninfo = json_decode($result,true);
  239. return $jsoninfo;
  240. }
  241. /****************************************************
  242. * 微信客服接口 - Add 添加客服人员
  243. ****************************************************/
  244. public function wxServiceAdd($jsonData){
  245. $wxAccessToken = $this->wxAccessToken();
  246. $url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=" . $wxAccessToken;
  247. $result = $this->wxHttpsRequest($url,true);
  248. return $jsoninfo;
  249. }
  250. /****************************************************
  251. * 微信客服接口 - Update 编辑客服人员
  252. ****************************************************/
  253. public function wxServiceUpdate($jsonData){
  254. $wxAccessToken = $this->wxAccessToken();
  255. $url = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=" . $wxAccessToken;
  256. $result = $this->wxHttpsRequest($url,true);
  257. return $jsoninfo;
  258. }
  259. /****************************************************
  260. * 微信客服接口 - Delete 删除客服人员
  261. ****************************************************/
  262. public function wxServiceDelete($jsonData){
  263. $wxAccessToken = $this->wxAccessToken();
  264. $url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=" . $wxAccessToken;
  265. $result = $this->wxHttpsRequest($url,true);
  266. return $jsoninfo;
  267. }
  268. /*******************************************************
  269. * 微信客服接口 - 上传头像
  270. *******************************************************/
  271. public function wxServiceUpdateCover($kf_account,$media = '') {
  272. $wxAccessToken = $this->wxAccessToken();
  273. //$data['access_token'] = $wxAccessToken;
  274. $data['media'] = '@D:\\workspace\\htdocs\\yky_test\\logo.jpg';
  275. $url = "https:// api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=".$wxAccessToken."&kf_account=".$kf_account;
  276. $result = $this->wxHttpsRequest($url,$data);
  277. $jsoninfo = json_decode($result,true);
  278. return $jsoninfo;
  279. }
  280. /****************************************************
  281. * 微信客服接口 - 获取客服列表
  282. ****************************************************/
  283. public function wxServiceList(){
  284. $wxAccessToken = $this->wxAccessToken();
  285. $url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" . $wxAccessToken;
  286. $result = $this->wxHttpsRequest($url);
  287. $jsoninfo = json_decode($result,true);
  288. return $jsoninfo;
  289. }
  290. /****************************************************
  291. * 微信客服接口 - 获取在线客服接待信息
  292. ****************************************************/
  293. public function wxServiceOnlineList(){
  294. $wxAccessToken = $this->wxAccessToken();
  295. $url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=" . $wxAccessToken;
  296. $result = $this->wxHttpsRequest($url);
  297. $jsoninfo = json_decode($result,true);
  298. return $jsoninfo;
  299. }
  300. /****************************************************
  301. * 微信客服接口 - 客服发送信息
  302. ****************************************************/
  303. public function wxServiceSend($jsonData){
  304. $wxAccessToken = $this->wxAccessToken();
  305. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $wxAccessToken;
  306. $result = $this->wxHttpsRequest($url,true);
  307. return $jsoninfo;
  308. }
  309. /****************************************************
  310. * 微信客服会话接口 - 创建会话
  311. ****************************************************/
  312. public function wxServiceSessionAdd($jsonData){
  313. $wxAccessToken = $this->wxAccessToken();
  314. $url = "https://api.weixin.qq.com/customservice/kfsession/create?access_token=" . $wxAccessToken;
  315. $result = $this->wxHttpsRequest($url,true);
  316. return $jsoninfo;
  317. }
  318. /****************************************************
  319. * 微信客服会话接口 - 关闭会话
  320. ****************************************************/
  321. public function wxServiceSessionClose(){
  322. $wxAccessToken = $this->wxAccessToken();
  323. $url = "https://api.weixin.qq.com/customservice/kfsession/close?access_token=" . $wxAccessToken;
  324. $result = $this->wxHttpsRequest($url);
  325. $jsoninfo = json_decode($result,true);
  326. return $jsoninfo;
  327. }
  328. /****************************************************
  329. * 微信客服会话接口 - 获取会话
  330. ****************************************************/
  331. public function wxServiceSessionGet($openId){
  332. $wxAccessToken = $this->wxAccessToken();
  333. $url = "https://api.weixin.qq.com/customservice/kfsession/getsession?access_token=".$wxAccessToken."&openid=" . $openId;
  334. $result = $this->wxHttpsRequest($url);
  335. $jsoninfo = json_decode($result,true);
  336. return $jsoninfo;
  337. }
  338. /****************************************************
  339. * 微信客服会话接口 - 获取会话列表
  340. ****************************************************/
  341. public function wxServiceSessionList($kf_account){
  342. $wxAccessToken = $this->wxAccessToken();
  343. $url = "https://api.weixin.qq.com/customservice/kfsession/getsessionlist?access_token=".$wxAccessToken."&kf_account=" . $kf_account ;
  344. $result = $this->wxHttpsRequest($url);
  345. $jsoninfo = json_decode($result,true);
  346. return $jsoninfo;
  347. }
  348. /****************************************************
  349. * 微信客服会话接口 - 未接入会话
  350. ****************************************************/
  351. public function wxServiceSessionWaitCase(){
  352. $wxAccessToken = $this->wxAccessToken();
  353. $url = "https://api.weixin.qq.com/customservice/kfsession/getwaitcase?access_token=".$wxAccessToken;
  354. $result = $this->wxHttpsRequest($url);
  355. $jsoninfo = json_decode($result,true);
  356. return $jsoninfo;
  357. }
  358. /****************************************************
  359. * 微信摇一摇 - 申请设备ID
  360. ****************************************************/
  361. public function wxDeviceApply($jsonData){
  362. $wxAccessToken = $this->wxAccessToken();
  363. $url = "https://api.weixin.qq.com/shakearound/device/applyid?access_token=" . $wxAccessToken;
  364. $result = $this->wxHttpsRequest($url,true);
  365. return $jsoninfo;
  366. }
  367. /****************************************************
  368. * 微信摇一摇 - 编辑设备ID
  369. ****************************************************/
  370. public function wxDeviceUpdate($jsonData){
  371. $wxAccessToken = $this->wxAccessToken();
  372. $url = "https://api.weixin.qq.com/shakearound/device/update?access_token=" . $wxAccessToken;
  373. $result = $this->wxHttpsRequest($url,true);
  374. return $jsoninfo;
  375. }
  376. /****************************************************
  377. * 微信摇一摇 - 本店关联设备
  378. ****************************************************/
  379. public function wxDeviceBindLocation($jsonData){
  380. $wxAccessToken = $this->wxAccessToken();
  381. $url = "https://api.weixin.qq.com/shakearound/device/bindlocation?access_token=" . $wxAccessToken;
  382. $result = $this->wxHttpsRequest($url,true);
  383. return $jsoninfo;
  384. }
  385. /****************************************************
  386. * 微信摇一摇 - 查询设备列表
  387. ****************************************************/
  388. public function wxDeviceSearch($jsonData){
  389. $wxAccessToken = $this->wxAccessToken();
  390. $url = "https://api.weixin.qq.com/shakearound/device/search?access_token=" . $wxAccessToken;
  391. $result = $this->wxHttpsRequest($url,true);
  392. return $jsoninfo;
  393. }
  394. /****************************************************
  395. * 微信摇一摇 - 新增页面
  396. ****************************************************/
  397. public function wxPageAdd($jsonData){
  398. $wxAccessToken = $this->wxAccessToken();
  399. $url = "https://api.weixin.qq.com/shakearound/page/add?access_token=" . $wxAccessToken;
  400. $result = $this->wxHttpsRequest($url,true);
  401. return $jsoninfo;
  402. }
  403. /****************************************************
  404. * 微信摇一摇 - 编辑页面
  405. ****************************************************/
  406. public function wxPageUpdate($jsonData){
  407. $wxAccessToken = $this->wxAccessToken();
  408. $url = "https://api.weixin.qq.com/shakearound/page/update?access_token=" . $wxAccessToken;
  409. $result = $this->wxHttpsRequest($url,true);
  410. return $jsoninfo;
  411. }
  412. /****************************************************
  413. * 微信摇一摇 - 查询页面
  414. ****************************************************/
  415. public function wxPageSearch($jsonData){
  416. $wxAccessToken = $this->wxAccessToken();
  417. $url = "https://api.weixin.qq.com/shakearound/page/search?access_token=" . $wxAccessToken;
  418. $result = $this->wxHttpsRequest($url,true);
  419. return $jsoninfo;
  420. }
  421. /****************************************************
  422. * 微信摇一摇 - 删除页面
  423. ****************************************************/
  424. public function wxPageDelete($jsonData){
  425. $wxAccessToken = $this->wxAccessToken();
  426. $url = "https://api.weixin.qq.com/shakearound/page/delete?access_token=" . $wxAccessToken;
  427. $result = $this->wxHttpsRequest($url,true);
  428. return $jsoninfo;
  429. }
  430. /*******************************************************
  431. * 微信摇一摇 - 上传图片素材
  432. *******************************************************/
  433. public function wxMaterialAdd($media = '') {
  434. $wxAccessToken = $this->wxAccessToken();
  435. //$data['access_token'] = $wxAccessToken;
  436. $data['media'] = '@D:\\workspace\\htdocs\\yky_test\\logo.jpg';
  437. $url = "https://api.weixin.qq.com/shakearound/material/add?access_token=".$wxAccessToken;
  438. $result = $this->wxHttpsRequest($url,true);
  439. return $jsoninfo;
  440. }
  441. /****************************************************
  442. * 微信摇一摇 - 配置设备与页面的关联关系
  443. ****************************************************/
  444. public function wxDeviceBindPage($jsonData){
  445. $wxAccessToken = $this->wxAccessToken();
  446. $url = "https://api.weixin.qq.com/shakearound/device/bindpage?access_token=" . $wxAccessToken;
  447. $result = $this->wxHttpsRequest($url,true);
  448. return $jsoninfo;
  449. }
  450. /****************************************************
  451. * 微信摇一摇 - 获取摇周边的设备及用户信息
  452. ****************************************************/
  453. public function wxGetShakeInfo($jsonData){
  454. $wxAccessToken = $this->wxAccessToken();
  455. $url = "https://api.weixin.qq.com/shakearound/user/getshakeinfo?access_token=" . $wxAccessToken;
  456. $result = $this->wxHttpsRequest($url,true);
  457. return $jsoninfo;
  458. }
  459. /****************************************************
  460. * 微信摇一摇 - 以设备为维度的数据统计接口
  461. ****************************************************/
  462. public function wxGetShakeStatistics($jsonData){
  463. $wxAccessToken = $this->wxAccessToken();
  464. $url = "https://api.weixin.qq.com/shakearound/statistics/device?access_token=" . $wxAccessToken;
  465. $result = $this->wxHttpsRequest($url,true);
  466. return $jsoninfo;
  467. }
  468. /*****************************************************
  469. * 生成随机字符串 - 最长为32位字符串
  470. *****************************************************/
  471. public function wxNonceStr($length = 16,$type = FALSE) {
  472. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  473. $str = "";
  474. for ($i = 0; $i < $length; $i++) {
  475. $str .= substr($chars,mt_rand(0,strlen($chars) - 1),1);
  476. }
  477. if($type == TRUE){
  478. return strtoupper(md5(time() . $str));
  479. }
  480. else {
  481. return $str;
  482. }
  483. }
  484. /*******************************************************
  485. * 微信商户订单号 - 最长28位字符串
  486. *******************************************************/
  487. public function wxMchBillno($mchid = NULL) {
  488. if(is_null($mchid)){
  489. if(self::mchid == "" || is_null(self::mchid)){
  490. $mchid = time();
  491. }
  492. else{
  493. $mchid = self::mchid;
  494. }
  495. }
  496. else{
  497. $mchid = substr(addslashes($mchid),10);
  498. }
  499. return date("Ymd",time()).time().$mchid;
  500. }
  501. /*******************************************************
  502. * 微信格式化数组变成参数格式 - 支持url加密
  503. *******************************************************/
  504. public function wxSetParam($parameters){
  505. if(is_array($parameters) && !empty($parameters)){
  506. $this->parameters = $parameters;
  507. return $this->parameters;
  508. }
  509. else{
  510. return array();
  511. }
  512. }
  513. /*******************************************************
  514. * 微信格式化数组变成参数格式 - 支持url加密
  515. *******************************************************/
  516. public function wxFormatArray($parameters = NULL,$urlencode = FALSE){
  517. if(is_null($parameters)){
  518. $parameters = $this->parameters;
  519. }
  520. $restr = "";//初始化空
  521. ksort($parameters);//排序参数
  522. foreach ($parameters as $k => $v){//循环定制参数
  523. if (null != $v && "null" != $v && "sign" != $k) {
  524. if($urlencode){//如果参数需要增加URL加密就增加,不需要则不需要
  525. $v = urlencode($v);
  526. }
  527. $restr .= $k . "=" . $v . "&";//返回完整字符串
  528. }
  529. }
  530. if (strlen($restr) > 0) {//如果存在数据则将最后“&”删除
  531. $restr = substr($restr,strlen($restr)-1);
  532. }
  533. return $restr;//返回字符串
  534. }
  535. /*******************************************************
  536. * 微信MD5签名生成器 - 需要将参数数组转化成为字符串[wxFormatArray方法]
  537. *******************************************************/
  538. public function wxMd5Sign($content,$privatekey){
  539. try {
  540. if (is_null($privatekey)) {
  541. throw new Exception("财付通签名key不能为空!");
  542. }
  543. if (is_null($content)) {
  544. throw new Exception("财付通签名内容不能为空");
  545. }
  546. $signStr = $content . "&key=" . $privatekey;
  547. return strtoupper(md5($signStr));
  548. }
  549. catch (Exception $e)
  550. {
  551. die($e->getMessage());
  552. }
  553. }
  554. /*******************************************************
  555. * 微信Sha1签名生成器 - 需要将参数数组转化成为字符串[wxFormatArray方法]
  556. *******************************************************/
  557. public function wxSha1Sign($content){
  558. try {
  559. if (is_null($content)) {
  560. throw new Exception("签名内容不能为空");
  561. }
  562. //$signStr = $content;
  563. return sha1($content);
  564. }
  565. catch (Exception $e)
  566. {
  567. die($e->getMessage());
  568. }
  569. }
  570. /*******************************************************
  571. * 微信jsApi整合方法 - 通过调用方法获得jsapi数据
  572. *******************************************************/
  573. public function wxJsapiPackage(){
  574. $jsapi_ticket = $this->wxVerifyJsApiTicket();
  575. // 注意 URL 一定要动态获取,不能 hardcode.
  576. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  577. $url = $protocol.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
  578. $timestamp = time();
  579. $nonceStr = $this->wxNonceStr();
  580. $signPackage = array(
  581. "jsapi_ticket" => $jsapi_ticket,"nonceStr" => $nonceStr,"timestamp" => $timestamp,"url" => $url
  582. );
  583. // 这里参数的顺序要按照 key 值 ASCII 码升序排序
  584. $rawString = "jsapi_ticket=$jsapi_ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
  585. //$rawString = $this->wxFormatArray($signPackage);
  586. $signature = $this->wxSha1Sign($rawString);
  587. $signPackage['signature'] = $signature;
  588. $signPackage['rawString'] = $rawString;
  589. $signPackage['appId'] = self::appId;
  590. return $signPackage;
  591. }
  592. /*******************************************************
  593. * 微信卡券:JSAPI 卡券Package - 基础参数没有附带任何值 - 再生产环境中需要根据实际情况进行修改
  594. *******************************************************/
  595. public function wxCardPackage($cardId,$timestamp = ''){
  596. $api_ticket = $this->wxVerifyJsApiTicket();
  597. if(!empty($timestamp)){
  598. $timestamp = $timestamp;
  599. }
  600. else{
  601. $timestamp = time();
  602. }
  603. $arrays = array(self::appSecret,$timestamp,$cardId);
  604. sort($arrays,SORT_STRING);
  605. //print_r($arrays);
  606. //echo implode("",$arrays)."<br />";
  607. $string = sha1(implode($arrays));
  608. //echo $string;
  609. $resultArray['cardId'] = $cardId;
  610. $resultArray['cardExt'] = array();
  611. $resultArray['cardExt']['code'] = '';
  612. $resultArray['cardExt']['openid'] = '';
  613. $resultArray['cardExt']['timestamp'] = $timestamp;
  614. $resultArray['cardExt']['signature'] = $string;
  615. //print_r($resultArray);
  616. return $resultArray;
  617. }
  618. /*******************************************************
  619. * 微信卡券:JSAPI 卡券全部卡券 Package
  620. *******************************************************/
  621. public function wxCardAllPackage($cardIdArray = array(),$timestamp = ''){
  622. $reArrays = array();
  623. if(!empty($cardIdArray) && (is_array($cardIdArray) || is_object($cardIdArray))){
  624. //print_r($cardIdArray);
  625. foreach($cardIdArray as $value){
  626. //print_r($this->wxCardPackage($value,$openid));
  627. $reArrays[] = $this->wxCardPackage($value,$timestamp);
  628. }
  629. //print_r($reArrays);
  630. }
  631. else{
  632. $reArrays[] = $this->wxCardPackage($cardIdArray,$timestamp);
  633. }
  634. return strval(json_encode($reArrays));
  635. }
  636. /*******************************************************
  637. * 微信卡券:获取卡券列表
  638. *******************************************************/
  639. public function wxCardListPackage($cardType = "",$cardId = ""){
  640. //$api_ticket = $this->wxVerifyJsApiTicket();
  641. $resultArray = array();
  642. $timestamp = time();
  643. $nonceStr = $this->wxNonceStr();
  644. //$strings =
  645. $arrays = array(self::appId,self::appSecret,$nonceStr);
  646. sort($arrays,SORT_STRING);
  647. $string = sha1(implode($arrays));
  648. $resultArray['app_id'] = self::appId;
  649. $resultArray['card_sign'] = $string;
  650. $resultArray['time_stamp'] = $timestamp;
  651. $resultArray['nonce_str'] = $nonceStr;
  652. $resultArray['card_type'] = $cardType;
  653. $resultArray['card_id'] = $cardId;
  654. return $resultArray;
  655. }
  656. /*******************************************************
  657. * 将数组解析XML - 微信红包接口
  658. *******************************************************/
  659. public function wxArrayToXml($parameters = NULL){
  660. if(is_null($parameters)){
  661. $parameters = $this->parameters;
  662. }
  663. if(!is_array($parameters) || empty($parameters)){
  664. die("参数不为数组无法解析");
  665. }
  666. $xml = "<xml>";
  667. foreach ($arr as $key=>$val)
  668. {
  669. if (is_numeric($val))
  670. {
  671. $xml.="<".$key.">".$val."</".$key.">";
  672. }
  673. else
  674. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  675. }
  676. $xml.="</xml>";
  677. return $xml;
  678. }
  679. /*******************************************************
  680. * 微信卡券:上传logo - 需要改写动态功能
  681. *******************************************************/
  682. public function wxCardUpdateImg() {
  683. $wxAccessToken = $this->wxAccessToken();
  684. //$data['access_token'] = $wxAccessToken;
  685. $data['buffer'] = '@D:\\workspace\\htdocs\\yky_test\\logo.jpg';
  686. $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$wxAccessToken;
  687. $result = $this->wxHttpsRequest($url,true);
  688. return $jsoninfo;
  689. //array(1) { ["url"]=> string(121) "http://mmbiz.qpic.cn/mmbiz/ibuYxPHqeXePNTW4ATKyias1Cf3zTKiars9PFPzF1k5icvXD7xW0kXUAxHDzkEPd9micCMCN0dcTJfW6Tnm93MiaAfRQ/0" }
  690. }
  691. /*******************************************************
  692. * 微信卡券:获取颜色
  693. *******************************************************/
  694. public function wxCardColor(){
  695. $wxAccessToken = $this->wxAccessToken();
  696. $url = "https://api.weixin.qq.com/card/getcolors?access_token=".$wxAccessToken;
  697. $result = $this->wxHttpsRequest($url);
  698. $jsoninfo = json_decode($result,true);
  699. return $jsoninfo;
  700. }
  701. /*******************************************************
  702. * 微信卡券:拉取门店列表
  703. *******************************************************/
  704. public function wxBatchGet($offset = 0,$count = 0){
  705. $jsonData = json_encode(array('offset' => intval($offset),'count' => intval($count)));
  706. $wxAccessToken = $this->wxAccessToken();
  707. $url = "https://api.weixin.qq.com/card/location/batchget?access_token=" . $wxAccessToken;
  708. $result = $this->wxHttpsRequest($url,true);
  709. return $jsoninfo;
  710. }
  711. /*******************************************************
  712. * 微信卡券:创建卡券
  713. *******************************************************/
  714. public function wxCardCreated($jsonData) {
  715. $wxAccessToken = $this->wxAccessToken();
  716. $url = "https://api.weixin.qq.com/card/create?access_token=" . $wxAccessToken;
  717. $result = $this->wxHttpsRequest($url,true);
  718. return $jsoninfo;
  719. }
  720. /*******************************************************
  721. * 微信卡券:查询卡券详情
  722. *******************************************************/
  723. public function wxCardGetInfo($jsonData) {
  724. $wxAccessToken = $this->wxAccessToken();
  725. $url = "https://api.weixin.qq.com/card/get?access_token=" . $wxAccessToken;
  726. $result = $this->wxHttpsRequest($url,true);
  727. return $jsoninfo;
  728. }
  729. /*******************************************************
  730. * 微信卡券:设置白名单
  731. *******************************************************/
  732. public function wxCardWhiteList($jsonData){
  733. $wxAccessToken = $this->wxAccessToken();
  734. $url = "https://api.weixin.qq.com/card/testwhitelist/set?access_token=" . $wxAccessToken;
  735. $result = $this->wxHttpsRequest($url,true);
  736. return $jsoninfo;
  737. }
  738. /*******************************************************
  739. * 微信卡券:消耗卡券
  740. *******************************************************/
  741. public function wxCardConsume($jsonData){
  742. $wxAccessToken = $this->wxAccessToken();
  743. $url = "https://api.weixin.qq.com/card/code/consume?access_token=" . $wxAccessToken;
  744. $result = $this->wxHttpsRequest($url,true);
  745. return $jsoninfo;
  746. }
  747. /*******************************************************
  748. * 微信卡券:删除卡券
  749. *******************************************************/
  750. public function wxCardDelete($jsonData){
  751. $wxAccessToken = $this->wxAccessToken();
  752. $url = "https://api.weixin.qq.com/card/delete?access_token=" . $wxAccessToken;
  753. $result = $this->wxHttpsRequest($url,true);
  754. return $jsoninfo;
  755. }
  756. /*******************************************************
  757. * 微信卡券:选择卡券 - 解析CODE
  758. *******************************************************/
  759. public function wxCardDecryptCode($jsonData){
  760. $wxAccessToken = $this->wxAccessToken();
  761. $url = "https://api.weixin.qq.com/card/code/decrypt?access_token=" . $wxAccessToken;
  762. $result = $this->wxHttpsRequest($url,true);
  763. return $jsoninfo;
  764. }
  765. /*******************************************************
  766. * 微信卡券:更改库存
  767. *******************************************************/
  768. public function wxCardModifyStock($cardId,$increase_stock_value = 0,$reduce_stock_value = 0){
  769. if(intval($increase_stock_value) == 0 && intval($reduce_stock_value) == 0){
  770. return false;
  771. }
  772. $jsonData = json_encode(array("card_id" => $cardId,'increase_stock_value' => intval($increase_stock_value),'reduce_stock_value' => intval($reduce_stock_value)));
  773. $wxAccessToken = $this->wxAccessToken();
  774. $url = "https://api.weixin.qq.com/card/modifystock?access_token=" . $wxAccessToken;
  775. $result = $this->wxHttpsRequest($url,true);
  776. return $jsoninfo;
  777. }
  778. /*******************************************************
  779. * 微信卡券:查询用户CODE
  780. *******************************************************/
  781. public function wxCardQueryCode($code,$cardId = ''){
  782. $jsonData = json_encode(array("code" => $code,'card_id' => $cardId ));
  783. $wxAccessToken = $this->wxAccessToken();
  784. $url = "https://api.weixin.qq.com/card/code/get?access_token=" . $wxAccessToken;
  785. $result = $this->wxHttpsRequest($url,true);
  786. return $jsoninfo;
  787. }
  788. }
 

猜你在找的PHP相关文章