|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the LineMob package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Ishmael Doss <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LineMob\Core\HttpClient; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Psr7\Request; |
|
15
|
|
|
use Http\Adapter\Guzzle6\Client as GuzzleAdapter; |
|
16
|
|
|
use Http\Client\HttpClient; |
|
17
|
|
|
use LINE\LINEBot\HTTPClient as HttpClientInterface; |
|
18
|
|
|
use LINE\LINEBot\Response; |
|
19
|
|
|
use LineMob\Core\Constants; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Ishmael Doss <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class GuzzleHttpClient implements HttpClientInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var HttpClient |
|
28
|
|
|
*/ |
|
29
|
|
|
private $httpClient; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct($channelToken, array $config = []) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->httpClient = GuzzleAdapter::createWithConfig(array_replace_recursive([ |
|
34
|
|
|
'verify' => true, |
|
35
|
|
|
'headers' => [ |
|
36
|
|
|
'User-Agent' => 'LINEMOB-PHP/'.Constants::VERSION, |
|
37
|
|
|
'Authorization' => 'Bearer '.$channelToken, |
|
38
|
|
|
] |
|
39
|
|
|
], $config)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function get($url) |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->doRequest('POST', $url, [], []); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function post($url, array $data) |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->doRequest('POST', $url, ['Content-Type' => 'application/json; charset=utf-8'], $data); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $method |
|
60
|
|
|
* @param $url |
|
61
|
|
|
* @param array $headers |
|
62
|
|
|
* @param array $data |
|
63
|
|
|
* |
|
64
|
|
|
* @return Response |
|
65
|
|
|
*/ |
|
66
|
|
|
private function doRequest($method, $url, array $headers = [], array $data = []) |
|
67
|
|
|
{ |
|
68
|
|
|
$request = new Request($method, $url, $headers, \GuzzleHttp\json_encode($data)); |
|
69
|
|
|
$response = $this->httpClient->sendRequest($request); |
|
70
|
|
|
|
|
71
|
|
|
return new Response( |
|
72
|
|
|
$response->getStatusCode(), |
|
73
|
|
|
$response->getBody()->getContents(), |
|
74
|
|
|
/** @scrutinizer ignore-type */ |
|
75
|
|
|
$response->getHeaders() |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|