forked from quaderno/quaderno-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quaderno_json.php
36 lines (29 loc) · 1.17 KB
/
quaderno_json.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
<?php
/* Low level library to encode and decode messages using JSON
and sending those messages through HTTP with cURL */
abstract class QuadernoJSON {
static function exec($url, $method, $username, $password, $data=null) {
// Initialization
$ch = curl_init($url);
// Encode data in JSON
$json = $data ? json_encode($data) : null;
// cURL configuration options
$options = array (
CURLOPT_RETURNTRANSFER => true, // Accept answer
CURLOPT_USERPWD => $username . ":" . $password, // User and password
CURLOPT_CUSTOMREQUEST => $method, // HTTP method to use
CURLOPT_HTTPHEADER => array('Content-type: application/json') // JSON headers
);
if ($json) $options += array(CURLOPT_POSTFIELDS => $json);
curl_setopt_array($ch, $options);
// Get results
$result['data'] = curl_exec($ch);
$result['error'] = curl_errno($ch);
$result['format_error'] = curl_error($ch);
$result += curl_getinfo($ch);
curl_close($ch);
// Decode data
if ($result['data']) $result['data'] = json_decode($result['data'], true);
return $result;
}
}