-
Notifications
You must be signed in to change notification settings - Fork 12
/
quaderno_json.php
executable file
·63 lines (55 loc) · 1.62 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* Quaderno JSON
*
* Low level library to encode and decode messages using JSON
* and sending those messages through HTTP with cURL
*
* @package Quaderno PHP
* @author Quaderno <support@quaderno.io>
* @copyright Copyright (c) 2021, Quaderno
* @license https://opensource.org/licenses/MIT The MIT License
*/
abstract class QuadernoJSON
{
/**
* @param string $url
* @param string $method
* @param string $username
* @param string $password
* @param string|null $version
* @param array|null $data
*
* @return array
*/
public static function exec($url, $method, $username, $password, $version = null, $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',
$version ? 'Accept: application/json; api_version='.$version : 'Accept: application/json'
) // JSON headers
);
if ($json) $options += array(CURLOPT_POSTFIELDS => $json);
curl_setopt_array($ch, $options);
// Get results
$result = array();
$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;
}
}