1
+ <?php
2
+ /*
3
+ * Licensed to the Apache Software Foundation (ASF) under one
4
+ * or more contributor license agreements. See the NOTICE file
5
+ * distributed with this work for additional information
6
+ * regarding copyright ownership. The ASF licenses this file
7
+ * to you under the Apache License, Version 2.0 (the
8
+ * "License"); you may not use this file except in compliance
9
+ * with the License. You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing,
14
+ * software distributed under the License is distributed on an
15
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ * KIND, either express or implied. See the License for the
17
+ * specific language governing permissions and limitations
18
+ * under the License.
19
+ */
20
+
21
+ namespace aliyun \cdn ;
22
+
23
+ use GuzzleHttp \HandlerStack ;
24
+ use GuzzleHttp \Client as HttpClient ;
25
+ use aliyun \guzzle \subscriber \Rpc ;
26
+
27
+ /**
28
+ * Class Client
29
+ * @package aliyun\cdn
30
+ */
31
+ class Client
32
+ {
33
+ /**
34
+ * @var string
35
+ */
36
+ public $ accessKeyId ;
37
+
38
+ /**
39
+ * @var string
40
+ */
41
+ public $ accessSecret ;
42
+
43
+ /**
44
+ * @var string API版本
45
+ */
46
+ public $ version = '2014-11-11 ' ;
47
+
48
+ /**
49
+ * @var string 网关地址
50
+ */
51
+ public $ baseUri = 'http://cdn.aliyuncs.com/ ' ;
52
+
53
+ /**
54
+ * @var HttpClient
55
+ */
56
+ private $ _httpClient ;
57
+
58
+ /**
59
+ * Request constructor.
60
+ * @param array $config
61
+ */
62
+ public function __construct ($ config = [])
63
+ {
64
+ foreach ($ config as $ name => $ value ) {
65
+ $ this ->{$ name } = $ value ;
66
+ }
67
+ }
68
+
69
+ /**
70
+ * 获取Http Client
71
+ * @return HttpClient
72
+ */
73
+ public function getHttpClient ()
74
+ {
75
+ if (!is_object ($ this ->_httpClient )) {
76
+ $ stack = HandlerStack::create ();
77
+ $ middleware = new Rpc ([
78
+ 'accessKeyId ' => $ this ->accessKeyId ,
79
+ 'accessSecret ' => $ this ->accessSecret ,
80
+ 'Version ' => $ this ->version
81
+ ]);
82
+ $ stack ->push ($ middleware );
83
+ $ this ->_httpClient = new HttpClient ([
84
+ 'base_uri ' => $ this ->baseUri ,
85
+ 'handler ' => $ stack ,
86
+ 'verify ' => false ,
87
+ 'http_errors ' => false ,
88
+ 'connect_timeout ' => 3 ,
89
+ 'read_timeout ' => 10 ,
90
+ 'debug ' => false ,
91
+ ]);
92
+ }
93
+ return $ this ->_httpClient ;
94
+ }
95
+
96
+ /**
97
+ * @param array $params
98
+ * @return string
99
+ */
100
+ public function createRequest (array $ params )
101
+ {
102
+ return $ this ->getHttpClient ()->get ('/ ' , ['query ' => $ params ]);
103
+ }
104
+ }
0 commit comments