1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Bouncer package. |
5
|
|
|
* |
6
|
|
|
* (c) François Hodierne <[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 Bouncer\Http; |
13
|
|
|
|
14
|
|
|
class SimpleClient |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $apiKey; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $siteUrl; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $timeout = 2; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $baseUserAgent = 'Bouncer Http'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. |
39
|
|
|
* |
40
|
|
|
* @param array|string $options |
41
|
|
|
*/ |
42
|
|
|
public function __construct($options = array()) |
43
|
|
|
{ |
44
|
|
|
// Compatibility with previous API |
45
|
|
|
if (is_string($options)) { |
46
|
|
|
$options = array('apiKey' => $options); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (isset($options['apiKey']) && is_string($options['apiKey'])) { |
50
|
|
|
$this->setApiKey($options['apiKey']); |
51
|
|
|
} |
52
|
|
|
if (isset($options['siteUrl']) && is_string($options['siteUrl'])) { |
53
|
|
|
$this->setSiteUrl($options['siteUrl']); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return SimpleClient |
59
|
|
|
*/ |
60
|
|
|
public function setApiKey($apiKey) |
61
|
|
|
{ |
62
|
|
|
$this->apiKey = $apiKey; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return SimpleClient |
69
|
|
|
*/ |
70
|
|
|
public function setSiteUrl($siteUrl) |
71
|
|
|
{ |
72
|
|
|
$this->siteUrl = $siteUrl; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getUserAgent() |
81
|
|
|
{ |
82
|
|
|
if ($this->siteUrl) { |
83
|
|
|
return "{$this->baseUserAgent}; {$this->siteUrl}"; |
84
|
|
|
} |
85
|
|
|
else { |
86
|
|
|
return $this->baseUserAgent; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function request($method, $url, $data = null) |
91
|
|
|
{ |
92
|
|
|
$userAgent = $this->getUserAgent(); |
93
|
|
|
$options = array( |
94
|
|
|
'http' => array( |
95
|
|
|
'timeout' => $this->timeout, |
96
|
|
|
'method' => $method, |
97
|
|
|
'header' => "User-Agent: {$userAgent}\r\n" |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
if ($this->apiKey) { |
101
|
|
|
$options['http']['header'] .= "Api-Key: {$this->apiKey}\r\n"; |
102
|
|
|
} |
103
|
|
|
if ($data) { |
104
|
|
|
$content = json_encode($data); |
105
|
|
|
$length = strlen($content); |
106
|
|
|
$options['http']['header'] .= "Content-Type: application/json\r\n"; |
107
|
|
|
$options['http']['header'] .= "Content-Length: {$length}\r\n"; |
108
|
|
|
$options['http']['content'] = $content; |
109
|
|
|
} |
110
|
|
|
$context = stream_context_create($options); |
111
|
|
|
$result = @file_get_contents($url, false, $context); |
112
|
|
|
if ($result) { |
113
|
|
|
$response = json_decode($result, true); |
114
|
|
|
return $response; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function get($url) |
119
|
|
|
{ |
120
|
|
|
return self::request('GET', $url); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function post($url, $data = null) |
124
|
|
|
{ |
125
|
|
|
return self::request('POST', $url, $data); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|