-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-xmlrpc.php
More file actions
94 lines (78 loc) · 2.59 KB
/
Copy pathclass-xmlrpc.php
File metadata and controls
94 lines (78 loc) · 2.59 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* XML-RPC protocol support
*
* Replace the deprecated php xmlrcp extension with a pure php implementation.
*
* Currently for reference and evaluation only, not yet used in the project.
*
* https://php.watch/versions/8.0/xmlrpc
*
**/
# Example with phpxmlrpc/phpxmlrpc composer package
# composer require phpxmlrpc/phpxmlrpc
use PhpXmlRpc\Client;
use PhpXmlRpc\Request;
use PhpXmlRpc\Value;
class OS_XMLRPC_Client {
private $endpoint;
private $timeout;
public function __construct($endpoint, $timeout = 10) {
$this->endpoint = $endpoint;
$this->timeout = $timeout;
}
public function call($method, $params = []) {
$request = OS_XMLRPC::xmlrpc_encode_request($method, $params);
$context = stream_context_create([
'http' => [
'method' => "POST",
'header' => "Content-Type: text/xml",
'timeout' => $this->timeout,
'content' => $request
]
]);
$response = file_get_contents($this->endpoint, false, $context);
if ($response === false) {
throw new Exception("Failed to connect to XML-RPC server");
}
$result = OS_XMLRPC::xmlrpc_decode($response);
if (OS_XMLRPC::xmlrpc_is_fault($result)) {
throw new Exception($result['faultString'], $result['faultCode']);
}
return $result;
}
}
/**
* Static class to providing drop-in replacement for the deprecated php xmlrpc extension.
*/
class OS_XMLRPC {
public static function xmlrpc_encode($value) {
return new Value($value);
}
public static function xmlrpc_decode($xml_response) {
if (is_string($xml_response)) {
$response = new PhpXmlRpc\Response($xml_response);
return $response->value();
}
return $xml_response->scalarval();
}
public static function xmlrpc_encode_request($method, $params) {
$xmlrpcParams = array_map(function($param) {
return new Value($param);
}, $params);
$request = new Request($method, $xmlrpcParams);
return $request->serialize();
}
public static function xmlrpc_is_fault($response) {
if ($response instanceof PhpXmlRpc\Response) {
return $response->faultCode() !== 0;
}
return false;
}
public static function xmlrpc_server_create() {
return new PhpXmlRpc\Server();
}
}
// Usage remains similar:
// $request = OS_XMLRPC::xmlrpc_encode_request('method', ['param1']);
// $response = OS_XMLRPC::xmlrpc_decode($result);