php页面调用微信扫一扫

functions.php

  1. <?php
  2. define( "appID", "微信公众号appId");
  3. define( "appsecret", "");
  4. class JSSDK {
  5. private $appId;
  6. private $appSecret;
  7. public function __construct($appId, $appSecret) {
  8. $this->appId = $appId;
  9. $this->appSecret = $appSecret;
  10. }
  11. public function getSignPackage() {
  12. $jsapiTicket = $this->getJsApiTicket();
  13. $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  14. $timestamp = time();
  15. $nonceStr = $this->createNonceStr();
  16. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
  17. $signature = sha1($string);
  18. $signPackage = array(
  19. "appId" => $this->appId,
  20. "nonceStr" => $nonceStr,
  21. "timestamp" => $timestamp,
  22. "url" => $url,
  23. "signature" => $signature,
  24. "rawString" => $string
  25. );
  26. return $signPackage;
  27. }
  28. private function createNonceStr($length = 16) {
  29. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  30. $str = "";
  31. for ($i = 0; $i < $length; $i++) {
  32. $str .= substr($chars, mt_rand( 0, strlen($chars) - 1), 1);
  33. }
  34. return $str;
  35. }
  36. private function getJsApiTicket() {
  37. $data = json_decode(file_get_contents( "wp-token/jsapi_ticket.json"));
  38. if ($data->expire_time < time()) {
  39. $accessToken = $this->getAccessToken();
  40. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
  41. $res = json_decode( $this->httpGet($url));
  42. $ticket = $res->ticket;
  43. if ($ticket) {
  44. $data->expire_time = time() + 7000;
  45. $data->jsapi_ticket = $ticket;
  46. $fp = fopen( "wp-token/jsapi_ticket.json", "w");
  47. fwrite($fp, json_encode($data));
  48. fclose($fp);
  49. }
  50. } else {
  51. $ticket = $data->jsapi_ticket;
  52. }
  53. return $ticket;
  54. }
  55. public function getAccessToken() {
  56. $data = json_decode(file_get_contents( "wp-token/access_token.json"));
  57. if ($data->expire_time < time()) {
  58. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
  59. $res = json_decode( $this->httpGet($url));
  60. $access_token = $res->access_token;
  61. if ($access_token) {
  62. $data->expire_time = time() + 7000;
  63. $data->access_token = $access_token;
  64. $fp = fopen( "wp-token/access_token.json", "w");
  65. fwrite($fp, json_encode($data));
  66. fclose($fp);
  67. }
  68. } else {
  69. $access_token = $data->access_token;
  70. }
  71. return $access_token;
  72. }
  73. public function httpGet($url) {
  74. $curl = curl_init();
  75. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  76. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  77. curl_setopt($curl, CURLOPT_URL, $url);
  78. $res = curl_exec($curl);
  79. curl_close($curl);
  80. return $res;
  81. }
  82. }
  83. ?>



test.php


  1. <?php
  2. require_once( "functions.php");
  3. $jssdk= new JSSDK(appID,appsecret);
  4. $signPackage = $jssdk->GetSignPackage();
  5. require_once( 'header.php');
  6. ?>
  7. <a href= "javascript:;">扫一扫</a>
  8. <?php
  9. require_once( 'footer.php');
  10. ?>
  11. <script src= "scripts/jquery/3.1.1/jquery.min.js"></script>
  12. <script src= "scripts/jweixin-1.0.0.js"></script>
  13. <script>
  14. wx.config({
  15. debug: false,
  16. appId: '<?php echo $signPackage["appId"];?>',
  17. timestamp: <?php echo $signPackage[ "timestamp"]; ?>,
  18. nonceStr: '<?php echo $signPackage["nonceStr"];?>',
  19. signature: '<?php echo $signPackage["signature"];?>',
  20. jsApiList: [
  21. // 所有要调用的 API 都要加到这个列表中
  22. "scanQRCode"
  23. ]
  24. });
  25. wx.ready( function () {
  26. // 在这里调用 API
  27. $(document).on( "click", "a", function(){
  28. wx.scanQRCode({
  29. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  30. scanType: [ "qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  31. success: function (res) {
  32. //alert(res);
  33. for(i in res ){
  34. // alert(i); //获得属性
  35. alert(i + "---" + res[i]); //获得属性值
  36. }
  37. //var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
  38. }
  39. });
  40. })
  41. });
  42. </script>
网址web.wxticket.com
扣扣:904九九九九八八

猜你喜欢

转载自blog.csdn.net/qq_38491171/article/details/80868495