|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* AccessToken.php. |
|
14
|
|
|
* |
|
15
|
|
|
* Part of Overtrue\WeChat. |
|
16
|
|
|
* |
|
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
18
|
|
|
* file that was distributed with this source code. |
|
19
|
|
|
* |
|
20
|
|
|
* @author mingyoung <[email protected]> |
|
21
|
|
|
* @copyright 2016 |
|
22
|
|
|
* |
|
23
|
|
|
* @see https://github.com/overtrue |
|
24
|
|
|
* @see http://overtrue.me |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace EasyWeChat\OpenPlatform; |
|
28
|
|
|
|
|
29
|
|
|
use EasyWeChat\Core\AccessToken as WechatAccessToken; |
|
30
|
|
|
use EasyWeChat\Core\Exceptions\HttpException; |
|
31
|
|
|
use EasyWeChat\OpenPlatform\Traits\VerifyTicket; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class AccessToken extends WechatAccessToken |
|
34
|
|
|
{ |
|
35
|
|
|
use VerifyTicket; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* API. |
|
39
|
|
|
*/ |
|
40
|
|
|
const API_TOKEN_GET = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc}. |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $queryName = 'component_access_token'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc}. |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $tokenJsonKey = 'component_access_token'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc}. |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $prefix = 'easywechat.common.component_access_token.'; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc}. |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function getTokenFromServer() |
|
61
|
|
|
{ |
|
62
|
|
|
$data = [ |
|
63
|
|
|
'component_appid' => $this->appId, |
|
64
|
|
|
'component_appsecret' => $this->secret, |
|
65
|
|
|
'component_verify_ticket' => $this->verifyTicket->getTicket(), |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
$http = $this->getHttp(); |
|
69
|
|
|
|
|
70
|
|
|
$token = $http->parseJSON($http->json(self::API_TOKEN_GET, $data)); |
|
71
|
|
|
|
|
72
|
|
|
if (empty($token[$this->tokenJsonKey])) { |
|
73
|
|
|
throw new HttpException('Request ComponentAccessToken fail. response: '.json_encode($token, JSON_UNESCAPED_UNICODE)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $token; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: