ang3/php-xmlrpc-client
Lightweight PHP XML-RPC client inspired by Ripcord. Create a client with the server URL and call remote methods with optional args. Requires the php-xmlrpc extension. Throws transport and remote exceptions for failed requests or server errors.
## Getting Started
### Minimal Setup
1. **Installation**
```bash
composer require ang3/php-xmlrpc-client
Add the namespace to your composer.json autoload or use:
use Ang3\XmlRpc\Client;
First Request
$client = new Client('http://example.com/RPC2');
$response = $client->call('methodName', ['param1', 'param2']);
print_r($response);
Key Files to Review
src/Client.php (Core client logic)src/Request.php (Request building)src/Response.php (Response handling)$client = new Client('https://yoursite.com/xmlrpc.php');
$response = $client->call('wp.getUsersBlogs', ['username']);
Instantiate Client
$client = new Client('https://api.example.com/xmlrpc', [
'timeout' => 10,
'user_agent' => 'MyApp/1.0'
]);
Batch Requests
$client->call('method1', [$arg1]);
$client->call('method2', [$arg2]);
$client->send(); // Send all queued requests
Error Handling
try {
$response = $client->call('methodName', [$arg]);
} catch (\Ang3\XmlRpc\Exception\FaultException $e) {
// Handle XML-RPC fault
log::error($e->getMessage());
} catch (\Exception $e) {
// Handle other errors
}
Laravel Service Provider
public function register()
{
$this->app->singleton('xmlrpc', function ($app) {
return new Client(config('xmlrpc.endpoint'));
});
}
Queueable Jobs
use Illuminate\Bus\Queueable;
class XmlRpcJob implements ShouldQueue
{
use Queueable;
public function handle()
{
$client = app('xmlrpc');
$client->call('processData', [$this->data]);
}
}
Caching Responses
$cacheKey = 'xmlrpc_method_' . md5(serialize($args));
$response = Cache::remember($cacheKey, 3600, function () use ($client, $method, $args) {
return $client->call($method, $args);
});
Fault Handling
FaultException with a faultCode and faultString.Data Type Mismatches
boolean vs int).is_bool(), is_int(), etc., for validation.Timeouts
$client = new Client('url', ['timeout' => 5]);
Base64 Encoding
$binaryData = base64_encode(file_get_contents('file.pdf'));
$client->call('upload', [$binaryData]);
Enable Debugging
$client = new Client('url', ['debug' => true]);
Logs requests/responses to storage/logs/xmlrpc.log.
Inspect Raw Requests
$request = $client->getRequest();
file_put_contents('debug.xml', $request->getXml());
Custom Request/Response Classes
Extend \Ang3\XmlRpc\Request or \Ang3\XmlRpc\Response for custom logic.
Middleware
Implement \Ang3\XmlRpc\Middleware\MiddlewareInterface for request/response filtering.
Transport Layer
Override the HTTP client by implementing \Ang3\XmlRpc\Transport\TransportInterface.
Default Headers
The client adds Content-Type: text/xml and User-Agent automatically. Override via:
$client = new Client('url', ['headers' => ['X-Custom' => 'Value']]);
SSL Verification Disable with:
$client = new Client('url', ['verify_ssl' => false]);
(Use cautiously in production.)
PHP 8.0 Compatibility
$response = $client->call('methodName', ['param1' => 'value1', 'param2' => 123]);
Reuse Client Instances Instantiate once and reuse for multiple calls to avoid connection overhead.
Compress Large Payloads Enable gzip compression for large requests:
$client = new Client('url', ['compression' => true]);
NO_UPDATE_NEEDED would not apply here as the PHP 8.0 compatibility addition warrants updating the **Configuration Quirks** section.
How can I help you explore Laravel packages today?