1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jne; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as Guzzle; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
8
|
|
|
use Jne\Contracts\HttpClientInterface; |
9
|
|
|
|
10
|
|
|
class HttpClient extends Guzzle implements HttpClientInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Http base uri. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $baseUri; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create a new instance of HttpClient. |
21
|
|
|
* |
22
|
|
|
* @param string $baseUri |
23
|
|
|
*/ |
24
|
11 |
|
public function __construct($baseUri) |
25
|
|
|
{ |
26
|
11 |
|
$this->baseUri = $baseUri; |
27
|
|
|
|
28
|
11 |
|
parent::__construct([ |
29
|
11 |
|
'base_uri' => $this->baseUri, |
30
|
|
|
'headers' => [ |
31
|
11 |
|
'Accept' => 'text/plain', |
32
|
11 |
|
'Accept-Encoding' => 'gzip, deflate, sdch', |
33
|
11 |
|
'Accept-Language' => 'en-US', |
34
|
11 |
|
'Host' => 'www.jne.co.id', |
35
|
11 |
|
'Referer' => 'http://www.jne.co.id/home.php', |
36
|
11 |
|
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36', |
37
|
11 |
|
], |
38
|
11 |
|
]); |
39
|
11 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get base uri. |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
1 |
|
public function baseUri() |
47
|
|
|
{ |
48
|
1 |
|
return $this->baseUri; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Send HTTP GET request. |
53
|
|
|
* |
54
|
|
|
* @param string $uri |
55
|
|
|
* |
56
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
57
|
|
|
*/ |
58
|
6 |
|
public function get($uri) |
59
|
|
|
{ |
60
|
6 |
|
return $this->request('GET', $uri); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Send HTTP POST request. |
65
|
|
|
* |
66
|
|
|
* @param string $uri |
67
|
|
|
* @param array $data |
68
|
|
|
* |
69
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
70
|
|
|
*/ |
71
|
3 |
|
public function post($uri, array $data = []) |
72
|
|
|
{ |
73
|
3 |
|
return $this->request('POST', $uri, [ |
74
|
3 |
|
'form_params' => $data, |
75
|
3 |
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Parse JSON response. |
80
|
|
|
* |
81
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
4 |
|
public function parseJsonResponse(ResponseInterface $response) |
86
|
|
|
{ |
87
|
4 |
|
return json_decode($response->getBody(), 1); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Parse HTML response. |
92
|
|
|
* |
93
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
94
|
|
|
* |
95
|
|
|
* @return \Symfony\Component\DomCrawler\Crawler |
96
|
|
|
*/ |
97
|
3 |
|
public function parseHtmlResponse(ResponseInterface $response) |
98
|
|
|
{ |
99
|
3 |
|
$html = $response->getBody()->getContents(); |
100
|
|
|
|
101
|
3 |
|
return new Crawler($html); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Send HTTP GET request and JSON response. |
106
|
|
|
* |
107
|
|
|
* @param string $uri |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
3 |
|
public function getAndParseJson($uri) |
112
|
|
|
{ |
113
|
3 |
|
$response = $this->get($uri); |
114
|
|
|
|
115
|
3 |
|
return $this->parseJsonResponse($response); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Send HTTP POST request. |
120
|
|
|
* |
121
|
|
|
* @param string $uri |
122
|
|
|
* @param array $data |
123
|
|
|
* |
124
|
|
|
* @return \Symfony\Component\DomCrawler\Crawler |
125
|
|
|
*/ |
126
|
3 |
|
public function postAndParseHtml($uri, array $data = []) |
127
|
|
|
{ |
128
|
3 |
|
$response = $this->post($uri, $data); |
129
|
|
|
|
130
|
2 |
|
return $this->parseHtmlResponse($response); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|