Tuesday, January 17, 2012

Work scratchpad for January 17

Today I am continuing my effort to retrieve contents of a Drupal node in an authenticated session. I started by setting up the XMLRPC server in Drupal 7 Services 3.

8:40 a.m. update:
Using the Poster plugin for Firefox, I was able to successfully retrieve a node while logged into Drupal in Firefox by POSTing this to mysite.com/drupal-folder/xmlrpc-endpoint/node/ :

<?xml version="1.0"?>
<methodCall>
 <methodName>node.retrieve</methodName>
 <struct>
  <member>
    <name>nid</name>
    <value><i4>19</i4></value>
   </member>
 </struct>
</methodCall>

And, predictably, when I logged out of Drupal in Firefox, the same POST from Poster was rejected as being from an anonymous user. Good.

I found this valuable comment on StackOverflow:
If you're using Drupal 7 you must be using Services 3 which doesn't have a node.get method (or node.save as it happens). They've been replaced with node.retrieve and node.create & node.update respectively.
Update 9:10

I was able to get a successful XMLRPC login response and user object from Drupal by POSTing this from Firefox Poster:
<?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>

Next step, to capture session auth data and learn how to send it back for the node request.

Update 12:15

So, it turns out that our webhost doesn't support the php_xmlrpc extension on our server. They do provide the PEAR XML_RPC package. Unfortunately, the encoding results are very different and the PEAR-produced object does not seem to be compatible with what Drupal wants. I posted a question about this difference on StackOverflow. Now, back to digging...

Update 13:40

I got a helpful response to my StackOverflow question from mario. He suggested:


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(); 

I tested that code, and the print result was:

<?xml version="1.0" encoding="UTF-8"?><methodCall><methodName>function</methodName><params><param><value><int>123</int></value></param></params></methodCall>

...exactly what I needed. Now I'll modify the example code into the actual parameters and see what I get.

Update 15:00

I spent lots of time installing PEAR XML_RPC and XML_RPC2 packages and trying to locate and repair their dependencies. No success on version 2, which was the recommended solution.

Using the old XML_RPC package, I can talk to the Drupal server and get my user authenticated. However, I have been unable to parse the server's response. I can see the raw XML it returns if I do a PHP var_dump, but attempts to use the XML_RPC methods fail.  The var_dump shows it is a proper XML_RPC object, but trying to use the ->value() method as used in the example at http://pear.php.net/manual/en/package.webservices.xml-rpc.examples.php results in fatal errors.

Example code from above reference:
if (!$resp->faultCode()) {
$val = $resp->value();
$data = XML_RPC_decode($val);
echo $data[0]['name'] . ' is at version ' . $data[0]['version'];
} else {
/*
* Display problems that have been gracefully cought and
* reported by the xmlrpc.php script
*/
echo 'Fault Code: ' . $resp->faultCode() . "\n";
echo 'Fault Reason: ' . $resp->faultString() . "\n";
}
I get this:
Fatal error: Call to a member function kindOf() on a non-object in /mypath/php/XML/RPC.php on line 1948
 even though the var_dump of $resp shows as:

object(XML_RPC_Response)#4
 Frustrated. I'm going to try another method.

No comments:

Post a Comment