Qrcode.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeChat;
  16. use WeChat\Contracts\BasicWeChat;
  17. /**
  18. * 二维码管理
  19. * Class Qrcode
  20. * @package WeChat
  21. */
  22. class Qrcode extends BasicWeChat
  23. {
  24. /**
  25. * 创建二维码ticket
  26. * @param string|integer $scene 场景
  27. * @param int $expire_seconds 有效时间
  28. * @return array
  29. * @throws \WeChat\Exceptions\InvalidResponseException
  30. * @throws \WeChat\Exceptions\LocalCacheException
  31. */
  32. public function create($scene, $expire_seconds = 0)
  33. {
  34. if (is_integer($scene)) { // 二维码场景类型
  35. $data = ['action_info' => ['scene' => ['scene_id' => $scene]]];
  36. } else {
  37. $data = ['action_info' => ['scene' => ['scene_str' => $scene]]];
  38. }
  39. if ($expire_seconds > 0) { // 临时二维码
  40. $data['expire_seconds'] = $expire_seconds;
  41. $data['action_name'] = is_integer($scene) ? 'QR_SCENE' : 'QR_STR_SCENE';
  42. } else { // 永久二维码
  43. $data['action_name'] = is_integer($scene) ? 'QR_LIMIT_SCENE' : 'QR_LIMIT_STR_SCENE';
  44. }
  45. $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN";
  46. $this->registerApi($url, __FUNCTION__, func_get_args());
  47. return $this->httpPostForJson($url, $data);
  48. }
  49. /**
  50. * 通过ticket换取二维码
  51. * @param string $ticket 获取的二维码ticket,凭借此ticket可以在有效时间内换取二维码。
  52. * @return string
  53. */
  54. public function url($ticket)
  55. {
  56. return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode($ticket);
  57. }
  58. /**
  59. * 长链接转短链接接口
  60. * @param string $longUrl 需要转换的长链接
  61. * @return array
  62. * @throws \WeChat\Exceptions\InvalidResponseException
  63. * @throws \WeChat\Exceptions\LocalCacheException
  64. */
  65. public function shortUrl($longUrl)
  66. {
  67. $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=ACCESS_TOKEN";
  68. $this->registerApi($url, __FUNCTION__, func_get_args());
  69. return $this->httpPostForJson($url, ['action' => 'long2short', 'long_url' => $longUrl]);
  70. }
  71. }