来自iOS App New Post的XMLRPC

时间:2014-08-06 21:16:01

标签: ios wordpress xml-rpc

我正在尝试为我的应用添加功能,以便将新文章发布到wordpress博客。我知道Wordpress有XMLRPC,但我在实现wp.newPost方面遇到了问题,因为Ruby PHP或JAVA之外的文档很少。

以下是我在应用中的内容:

-(IBAction)postNews {
    NSURL *xmlrpcURL = [NSURL URLWithString:@"https://myurl.wordpress.com/xmlrpc.php"];
    NSString *username = @"email@yahoo.com";
    NSString *password = @"password";
    NSString *title = @"Test";
    NSString *content = @"This is a test of posting to the news section from the app.";

    NSString *myRequestString = [NSString stringWithFormat:@"username=%@&password=%@&content=%@", username, password, title];

    // Create Data from request
    NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: xmlrpcURL];
    // set Request Type
    [request setHTTPMethod: @"POST"];
    // Set content-type
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    // Set Request Body
    [request setHTTPBody: myRequestData];
    // Now send a request and get Response
    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
    // Log Response
    NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",response);


}

我经常得到答复:

<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
  <fault>
    <value>
      <struct>
        <member>
          <name>faultCode</name>
          <value><int>-32700</int></value>
        </member>
        <member>
          <name>faultString</name>
          <value><string>parse error. not well formed</string></value>
        </member>
      </struct>
    </value>
  </fault>
</methodResponse>

我做错了什么?

3 个答案:

答案 0 :(得分:1)

好的,对于那些试图这样做的人来说,Obj-C的文档很难找到,但这就是我所做的。我首先从here导入了XMLRPC入门套件。接下来,在我的应用程序中,我按照它的建议定义了服务器用户名和密码,在我的操作中,我使用NSDictionary和NSArray来完成帖子。再次,这是一个简单的文本发布到wordpress博客。

NSString *server = kWordpressBaseURL;
    XMLRPCRequest *reqFRC = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
    NSDictionary* filter = @{
                             @"post_type": @"post",
                             @"post_status": @"publish",
                             @"post_title": @"Test Title",
                             @"post_content": @"Test Content",
                            };
    NSArray *postParams = @[ @0, kWordpressUserName, kWordpressPassword, filter, @[@"post_title"]];    [reqFRC setMethod:@"wp.newPost" withObjects:postParams];

    //The result for this method is a string so we know to send it into a NSString when making the call.
    NSString *result = [self executeXMLRPCRequest:reqFRC];

    [reqFRC release]; //Release the request

    //Basic error checking
    if( ![result isKindOfClass:[NSString class]] ) //error occured.

    NSLog(@"demo.sayHello Response: %@", result);

显然,您可以为自己的博文内容提供文字字段,但这样做效果很好!

答案 1 :(得分:1)

你可以使用xmlrpc作为给定代码添加新帖子

XMLRPCRequest * req = [[XMLRPCRequest alloc] initWithURL:[NSURL URLWithString:@&#34;您的网址名称&#34;]];

NSArray * yourparameter = @ [@ 0,@&#34;您的用户ID&#34;,@&#34;您的密码&#34;];

[request setMethod:@&#34; wp.newPost&#34; withParameters:yourparameter];

XMLRPCResponse * saveRessponse = [XMLRPCConnection sendSynchronousXMLRPCRequest:req error:nil];

NSLog(@&#34;响应是%@&#34;,[saveRessponse object]);

答案 2 :(得分:0)

您可以使用xml-rpc作为

添加新帖子
XMLRPCRequest *reqFRC = [[XMLRPCRequest alloc] initWithURL:[NSURL URLWithString:@"your url name"]];

//在此处设置您的网址。

 NSArray *params = @[@0,@"your user id",@"your password"]; 

//在此处添加您的网址参数。

[request setMethod:@"wp.newPost" withParameters:params]; // To add new post.

XMLRPCResponse *nodeSaveRessponse = [XMLRPCConnection sendSynchronousXMLRPCRequest:request error:nil];

NSLog(@"server response :%@",[nodeSaveRessponse object]);