1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WooCommerce REST API Client |
4
|
|
|
* |
5
|
|
|
* @category Client |
6
|
|
|
* @package Automattic/WooCommerce |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Automattic\WooCommerce; |
10
|
|
|
|
11
|
|
|
use Automattic\WooCommerce\HttpClient\HttpClient; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* REST API Client class. |
15
|
|
|
* |
16
|
|
|
* @package Automattic/WooCommerce |
17
|
|
|
*/ |
18
|
|
|
class Client |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* WooCommerce REST API Client version. |
23
|
|
|
*/ |
24
|
|
|
const VERSION = '1.1.3'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* HttpClient instance. |
28
|
|
|
* |
29
|
|
|
* @var HttpClient |
30
|
|
|
*/ |
31
|
|
|
public $http; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Initialize client. |
35
|
|
|
* |
36
|
|
|
* @param string $url Store URL. |
37
|
|
|
* @param string $consumerKey Consumer key. |
38
|
|
|
* @param string $consumerSecret Consumer secret. |
39
|
|
|
* @param array $options Options (version, timeout, verify_ssl). |
40
|
|
|
*/ |
41
|
|
|
public function __construct($url, $consumerKey, $consumerSecret, $options = []) |
42
|
|
|
{ |
43
|
|
|
$this->http = new HttpClient($url, $consumerKey, $consumerSecret, $options); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get headers from last response |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function getHeaders() |
51
|
|
|
{ |
52
|
|
|
$resp = $this->http->getResponse(); |
53
|
|
|
|
54
|
|
|
return is_object($resp) ? $resp->getHeaders() : array(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* POST method. |
59
|
|
|
* |
60
|
|
|
* @param string $endpoint API endpoint. |
61
|
|
|
* @param array $data Request data. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function post($endpoint, $data) |
66
|
|
|
{ |
67
|
|
|
return $this->http->request($endpoint, 'POST', $data); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* PUT method. |
72
|
|
|
* |
73
|
|
|
* @param string $endpoint API endpoint. |
74
|
|
|
* @param array $data Request data. |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function put($endpoint, $data) |
79
|
|
|
{ |
80
|
|
|
return $this->http->request($endpoint, 'PUT', $data); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* GET method. |
85
|
|
|
* |
86
|
|
|
* @param string $endpoint API endpoint. |
87
|
|
|
* @param array $parameters Request parameters. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function get($endpoint, $parameters = []) |
92
|
|
|
{ |
93
|
|
|
return $this->http->request($endpoint, 'GET', [], $parameters); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* DELETE method. |
98
|
|
|
* |
99
|
|
|
* @param string $endpoint API endpoint. |
100
|
|
|
* @param array $parameters Request parameters. |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function delete($endpoint, $parameters = []) |
105
|
|
|
{ |
106
|
|
|
return $this->http->request($endpoint, 'DELETE', [], $parameters); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* OPTIONS method. |
111
|
|
|
* |
112
|
|
|
* @param string $endpoint API endpoint. |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function options($endpoint) |
117
|
|
|
{ |
118
|
|
|
return $this->http->request($endpoint, 'OPTIONS', [], []); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|