Help creating files and nodes remotely using Services 2.x

Communicating with the Drupal services module can get rough for the uninitiated. While connecting and reading data is quite well documented and straightforward, creating and saving objects can sometimes mean a lot of trial and error.

Publishing content via the service module is done via the ‘node.save’ and ‘file.save’ method calls. They both take one argument, an object (though some documentation says array, they require an object), and both return the id of the node or file they create.

The while the documentation says that the node object for ‘node.save’ and the file object for ‘file.save’, this may not actually work in all cases. The methods ‘node.save’ and ‘file.save’ work by generating and submitting node or file creation form, filling it in with the data in the provided object. Since the form to create a node can be different than the node object it creates, valid node objects submitted to ‘node.save’ may not be created correctly.

For that reason, when trying to discover how you should format the fields for ‘node.save’, you should look at how those fields are submitted through the normal creation form on the web interface, and not at completed the node objects in Drupal.

For reference and to get started publishing via services, here’s the most basic node object for the ‘node.save’ method:

$node->type = $content_type;  // The machine name of the content type (i.e. 'story', 'page', 'book_page'...)
$node->uid = $author;  // The uid of the author of this node
$node->title = "";  // The title of the node
$node->body = "";  // The body of the node

And the same for a file object for ‘file.save’:

$file->file = base64_encode(file_get_contents($filepath));   //$file->file should be the base64_encode file
$file->filename = basename($filepath);  // The name of the file
$file->filepath = 'sites/www.example.com/files/';  // The path where the file should be saved on the file system
$file->filemime = "application/x-compressed";  // The mime type of the file in question
$file->filesize = filesize($filepath);  // The size of the file