我正在尝试向我的应用程序添加将新文章发布到wordpress博客的功能。我知道Wordpress具有XMLRPC,但是在实现wp.newPost时遇到了问题,因为在Ruby PHP或JAVA之外几乎没有任何文档。
这是我的应用程序中的内容:
-(IBAction)postNews {
NSURL *xmlrpcURL = [NSURL URLWithString:@"https://myurl.wordpress.com/xmlrpc.php"];
NSString *username = @"[email protected]";
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>
我在做什么错呢?
最佳答案
好的,对于那些尝试这样做的人来说,很难找到Obj-C的文档,但这就是我所做的。我首先从here导入了XMLRPC Starter Kit。接下来,在我的应用程序中,按照提示定义服务器名和密码,并在我的操作中使用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);
显然,您可以从文本字段中提取文本字段,但这很好用!
关于ios - iOS App的XMLRPC新文章,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25170366/