-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseWpApi.php
151 lines (128 loc) · 3.94 KB
/
BaseWpApi.php
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Created by PhpStorm.
* User: Nabeel
* Date: 2015-09-28
* Time: 10:23 AM
*/
namespace ShortCirquit\WordPressApi;
class BaseWpApi
{
public $baseUrl;
private $_lastHeaders;
public function getLastHeaders()
{
return $this->_lastHeaders;
}
protected $curlOptions;
protected function get($url, $params = [])
{
return $this->send(
new ApiRequest(
[
'method' => 'GET',
'url' => $url,
'params' => $params,
]
)
);
}
protected function delete($url, $params = [])
{
return $this->send(
new ApiRequest(
[
'method' => 'DELETE',
'url' => $url,
'params' => $params,
]
)
);
}
protected function post($url, $params = [], $body = null)
{
return $this->send(
new ApiRequest(
[
'method' => 'POST',
'url' => $url,
'params' => $params,
'body' => $body,
]
)
);
}
protected function put($url, $params = [], $body = null)
{
return $this->send(
new ApiRequest(
[
'method' => 'PUT',
'url' => $url,
'params' => $params,
'body' => $body,
]
)
);
}
protected function requestFilter(ApiRequest $request)
{
return $request;
}
protected function send(ApiRequest $request)
{
$request = $this->requestFilter($request);
$url = $this->composeUrl($request->url, $request->params);
$curlResource = curl_init($url);
$curlOptions = $this->curlOptions;
if ($request->body != null)
{
$curlOptions[CURLOPT_POST] = true;
$curlOptions[CURLOPT_POSTFIELDS] = $request->body;
}
foreach ($request->curlOptions as $option => $value)
{
$curlOptions[$option] = $value;
}
foreach ($curlOptions as $option => $value)
{
curl_setopt($curlResource, $option, $value);
}
curl_setopt($curlResource, CURLOPT_CUSTOMREQUEST, $request->method);
curl_setopt($curlResource, CURLOPT_HTTPHEADER, $request->headers);
curl_setopt($curlResource, CURLOPT_HEADER, true);
$response = curl_exec($curlResource);
$responseHeaders = curl_getinfo($curlResource);
$this->_lastHeaders = substr($response, 0, $responseHeaders['header_size']);
$response = substr($response, $responseHeaders['header_size']);
$errorNumber = curl_errno($curlResource);
$errorMessage = curl_error($curlResource);
curl_close($curlResource);
if ($errorNumber > 0)
{
$message = 'Curl error requesting "' .
$url . '": #' . $errorNumber .
' - ' . $errorMessage;
throw new WpApiException(500, $message);
}
if (strncmp($responseHeaders['http_code'], '20', 2) !== 0)
{
throw new WpApiException($responseHeaders['http_code'], $responseHeaders['http_code'] . ': ' . $response);
}
$result = json_decode($response, true);
if ($result == null)
{
parse_str($response, $result);
}
return $result;
}
private function composeUrl($url, $params = null)
{
$result = $this->baseUrl . $url;
if ( ! empty($params))
{
$result .= '?' . http_build_query($params, '', '&', PHP_QUERY_RFC3986);
}
return $result;
}
}