我已经向服务器发送了一个基本的http帖子并收回了XML数据,所以这很好。
那么,我决定安装MAMP并创建我的第一个php页面; - )
从我所做的所有谷歌搜索中,我怀疑这是一个utf-8 /编码/换行问题,但它正在驱使我的疯狂; - )
php服务器获取http post数据,但无法转换为XML,因此在那时失败(请参阅调试结束,变量为空)。
感谢任何建议!
这是目标-c:
NSString *postString = [NSString stringWithString:@"xml=<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"];
postString = [postString stringByAppendingString:@"<note>\r\n"];
postString = [postString stringByAppendingFormat:@"<tasks>%@</tasks>\r\n", @"task1234"];
postString = [postString stringByAppendingFormat:@"<details>%@</details>\r\n", @"det1"];
postString = [postString stringByAppendingString:@"</note>\r\n"];
NSURL *url = [NSURL URLWithString:@"http://localhost:8888/xmltest.php"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [postString length]];
[req addValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req setHTTPMethod:@"POST"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
这是PHP:
<?php
$form_input = $_POST['xml'];
$objDOM = new DOMDocument();
$objDOM->loadXML($form_input);
echo "here1: $form_input \n";
$note = $objDOM->getElementsByTagName("note");
echo "here2\n";
print_r ($note);
foreach( $note as $value )
{
echo "here3\n";
$tasks = $value->getElementsByTagName("tasks");
$task = $tasks->item(0)->nodeValue;
echo "$task :: $detail <br>";
}
调试输出:
here1: <?xml version=\"1.0\" encoding=\"utf-8\"?>
<mynotes>
<note>
<tasks>task1234</tasks>
<details>details1234</details>
</note>
</mynotes>
here2
DOMNodeList Object
(
)