Skip to content

Commit 0e9acdb

Browse files
committed
Allow passing parameters as a pre-encoded string (or something that allows casting to a string). Parameters passed as strings will not be merged with parameters specified in the default options.
Update documentation with PATCH JSON recommendations.
1 parent 0945744 commit 0e9acdb

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

README.markdown

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,36 @@ Verbs
5252
-----
5353
Four HTTP verbs are implemented as convenience methods: `get()`, `post()`, `put()` and `delete()`. Each accepts three arguments:
5454

55-
`url` - URL of the resource you are requesting. Will be prepended with the value of the `base_url` option, if it has been configured. Will be appended with the value of the `format` option, if it has been configured.
56-
`parameters` - An associative array of query parameters, which will be formatted with the URL in `GET` requests, and passed in the request body on all others.
57-
`headers` - An associative array of headers to include with the request.
55+
`url` - `string` URL of the resource you are requesting. Will be prepended with the value of the `base_url` option, if it has been configured. Will be appended with the value of the `format` option, if it has been configured.
56+
57+
`parameters` - `string` or associative `array` to be appended to the URL in `GET` requests and passed in the request body on all others. If an array is passed it will be encoded into a query string.
58+
59+
`headers` - An associative `array` of headers to include with the request.
5860

5961
You can make a request using any verb by calling `execute()` directly, which accepts four arguments: `url`, `method`, `parameters` and `headers`. All arguments expect the same values as in the convenience methods, with the exception of the additional `method` argument:
6062

61-
`method` - HTTP verb to perform the request with.
63+
`method` - `string` HTTP verb to perform the request with.
64+
65+
66+
JSON Verbs
67+
----------
68+
This library will never validate or construct `PATCH JSON` content, but it can be configured to communicate well-formed data.
69+
70+
`PATCH JSON` content with correct content type:
71+
72+
$result = $api->execute("http://httpbin.org/patch", 'PATCH',
73+
json_encode(array('foo' => 'bar')),
74+
array(
75+
'X-HTTP-Method-Override' => 'PATCH',
76+
'Content-Type' => 'application/json-patch+json'));
77+
78+
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.
79+
80+
`POST JSON` content with correct content type:
81+
82+
$result = $api->post("http://httpbin.org/post",
83+
json_encode(array('foo' => 'bar')),
84+
array('Content-Type' => 'application/json'));
6285

6386

6487
Not all endpoints support all HTTP verbs

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tcdent/php-restclient",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "A generic REST API client for PHP",
55
"type": "library",
66
"keywords": ["REST", "api", "client", "curl", "JSON", "XML"],

restclient.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct($options=array()){
2828
'headers' => array(),
2929
'parameters' => array(),
3030
'curl_options' => array(),
31-
'user_agent' => "PHP RestClient/0.1.3",
31+
'user_agent' => "PHP RestClient/0.1.4",
3232
'base_url' => NULL,
3333
'format' => NULL,
3434
'format_regex' => "/(\w+)\/(\w+)(;[.+])?/",
@@ -145,18 +145,27 @@ public function execute($url, $method='GET', $parameters=array(), $headers=array
145145
if($client->options['format'])
146146
$client->url .= '.'.$client->options['format'];
147147

148-
$parameters = array_merge($client->options['parameters'], $parameters);
148+
// Allow passing parameters as a pre-encoded string (or something that
149+
// allows casting to a string). Parameters passed as strings will not be
150+
// merged with parameters specified in the default options.
151+
if(is_array($parameters)){
152+
$parameters = array_merge($client->options['parameters'], $parameters);
153+
$paraeters_string = $client->format_query($parameters);
154+
}
155+
else
156+
$paraeters_string = (string) $parameters;
157+
149158
if(strtoupper($method) == 'POST'){
150159
$curlopt[CURLOPT_POST] = TRUE;
151-
$curlopt[CURLOPT_POSTFIELDS] = $client->format_query($parameters);
160+
$curlopt[CURLOPT_POSTFIELDS] = $paraeters_string;
152161
}
153162
elseif(strtoupper($method) != 'GET'){
154163
$curlopt[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
155-
$curlopt[CURLOPT_POSTFIELDS] = $client->format_query($parameters);
164+
$curlopt[CURLOPT_POSTFIELDS] = $paraeters_string;
156165
}
157-
elseif(count($parameters)){
166+
elseif($parameters_string){
158167
$client->url .= strpos($client->url, '?')? '&' : '?';
159-
$client->url .= $client->format_query($parameters);
168+
$client->url .= $parameters_string;
160169
}
161170

162171
if($client->options['base_url']){

0 commit comments

Comments
 (0)