获取小程序内访问页面的二维码

  1. <?php
  2. class RoutineCode{
  3. /**
  4. * 获取小程序内访问页面的二维码
  5. * @param string $path
  6. * @param int $width
  7. * @return mixed
  8. */
  9. public static function getPages($path = '',$width = 430){
  10. $accessToken = self::get_access_token();
  11. $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$accessToken;
  12. $data['path'] = $path;
  13. $data['width'] = $width;
  14. return self::curlPost($url,json_encode($data));
  15. }
  16. /**
  17. * 获取access_token access_token保存在文件中
  18. * @return mixed
  19. */
  20. public static function get_access_token(){
  21. $data = json_decode(file_get_contents("access_token.json"));
  22. if ($data->expire_time < time()) {
  23. $res = self::getAccessToken();
  24. $access_token = $res->access_token;
  25. if ($access_token) {
  26. $data->expire_time = time() + 7000;
  27. $data->access_token = $access_token;
  28. $fp = fopen("access_token.json", "w");
  29. fwrite($fp, json_encode($data));
  30. fclose($fp);
  31. }
  32. }else {
  33. $access_token = $data->access_token;
  34. }
  35. return $access_token;
  36. }
  37. /**
  38. * @param string $routineAppId
  39. * @param string $routineAppSecret
  40. * @return mixed
  41. */
  42. public static function getAccessToken(){
  43. $routineAppId = 'routineAppId';
  44. $routineAppSecret = 'routineAppSecret';
  45. $url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$routineAppId."&secret=".$routineAppSecret;
  46. return json_decode(self::curlGet($url),true);
  47. }
  48. /**
  49. * curl get方式
  50. * @param string $url
  51. * @param array $options
  52. * @return mixed
  53. */
  54. public static function curlGet($url = '', $options = array())
  55. {
  56. $ch = curl_init($url);
  57. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  58. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  59. if (!empty($options)) {
  60. curl_setopt_array($ch, $options);
  61. }
  62. //https请求 不验证证书和host
  63. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  64. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  65. $data = curl_exec($ch);
  66. curl_close($ch);
  67. return $data;
  68. }
  69. /**
  70. * curl post
  71. * @param string $url
  72. * @param string $postData
  73. * @param array $options
  74. * @return mixed
  75. */
  76. public static function curlPost($url = '', $postData = '', $options = array())
  77. {
  78. if (is_array($postData)) {
  79. $postData = http_build_query($postData);
  80. }
  81. $ch = curl_init();
  82. curl_setopt($ch, CURLOPT_URL, $url);
  83. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  84. curl_setopt($ch, CURLOPT_POST, 1);
  85. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  86. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  87. if (!empty($options)) {
  88. curl_setopt_array($ch, $options);
  89. }
  90. //https请求 不验证证书和host
  91. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  92. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  93. $data = curl_exec($ch);
  94. curl_close($ch);
  95. return $data;
  96. }
  97. }
  98. ?>


评论 1

发表评论

Top