@@ -5,23 +5,24 @@ https://github.com/tcdent/php-restclient
5
5
6
6
Installation
7
7
-----------
8
-
9
- $ php composer.phar require tcdent/php-restclient
8
+ ``` sh
9
+ $ php composer.phar require tcdent/php-restclient
10
+ ```
10
11
11
12
Basic Usage
12
13
-----------
13
-
14
- $api = new RestClient([
15
- 'base_url' => "https://api.twitter.com/1.1",
16
- 'format' => "json",
17
- // https://dev.twitter.com/docs/auth/application-only-auth
18
- 'headers' => ['Authorization' => 'Bearer '.OAUTH_BEARER],
19
- ]);
20
- $result = $api->get("search/tweets", ['q' => "#php"]);
21
- // GET http://api.twitter.com/1.1/search/tweets.json?q=%23php
22
- if($result->info->http_code == 200)
23
- var_dump($result->decode_response());
24
-
14
+ ``` php
15
+ $api = new RestClient([
16
+ 'base_url' => "https://api.twitter.com/1.1",
17
+ 'format' => "json",
18
+ // https://dev.twitter.com/docs/auth/application-only-auth
19
+ 'headers' => ['Authorization' => 'Bearer '.OAUTH_BEARER],
20
+ ]);
21
+ $result = $api->get("search/tweets", ['q' => "#php"]);
22
+ // GET http://api.twitter.com/1.1/search/tweets.json?q=%23php
23
+ if($result->info->http_code == 200)
24
+ var_dump($result->decode_response());
25
+ ```
25
26
26
27
Configurable Options
27
28
--------------------
@@ -38,17 +39,20 @@ Configurable Options
38
39
39
40
Options can be set upon instantiation, or individually afterword:
40
41
41
- $api = new RestClient([
42
- 'format' => "json",
43
- 'user_agent' => "my-application/0.1"
44
- ]);
42
+ ``` php
43
+ $api = new RestClient([
44
+ 'format' => "json",
45
+ 'user_agent' => "my-application/0.1"
46
+ ]);
47
+ ```
45
48
46
49
-or-
47
50
48
- $api = new RestClient;
49
- $api->set_option('format', "json");
50
- $api->set_option('user_agent', "my-application/0.1");
51
-
51
+ ``` php
52
+ $api = new RestClient;
53
+ $api->set_option('format', "json");
54
+ $api->set_option('user_agent', "my-application/0.1");
55
+ ```
52
56
53
57
Verbs
54
58
-----
@@ -82,17 +86,21 @@ option is set, it will be used to select the decoder. If no `format` option
82
86
is provided, an attempt is made to extract it from the response ` Content-Type `
83
87
header. This pattern is configurable with the ` format_regex ` option.
84
88
85
- $api = new RestClient([
86
- 'base_url' => "http://vimeo.com/api/v2",
87
- 'format' => "php"
88
- ]);
89
- $result = $api->get("tcdent/info");
90
- foreach($result as $key => $value)
91
- var_dump($value);
89
+ ``` php
90
+ $api = new RestClient([
91
+ 'base_url' => "http://vimeo.com/api/v2",
92
+ 'format' => "php"
93
+ ]);
94
+ $result = $api->get("tcdent/info");
95
+ foreach($result as $key => $value)
96
+ var_dump($value);
97
+ ```
92
98
93
99
Reading via ArrayAccess has been implemented, too:
94
100
95
- var_dump($result['id']);
101
+ ``` php
102
+ var_dump($result['id']);
103
+ ```
96
104
97
105
To access the decoded response as an array, call ` decode_response() ` .
98
106
@@ -101,26 +109,32 @@ and `unserialize` functions, respectively. Overrides and additional
101
109
decoders can be specified upon instantiation, or individually afterword.
102
110
Decoder functions take one argument: the raw request body. Lambdas and functions created with ` create_function ` work, too.
103
111
104
- function my_xml_decoder($data){
105
- new SimpleXMLElement($data);
106
- }
112
+ ``` php
113
+ function my_xml_decoder($data){
114
+ new SimpleXMLElement($data);
115
+ }
107
116
108
- $api = new RestClient([
109
- 'format' => "xml",
110
- 'decoders' => ['xml' => "my_xml_decoder"]
111
- ]);
117
+ $api = new RestClient([
118
+ 'format' => "xml",
119
+ 'decoders' => ['xml' => "my_xml_decoder"]
120
+ ]);
121
+ ```
112
122
113
123
-or-
114
124
115
- $api = new RestClient;
116
- $api->set_option('format', "xml");
117
- $api->register_decoder('xml', "my_xml_decoder");
125
+ ``` php
126
+ $api = new RestClient;
127
+ $api->set_option('format', "xml");
128
+ $api->register_decoder('xml', "my_xml_decoder");
129
+ ```
118
130
119
131
Or, using a lambda; this particular example allows you to receive decoded JSON data as an array.
120
132
121
- $api->register_decoder('json', function($data){
122
- return json_decode($data, TRUE);
123
- });
133
+ ``` php
134
+ $api->register_decoder('json', function($data){
135
+ return json_decode($data, TRUE);
136
+ });
137
+ ```
124
138
125
139
126
140
Duplicate Headers and Parameters
@@ -129,31 +143,39 @@ When duplicate (repeated) HTTP headers are received, they are accessible via an
129
143
130
144
Example (unlikely) response:
131
145
132
- HTTP/1.1 200 OK
133
- Content-Type: text/html
134
- Content-Type: text/html; charset=UTF-8
146
+ ```
147
+ HTTP/1.1 200 OK
148
+ Content-Type: text/html
149
+ Content-Type: text/html; charset=UTF-8
150
+ ```
135
151
136
152
Accessing repeated headers in the response instance:
137
153
138
- $result = $api->get('/');
139
- if(is_array($result->headers->content_type))
140
- var_dump($result->headers->content_type[0]);
141
-
142
- => "text/html"
154
+ ``` php
155
+ $result = $api->get('/');
156
+ if(is_array($result->headers->content_type))
157
+ var_dump($result->headers->content_type[0]);
158
+
159
+ => "text/html"
160
+ ```
143
161
144
162
Passing repeated headers and parameters:
145
163
146
- $result = $api->get('/', [
147
- 'foo[]' => ['bar', 'baz']
148
- ], [
149
- 'Accept' => ['text/json', 'application/json']
150
- ]);
164
+ ``` php
165
+ $result = $api->get('/', [
166
+ 'foo[]' => ['bar', 'baz']
167
+ ], [
168
+ 'Accept' => ['text/json', 'application/json']
169
+ ]);
170
+ ```
151
171
152
172
Will create a query string (GET) or response body (POST, etc) with the following content:
153
173
154
- GET /?foo[]=bar&foo[]=baz HTTP/1.1
155
- Accept: text/json
156
- Accept: application/json
174
+ ```
175
+ GET /?foo[]=bar&foo[]=baz HTTP/1.1
176
+ Accept: text/json
177
+ Accept: application/json
178
+ ```
157
179
158
180
159
181
Multiple HTTP Status Lines
@@ -162,16 +184,20 @@ Multiple status lines returned in a single response payload are supported, and a
162
184
163
185
Example response (truncated):
164
186
165
- HTTP/1.1 100 Continue
166
-
167
- HTTP/1.1 200 OK
168
- Cache-Control: no-cache
169
- ...
187
+ ```
188
+ HTTP/1.1 100 Continue
189
+
190
+ HTTP/1.1 200 OK
191
+ Cache-Control: no-cache
192
+ ...
193
+ ```
194
+
195
+ ``` php
196
+ $result = $api->get('/');
197
+ var_dump($result->response_status_lines);
170
198
171
- $result = $api->get('/');
172
- var_dump($result->response_status_lines);
173
-
174
- => ["HTTP/1.1 100 Continue", "HTTP/1.1 200 OK"]
199
+ => ["HTTP/1.1 100 Continue", "HTTP/1.1 200 OK"]
200
+ ```
175
201
176
202
177
203
JSON Verbs
@@ -180,18 +206,22 @@ This library will never validate or construct `PATCH JSON` content, but it can b
180
206
181
207
` PATCH JSON ` content with correct content type:
182
208
183
- $result = $api->execute("http://httpbin.org/patch", 'PATCH',
184
- json_encode([foo' => 'bar']), [
185
- 'X-HTTP-Method-Override' => 'PATCH',
186
- 'Content-Type' => 'application/json-patch+json']);
209
+ ``` php
210
+ $result = $api->execute("http://httpbin.org/patch", 'PATCH',
211
+ json_encode([foo' => 'bar']), [
212
+ 'X-HTTP-Method-Override' => 'PATCH',
213
+ 'Content-Type' => 'application/json-patch+json']);
214
+ ```
187
215
188
216
Note that your specific endpoint may not require the ` X-HTTP-Method-Override ` header, nor understand the [ correct] ( http://tools.ietf.org/html/rfc6902#section-6 ) ` application/json-patch+json ` content type.
189
217
190
218
` POST JSON ` content with correct content type:
191
219
192
- $result = $api->post("http://httpbin.org/post",
193
- json_encode(['foo' => 'bar']),
194
- ['Content-Type' => 'application/json']);
220
+ ``` php
221
+ $result = $api->post("http://httpbin.org/post",
222
+ json_encode(['foo' => 'bar']),
223
+ ['Content-Type' => 'application/json']);
224
+ ```
195
225
196
226
197
227
Not all endpoints support all HTTP verbs
@@ -200,27 +230,35 @@ These are examples of two common workarounds, but are entirely dependent on the
200
230
201
231
Passing an ` X-HTTP-Method-Override ` header:
202
232
203
- $result = $api->post("/", [], [
204
- 'X-HTTP-Method-Override' => "PUT"
205
- ]);
233
+ ``` php
234
+ $result = $api->post("/", [], [
235
+ 'X-HTTP-Method-Override' => "PUT"
236
+ ]);
237
+ ```
206
238
207
239
Passing a ` _method ` parameter:
208
240
209
- $result = $api->post("/", [
210
- '_method' => "PUT"
211
- ]);
241
+ ``` php
242
+ $result = $api->post("/", [
243
+ '_method' => "PUT"
244
+ ]);
245
+ ```
212
246
213
247
214
248
Tests
215
249
-----
216
250
The test package includes a simple server script which returns debug information for verifying functionality. Start the server first, then run tests:
217
251
218
- $ php -S localhost:8888 test.php
219
- $ phpunit test
252
+ ``` sh
253
+ $ php -S localhost:8888 test.php
254
+ $ phpunit test
255
+ ```
220
256
221
257
* Requires PHP > 5.5.7 in order for ` getallheaders ` data to populate.
222
258
* If you specify an alternate port number or hostname to the PHP server you need to re-configure it in your ` phpunit.xml ` file:
223
259
224
- ``` <php><var name="TEST_SERVER_URL" value="http://localhost:8888"/></php> ```
260
+ ``` xml
261
+ <php ><var name =" TEST_SERVER_URL" value =" http://localhost:8888" /></php >
262
+ ```
225
263
226
264
0 commit comments