Posted by kscottj on September 15, 2011 at 4:09am
I have written a javascript application that will post comments to any node in my local drupal install with and API key. The goal is to be able to use an android/iPhone mobile PhoneGap applicaiton to post content to a Drupal server.
My question is can I add topics to a Forum using the services XMLRPC interface?
I can add a node of type "forum" however that never show up in the forum.
I currently using services v2 on Drupal v6.2.2 server.
Here a simple JavaScript code snippet:
function postForumTopic(intNode,strText){
var method = "node.save";
var session=getSession();
session=signSession(session,method); //use API key , and session id to generate a valid hash
var request = new XmlRpcRequest("http://localhost/?q=services/xmlrpc",method);
request.addParam(session.hashStr);
request.addParam(DRUPAL_DOMAIN);
request.addParam(session.timeStampStr);
request.addParam(session.nounceStr);
request.addParam(session.sessid);
node_object.nid=0, // new node
node_object.type="forum";
node_object.uid=session.uid;
node_object.tnid=2;
node_object.title="XMLRPC Foum Topic";
node_object.body=strText+" \n\n "+MOBILE_EDITION+" " ;
console.debug("body="+node_object.body);
node_object.created=new Date().getTime();
request.addParam(node_object);
var response = request.send();
var xmlData=response.parseXML();
if(xmlData>1){
return true;
}
if(xmlData.faultCode ){
alert(XMLRPC_ERROR_MSG);
console.error("XMLRPC postForumTopic="+xmlData.faultCode+":"+xmlData.faultString);
return false;
}
return false;
}
Comments
Make sure node is published
Do you see the node in Drupal at all? Check its status to make sure it's published. If not, set status to 1 before saving to mark the node as published:
node_object.status=1;
Micah
the node does show up here some more of the code
Here the node javascript object:
<
pre>
var node_object = {
}
So the node does show up if I call it directly. Lets say after a call services call returns 123.
If I ran the following http://localhost/?q=/node/123 it will display the node, but, it does not show up as a forum post. So maybe the services module only works with core stuff, and not any modules (not even modules that come with a default install).
Probably need to add a forum taxonomy term
Forum module uses taxonomy terms to mark the discussion threads. Try to manually tag the node (from the edit form) with one of the forum topics taxonomy terms and see if it then appears in the forum. If so, then you need to set the node's taxonomy terms before calling node.save. This is a bit tricky because the way taxonomy terms are represented in the node object returned from node.get is not how node.save expects to get them (long story on why).
Here's a pointer to some info: http://drupal.org/node/507650
And I also commented on this issue here: http://groups.drupal.org/node/35894#comment-547684
Hope this helps.
Micah
yip it taxonomy related
I opened a forum node from the services interface I see this
[taxonomy] => Array ( [2] => stdClass Object ( [tid] => 2 [vid] => 1 [name] => general [description] => [weight] => 1 ) )"General" is my forum name. What is tid, vid?
Does anyone have an example of what the node object would look like with forum taxonomy that would work with a node.save?
tid is the term ID of the
tid is the term ID of the term (tag) identifying your forum post within a forum topic. As you can see, it's the same as the key for this object in the node's taxonomy array.
vid is the vocabulary ID of the vocabulary used by the forum module. I believe it's created automatically when you create a new forum.
the node.save method expects a two-layer array (array of arrays), where the first array is indexed by vid, and for each vid there is an array of that vocabulary's terms (indexed by tid). So for the above node, it would be:
You'll likely have multiple forum topics (not just 'general' with tid=2) so you'll need to put the correct tid.
I believe that the value of the inner array items (the "right" 2) is not important - the keys are what's important. But this is how I do it and it works.
For more info, see the above links I mentioned.
Micah
thanks
Ok thanks that should help
I am still missing something
Here my serialized XML does this look right?
<member>
<name>taxonomy</name>
<value>
<struct>
<member>
<name>1</name>
<value>
<struct>
<member>
<name>2</name>
<value>
<string>2</string>
</value>
</member>
</struct>
</value>
</member>
</struct>
</value>
</member>
This should =
[taxonomy] => Array
(
[1] => Array // 1 is vid of forum vocabulary
(
[2] => 2 // 2 is tid of forum topic
)
)
This looks OK
But I've never started with the data in XML, so not sure. If this doesn't work, make sure that you're creating arrays and not objects (not sure if the struct is fine).