【问题标题】:using c# to create node in drupal使用c#在drupal中创建节点
【发布时间】:2012-02-10 12:54:59
【问题描述】:

我需要制作一个(“webservice”)c# 应用程序,它可以使用 xmlrpc 为 drupal 7 创建/更新/删除节点。每次我运行我的应用程序时,我都会从 xmlrpc 文件(库)中得到错误。我尝试使用 xmlrpc 来查找 C# 的代码/文档以连接到 drupal,但徒劳无功。 如果您能指出正确的方向,或者与我分享一些 c# 代码,我会很好。

{
[XmlRpcUrl("http://testing/testserver")]
public interface IDrupalServices
{
    [XmlRpcMethod("node.get")]
    XmlRpcStruct NodeLoad(int nid, string[] field);
    [XmlRpcMethod("node.save")]
    void NodeSave(XmlRpcStruct node);
}

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();

        IDrupalServices drupal = XmlRpcProxyGen.Create<IDrupalServices>();

        int nid = 227;
        string[] fields = new string[] { };

        XmlRpcStruct node = drupal.NodeLoad(nid, fields);

        string teaser = node["teaser"].ToString();
        welcomeTxt.Text = teaser;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string title = txtTitle.Text;
        string body = txtBody.Text;

        IDrupalServices drupal = XmlRpcProxyGen.Create<IDrupalServices>();

        XmlRpcStruct node = new XmlRpcStruct();

        node["id"] = 1001;
        node["title"] = title;
        node["body"] = body;
        node["teaser"] = body;
        node["format"] = 1;
        node["type"] = "webservice";
        node["promote"] = false;

        drupal.NodeSave(node);
        MessageBox.Show("The post was been created!");

    }
}
}

运行此程序后,我收到错误:服务器返回错误异常:[-32601] 服务器错误。未指定请求的方法 node.get。 - 在 XmlRpcSerializer.cs 中

谢谢你, 弗洛林

【问题讨论】:

  • 请发布您尝试过的示例代码,以及您收到的实际错误消息。查看tinyurl.com/so-hints

标签: c# web-services drupal xml-rpc


【解决方案1】:

如果您使用的是 Drupal 7,则必须使用没有 node.get 方法(或 node.save 碰巧)的服务 3。它们已分别替换为 node.retrievenode.createnode.update

您可以在 Services 模块文件夹的 resources/node_resource.inc 文件中查看所有可用的方法。

更新

在内部使用drupal_execute 提交节点,这是用于提交表单的函数。由于 body 是 Drupal 中的一个字段,因此它应该是这种格式的多维数组(PHP 版本):

$data["body"][$language][0]["value"]

$language 将是节点的特定语言,或者 und 用于未定义的语言(除非您正在处理多语言站点,und 通常是要走的路)。您需要构建一个类似于 C# 代码中的数组,Drupal 应该保存它。

另一个更新

Java XML-RPC client example for Services 使用 HashMap 类型来执行此操作,所以我最好的猜测是您可以使用 Dictionary(尽管看起来不必要地复杂):

var innerValue = new Dictionary<string, string>();
innerValue.Add("value", txtBody.Text);

var language = new Dictionary<int, Dictionary<string, string>>();
language.Add(0, innerValue);

var body = new Dictionary<string, Dictionary<int, Dictionary<string, string>>>();
body.Add("und", language);

node["body"] = body;

自从我用 C# 编码以来已经有几年了,所以请原谅其中的任何错误。我也很确定它可以更有效地声明,但老实说我已经忘记了大部分语言!

【讨论】:

  • 谢谢,这行得通。但我仍然对节点的主体有问题,如果我将文本添加到节点[“body”],它将不会出现。你能告诉我为什么吗?
  • @pretender:这与 Drupal 期望节点上的字段被格式化的方式有关(主体是 Drupal 7 中的字段)。我已经更新了答案,希望对您有所帮助
  • 我做了一个数组 [,,] 但徒劳无功。你能否分享一些代码,如果它不是太多的要求。我欠你一个。谢谢!
  • @pretender:我又试了一次 :)
  • 感谢代码:D 应用程序运行,但是当我点击按钮创建节点时,它在 XmlRpcStruct.cs 中提供和错误 --- System.Collections.Generic.Dictionary2[System.String,System.Collections.Generic.Dictionary2 [System.Int32,System.Collections.Generic.Dictionary`2[System.String,System.String]]] 无法映射到 XML-RPC 类型
【解决方案2】:

简的回答不太对。如果您使用的是 cook xmlrpc 库,您需要做的就是:

        XmlRpcStruct postStruct = new XmlRpcStruct();
        postStruct.Add("type", "article");
        postStruct.Add("title", "wohoo another test");

        XmlRpcStruct postBodyStructParams = new XmlRpcStruct();
        postBodyStructParams.Add("value", "My body yaaay");
        postBodyStructParams.Add("format", "filtered_html");

        XmlRpcStruct[] postBodyStructParamsArr = new XmlRpcStruct[1];
        postBodyStructParamsArr[0] = postBodyStructParams;

        XmlRpcStruct postBodyStruct = new XmlRpcStruct();
        postBodyStruct.Add("und", postBodyStructParamsArr);

        postStruct.Add("body", postBodyStruct);

不幸的是,body 参数需要是 "und" 键下只有一个值的结构数组。我将此归咎于 drupal xmlrpc API 的草率。

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多