forked from educoder/pest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PestJSON.php
45 lines (40 loc) · 1.55 KB
/
PestJSON.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
<?php
require_once 'Pest.php';
/**
* Small Pest addition by Egbert Teeselink (http://www.github.com/eteeselink)
*
* Pest is a REST client for PHP.
* PestJSON adds JSON-specific functionality to Pest, automatically converting
* JSON data resturned from REST services into PHP arrays and vice versa.
*
* In other words, while Pest's get/post/put/delete calls return raw strings,
* PestJSON return (associative) arrays.
*
* In case of >= 400 status codes, an exception is thrown with $e->getMessage()
* containing the error message that the server produced. User code will have to
* json_decode() that manually, if applicable, because the PHP Exception base
* class does not accept arrays for the exception message and some JSON/REST servers
* do not produce nice JSON
*
* See http://github.com/educoder/pest for details.
*
* This code is licensed for use, modification, and distribution
* under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License)
*/
class PestJSON extends Pest
{
public function post($url, $data, $headers=array()) {
return parent::post($url, json_encode($data), $headers);
}
public function put($url, $data, $headers=array()) {
return parent::put($url, json_encode($data), $headers);
}
protected function prepRequest($opts, $url) {
$opts[CURLOPT_HTTPHEADER][] = 'Accept: application/json';
$opts[CURLOPT_HTTPHEADER][] = 'Content-Type: application/json';
return parent::prepRequest($opts, $url);
}
public function processBody($body) {
return json_decode($body, true);
}
}