1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SmartCall Restful API (v3) HTTP Client |
4
|
|
|
* |
5
|
|
|
* @author Jacques Marneweck <[email protected]> |
6
|
|
|
* @copyright 2017 Jacques Marneweck. All rights strictly reserved. |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jacques\Smartcall\HttpClient; |
11
|
|
|
|
12
|
|
|
class Client extends \GuzzleHttp\Client |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @const string Version number |
16
|
|
|
*/ |
17
|
|
|
const VERSION = '0.0.1'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Defaults to expecting that Apache Tomcat runs on port 8080 on localhost |
21
|
|
|
* (127.0.0.1). |
22
|
|
|
* |
23
|
|
|
* @var array[] |
24
|
|
|
*/ |
25
|
|
|
protected $options = [ |
26
|
|
|
'scheme' => 'https', |
27
|
|
|
'hostname' => 'localhost', |
28
|
|
|
'port' => '8080', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $options array |
33
|
|
|
* |
34
|
|
|
* @return void |
|
|
|
|
35
|
|
|
*/ |
36
|
1 |
|
public function __construct($options = []) |
37
|
|
|
{ |
38
|
|
|
/* |
39
|
|
|
* Allow on instantiation to overwrite the defaults |
40
|
|
|
*/ |
41
|
1 |
|
$this->options = array_merge( |
|
|
|
|
42
|
1 |
|
$this->options, |
43
|
1 |
|
$options |
44
|
|
|
); |
45
|
|
|
$config = [ |
46
|
1 |
|
'base_uri' => sprintf( |
47
|
1 |
|
'%s://%s:%s/', |
48
|
1 |
|
$this->options['scheme'], |
49
|
1 |
|
$this->options['hostname'], |
50
|
1 |
|
$this->options['port'] |
51
|
|
|
), |
52
|
|
|
'verify' => false, |
53
|
|
|
'headers' => [ |
54
|
1 |
|
'User-Agent' => 'SmartcallRestfulAPIClient-PHP/'.self::VERSION.' '.\GuzzleHttp\default_user_agent(), |
55
|
|
|
], |
56
|
|
|
]; |
57
|
1 |
|
parent::__construct($config); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Authenticate and get Bearer token from SmartCall |
62
|
|
|
* |
63
|
|
|
* @param string $username |
64
|
|
|
* @param string $password |
65
|
|
|
* |
66
|
|
|
* @throws Exception |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function auth($username, $password) |
71
|
|
|
{ |
72
|
|
|
try { |
73
|
|
|
$response = $this->post( |
74
|
|
|
'/webservice/auth', |
75
|
|
|
[ |
76
|
|
|
'headers' => [ |
77
|
|
|
'Authorization' => sprintf( |
78
|
|
|
'Basic %s', |
79
|
|
|
base64_encode( |
80
|
|
|
sprintf( |
81
|
|
|
'%s:%s', |
82
|
|
|
$username, |
83
|
|
|
$password |
84
|
|
|
) |
85
|
|
|
) |
86
|
|
|
), |
87
|
|
|
], |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return [ |
92
|
|
|
'status' => 'ok', |
93
|
|
|
'http_code' => $response->getStatusCode(), |
94
|
|
|
'body' => (string) $response->getBody(), |
95
|
|
|
]; |
96
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
97
|
|
|
return $this->parseError($e); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Test SmartCall is responding. |
103
|
|
|
* |
104
|
|
|
* @throws Exception |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
1 |
|
public function ping() |
109
|
|
|
{ |
110
|
|
|
try { |
111
|
1 |
|
$response = $this->get( |
112
|
1 |
|
'/webservice/test/ping' |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
return [ |
116
|
1 |
|
'status' => 'ok', |
117
|
1 |
|
'http_code' => $response->getStatusCode(), |
118
|
1 |
|
'body' => (string) $response->getBody(), |
119
|
|
|
]; |
120
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
121
|
|
|
return $this->parseError($e); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.