-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(dev) example replacement xmlrpc library for further references (addr…
…ess issue #86), currently for reference only
- Loading branch information
1 parent
408def1
commit 17f542a
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 W4OS_XMLRPC_Client { | ||
private $endpoint; | ||
private $timeout; | ||
|
||
public function __construct($endpoint, $timeout = 10) { | ||
$this->endpoint = $endpoint; | ||
$this->timeout = $timeout; | ||
} | ||
|
||
public function call($method, $params = []) { | ||
$request = W4OS_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 = W4OS_XMLRPC::xmlrpc_decode($response); | ||
if (W4OS_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 W4OS_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 = W4OS_XMLRPC::xmlrpc_encode_request('method', ['param1']); | ||
// $response = W4OS_XMLRPC::xmlrpc_decode($result); |