AccessToken.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * AccessToken.php.
  12. *
  13. * Part of Overtrue\WeChat.
  14. *
  15. * For the full copyright and license information, please view the LICENSE
  16. * file that was distributed with this source code.
  17. *
  18. * @author mingyoung <mingyoungcheung@gmail.com>
  19. * @copyright 2016
  20. *
  21. * @see https://github.com/overtrue
  22. * @see http://overtrue.me
  23. */
  24. namespace EasyWeChat\OpenPlatform;
  25. use EasyWeChat\Core\AccessToken as CoreAccessToken;
  26. use EasyWeChat\Core\Exceptions\HttpException;
  27. class AccessToken extends CoreAccessToken
  28. {
  29. /**
  30. * VerifyTicket.
  31. *
  32. * @var \EasyWeChat\OpenPlatform\VerifyTicket
  33. */
  34. protected $verifyTicket;
  35. /**
  36. * API.
  37. */
  38. const API_TOKEN_GET = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
  39. /**
  40. * {@inheritdoc}.
  41. */
  42. protected $queryName = 'component_access_token';
  43. /**
  44. * {@inheritdoc}.
  45. */
  46. protected $tokenJsonKey = 'component_access_token';
  47. /**
  48. * {@inheritdoc}.
  49. */
  50. protected $prefix = 'easywechat.open_platform.component_access_token.';
  51. /**
  52. * Set VerifyTicket.
  53. *
  54. * @param EasyWeChat\OpenPlatform\VerifyTicket $verifyTicket
  55. *
  56. * @return $this
  57. */
  58. public function setVerifyTicket(VerifyTicket $verifyTicket)
  59. {
  60. $this->verifyTicket = $verifyTicket;
  61. return $this;
  62. }
  63. /**
  64. * {@inheritdoc}.
  65. */
  66. public function getTokenFromServer()
  67. {
  68. $data = [
  69. 'component_appid' => $this->appId,
  70. 'component_appsecret' => $this->secret,
  71. 'component_verify_ticket' => $this->verifyTicket->getTicket(),
  72. ];
  73. $http = $this->getHttp();
  74. $token = $http->parseJSON($http->json(self::API_TOKEN_GET, $data));
  75. if (empty($token[$this->tokenJsonKey])) {
  76. throw new HttpException('Request ComponentAccessToken fail. response: '.json_encode($token, JSON_UNESCAPED_UNICODE));
  77. }
  78. return $token;
  79. }
  80. }