mirror of
https://github.com/GuduleLapointe/opensim-helpers.git
synced 2026-08-14 08:52:11 +00:00
(dev) example replacement xmlrpc library for further references (merge from w4os)
This commit is contained in:
@@ -180,7 +180,7 @@ function opensim_format_tp( $uri, $format = TPLINK, $sep = "\n" ) {
|
||||
extract( $uri_parts );
|
||||
|
||||
$regionencoded = urlencode( $region );
|
||||
$region_hop = str_replace( ' ', '%20', $region );
|
||||
$region_hop = str_replace( ' ', '%20', $region );
|
||||
$pos_mandatory = ( empty( $pos ) ) ? '128/128/25' : $pos;
|
||||
$links = array();
|
||||
if ( $format & TPLINK_TXT ) {
|
||||
|
||||
@@ -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);
|
||||
Reference in New Issue
Block a user