-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGuzzle.php
More file actions
44 lines (31 loc) · 815 Bytes
/
Guzzle.php
File metadata and controls
44 lines (31 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace Essence\Http\Client;
use Essence\Http\Client;
use Essence\Http\Exception;
/**
* Handles HTTP related operations through Guzzle.
*/
class Guzzle implements Client {
private $options;
private $client;
public function __construct($base_uri='http://example.com', $timeout=2.0) {
$this->client = new \GuzzleHttp\Client([
'base_uri' => $base_uri,
'timeout' => $timeout,
]);
$this->options = [];
}
public function setUserAgent($agent) {
$this->options = [
'User-Agent' => $agent
];
}
public function get($url) {
try {
$response = $this->client->request('GET', $url, $this->options);
} catch (\GuzzleHttp\Exception\RequestException $e) {
throw new Exception($url, $e->getResponse()->getStatusCode());
}
return (string)$response->getBody();
}
}