zendframework/zend-xmlrpc
Zend\XmlRpc provides an XML-RPC client and server implementation for PHP. Build and parse XML-RPC requests/responses, expose methods over HTTP, and support common XML-RPC types and faults—useful for integrating with legacy XML-RPC services.
Installation Add the package via Composer:
composer require zendframework/zend-xmlrpc
Basic Client Usage Create a client instance and make a request:
use Zend\XmlRpc\Client;
$client = new Client('http://example.com/RPC2');
$response = $client->call('example.method', ['param1', 'param2']);
First Use Case: Consuming an XML-RPC API Use the client to interact with an external XML-RPC service (e.g., WordPress API, legacy systems):
$client = new Client('http://wp-site.com/xmlrpc.php');
$posts = $client->call('wp.getPosts', ['1', '10']);
Request/Response Handling
call() for synchronous requests with structured parameters.$response = $client->call('method.name', ['arg1', 'arg2']);
Error Handling
Wrap calls in try-catch blocks to handle Zend\XmlRpc\Exception:
try {
$result = $client->call('faulty.method', []);
} catch (\Zend\XmlRpc\Exception\FaultException $e) {
Log::error("XML-RPC Fault: " . $e->getMessage());
}
Authentication For APIs requiring auth (e.g., WordPress), pass credentials via parameters:
$client->call('metaWeblog.newPost', [
1, // User ID
'username',
'password',
['title' => 'Test Post']
]);
Laravel Service Providers Bind the client to the container for dependency injection:
$this->app->singleton('xmlrpc.client', function ($app) {
return new Client(config('services.xmlrpc.endpoint'));
});
Configuration
Store endpoints and defaults in config/services.php:
'xmlrpc' => [
'endpoint' => 'http://api.example.com/RPC2',
'timeout' => 30,
];
Testing Use Laravel’s HTTP testing helpers to mock XML-RPC responses:
$response = $this->call('POST', '/xmlrpc', [
'data' => '<methodCall><methodName>test.method</methodName>...</methodCall>'
]);
Deprecated Package
php/xmlrpc for active maintenance.Strict XML-RPC Compliance
if (!is_array($params)) {
throw new \InvalidArgumentException('Parameters must be an array.');
}
Namespace Conflicts
Xml facade or other Xml* classes.Performance
Enable Verbose Output
Use setOptions() to debug requests:
$client->setOptions([
'traceEnabled' => true,
'exceptions' => true,
]);
Inspect Raw Requests Log the request payload before sending:
$request = $client->getLastRequest();
Log::debug('XML-RPC Request:', [$request->getMessage()]);
Custom Transport
Extend Zend\XmlRpc\Transport\AbstractHttp for custom HTTP clients (e.g., Guzzle):
class GuzzleTransport extends AbstractHttp {
protected function doRequest($request) {
$client = new \GuzzleHttp\Client();
return $client->request('POST', $this->uri, ['body' => $request]);
}
}
Middleware
Add preprocessing/processing logic via setOptions():
$client->setOptions([
'requestFilters' => [$this->addAuthHeader(...)],
'responseFilters' => [$this->parseResponse(...)],
]);
Laravel Events
Dispatch events for critical XML-RPC actions (e.g., XmlRpcCalled, XmlRpcFailed):
event(new XmlRpcCalled($method, $params));
How can I help you explore Laravel packages today?