Skip to content

Commit c295cc5

Browse files
committed
Restore syntax highlighting in README
1 parent a05b5ec commit c295cc5

File tree

1 file changed

+122
-84
lines changed

1 file changed

+122
-84
lines changed

README.markdown

+122-84
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@ https://github.com/tcdent/php-restclient
55

66
Installation
77
-----------
8-
9-
$ php composer.phar require tcdent/php-restclient
8+
``` sh
9+
$ php composer.phar require tcdent/php-restclient
10+
```
1011

1112
Basic Usage
1213
-----------
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+
```
2526

2627
Configurable Options
2728
--------------------
@@ -38,17 +39,20 @@ Configurable Options
3839

3940
Options can be set upon instantiation, or individually afterword:
4041

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+
```
4548

4649
-or-
4750

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+
```
5256

5357
Verbs
5458
-----
@@ -82,17 +86,21 @@ option is set, it will be used to select the decoder. If no `format` option
8286
is provided, an attempt is made to extract it from the response `Content-Type`
8387
header. This pattern is configurable with the `format_regex` option.
8488

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+
```
9298

9399
Reading via ArrayAccess has been implemented, too:
94100

95-
var_dump($result['id']);
101+
``` php
102+
var_dump($result['id']);
103+
```
96104

97105
To access the decoded response as an array, call `decode_response()`.
98106

@@ -101,26 +109,32 @@ and `unserialize` functions, respectively. Overrides and additional
101109
decoders can be specified upon instantiation, or individually afterword.
102110
Decoder functions take one argument: the raw request body. Lambdas and functions created with `create_function` work, too.
103111

104-
function my_xml_decoder($data){
105-
new SimpleXMLElement($data);
106-
}
112+
``` php
113+
function my_xml_decoder($data){
114+
new SimpleXMLElement($data);
115+
}
107116

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+
```
112122

113123
-or-
114124

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+
```
118130

119131
Or, using a lambda; this particular example allows you to receive decoded JSON data as an array.
120132

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+
```
124138

125139

126140
Duplicate Headers and Parameters
@@ -129,31 +143,39 @@ When duplicate (repeated) HTTP headers are received, they are accessible via an
129143

130144
Example (unlikely) response:
131145

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+
```
135151

136152
Accessing repeated headers in the response instance:
137153

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+
```
143161

144162
Passing repeated headers and parameters:
145163

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+
```
151171

152172
Will create a query string (GET) or response body (POST, etc) with the following content:
153173

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+
```
157179

158180

159181
Multiple HTTP Status Lines
@@ -162,16 +184,20 @@ Multiple status lines returned in a single response payload are supported, and a
162184

163185
Example response (truncated):
164186

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);
170198

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+
```
175201

176202

177203
JSON Verbs
@@ -180,18 +206,22 @@ This library will never validate or construct `PATCH JSON` content, but it can b
180206

181207
`PATCH JSON` content with correct content type:
182208

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+
```
187215

188216
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.
189217

190218
`POST JSON` content with correct content type:
191219

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+
```
195225

196226

197227
Not all endpoints support all HTTP verbs
@@ -200,27 +230,35 @@ These are examples of two common workarounds, but are entirely dependent on the
200230

201231
Passing an `X-HTTP-Method-Override` header:
202232

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+
```
206238

207239
Passing a `_method` parameter:
208240

209-
$result = $api->post("/", [
210-
'_method' => "PUT"
211-
]);
241+
``` php
242+
$result = $api->post("/", [
243+
'_method' => "PUT"
244+
]);
245+
```
212246

213247

214248
Tests
215249
-----
216250
The test package includes a simple server script which returns debug information for verifying functionality. Start the server first, then run tests:
217251

218-
$ php -S localhost:8888 test.php
219-
$ phpunit test
252+
``` sh
253+
$ php -S localhost:8888 test.php
254+
$ phpunit test
255+
```
220256

221257
* Requires PHP > 5.5.7 in order for `getallheaders` data to populate.
222258
* If you specify an alternate port number or hostname to the PHP server you need to re-configure it in your `phpunit.xml` file:
223259

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+
```
225263

226264

0 commit comments

Comments
 (0)