代码之家  ›  专栏  ›  技术社区  ›  user717452

来自iOS应用程序的XMLRPC新帖子

  •  1
  • user717452  · 技术社区  · 10 年前

    我正在尝试为我的应用程序添加向wordpress博客发布新文章的功能。我知道Wordpress有XMLRPC,但我在实现wp.newPost时遇到了问题,因为除了RubyPHP或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 回复  |  直到 10 年前
        1
  •  1
  •   user717452    10 年前

    好的,对于那些试图这样做的人来说,Obj-C的文档很难找到,但这里是我所做的。我首先从 here 接下来,在我的应用程序中,我按照它的建议定义了服务器用户名和密码,并且在我的操作中,我使用了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);
    

    很明显,你可以从你的博客文章内容中提取文本字段,但这非常有用!

        2
  •  1
  •   Avinash651    10 年前

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

    XMLRPCRequest*req=[[XMLRPCRequest alloc]initWithURL:[NSURL URLWithString:@“your url name”]];

    NSArray*yourparameter=@[@0,@“您的用户id”,@“密码”];

    [request setMethod:@“wp.newPost”with Parameters:yourparameter];

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

    NSLog(@“响应为%@”,[saveResponse-object]);

        3
  •  0
  •   Programming Learner    10 年前

    您可以使用xml-rpc作为

    XMLRPCRequest *reqFRC = [[XMLRPCRequest alloc] initWithURL:[NSURL URLWithString:@"your url name"]];
    

    //在此处设置您的url。

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

    //在此处添加url参数。

    [request setMethod:@"wp.newPost" withParameters:params]; // To add new post.
    
    XMLRPCResponse *nodeSaveRessponse = [XMLRPCConnection sendSynchronousXMLRPCRequest:request error:nil];
    
    NSLog(@"server response :%@",[nodeSaveRessponse object]);