Search code examples
phpxml-rpcpear

PEAR XML_RPC_encode vs PHP xmlrpc_encode_request


I am exploring using a XMLRPC service to communicate between a Drupal system and another part of our website. I found some sample code for PHP's php_xmlrpc extension, but discovered our web host doesn't support that extension.

Instead, they provide the PEAR XML_RPC package

It appears the two methods encode very differently.

The PHP code I used to setup the request, based on http://drupal.org/node/339845

$method_name = 'user.login';
$user_credentials = array(
  0 => 'example.user',
  1 => 'password',
);

// any user-defined arguments for this service
// here we use the login credentials we specified at the top of the script
$user_args = $user_credentials;
$required_args=array();

// add the arguments to the request
foreach ($user_args as $arg) {
  array_push($required_args, $arg);
}

...then call the XMLRPC functions here...

I tested php_xmlrpc with WAMPserver on my PC, and function xmlrpc_encode_request ( http://us.php.net/manual/en/function.xmlrpc-encode-request.php ) from php_xmlrpc returns what I need, like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<methodCall>
<methodName>user.login</methodName>
<params>
 <param>
  <value>
   <string>example.user</string>
  </value>
 </param>
<param>
  <value>
   <string>password</string>
  </value>
 </param>
</params>
</methodCall>

whereas the PEAR XML_RPC_encode() function returns this:

Array
(
    [0] => example.user
    [1] => password
)
object(XML_RPC_Value)#1 (2) {
  ["me"]=>
  array(1) {
    ["string"]=>
    string(10) "user.login"
  }
  ["mytype"]=>
  int(1)
}

Is there another function available in PEAR XML_RPC that can encode parameters into XML?


Solution

  • The documentation is available from http://pear.php.net/manual/en/package.webservices.xml-rpc.api.php

    To get the XML marshalled output you first construct a message instead, and then use the ->serialize() method:

    $msg = new XML_RPC_Message("function", array(new XML_RPC_Value(123, "int")));
    print $msg->serialize();
    

    Your XML_RPC_encode() function is meant to wrap a plain php array into such XML_RPC_* objects.

    But obviously the PEAR class is meant to be used primarily via the XML_RPC_Client interface, which handles the raw data transformation.