简体   繁体   English

我的应用程序正在向服务器发送 XML 文件:需要一些关于编写 PHP 脚本以解析 XML 文件的指导

[英]my app is sending an XML file to a server: need some guidance on writing a PHP script to parse the XML file

So my iPhone app sends a shopping cart in a XML file to a URL through POST.因此,我的 iPhone 应用程序通过 POST 将 XML 文件中的购物车发送到 URL。 Here is the line of code that does that这是执行此操作的代码行

NSString *pathToSerializedCart = [rootPath stringByAppendingPathComponent:@"serializedCart.plist"];
NSString *shoppingCartString;
NSData *serializedData;
if (![fileManager fileExistsAtPath:pathToSerializedCart])
   {
    NSLog(@"ERROR:\nCouldnt find serialized cart in documents folder.");
    return;
   }


    serializedData = [NSData dataWithContentsOfFile:pathToSerializedCart];
    shoppingCartString = [[NSString alloc] initWithData:serializedData
                                                        encoding:NSUTF8StringEncoding];
    NSLog(@"%@", shoppingCartString);
    [shoppingCartString release];


    //==========================================================================


NSData *returnData = [NSURLConnection sendSynchronousRequest:request 
                                           returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] 
                          initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
[returnString release];

Now what Id like to do is put a script on the server in the file welcome.php so that the contents of this XML file are echoed to the browser.现在我想做的是在服务器上的文件welcome.php中放置一个脚本,以便将这个 XML 文件的内容回显到浏览器。

I've looked at many examples but they all talk about the situation where the XML file is present in the same directory as the PHP file on the server.我看过很多示例,但它们都在谈论 XML 文件与服务器上的 PHP 文件位于同一目录中的情况。 I haven't been able to find examples of PHP code that actually is evoked from an app.我一直无法找到实际从应用程序中调用的 PHP 代码示例。

Could someone please point me to the right direction.有人可以指出我正确的方向。

Thanks谢谢

UPDATE After seeing your full objc, I think the POST value name in question is userfile and I've updated the code below.更新在看到你的完整 objc 之后,我认为有问题的userfile值名称是用户文件,我已经更新了下面的代码。

In PHP, the filename location of your uploaded file will reside in $_FILES['userfile']['tmp_name'] .在 PHP 中,您上传文件的文件名位置将驻留在$_FILES['userfile']['tmp_name']中。 It is recommended to var_dump($_FILES) so you can understand how PHP treats the uploaded file.建议使用var_dump($_FILES)以便您了解 PHP 如何处理上传的文件。

The function file_get_contents() will read that temporary filename and return its contents as a string, which you can load into DOMDocument, as in the last bit of code below.. function file_get_contents()将读取该临时文件名并将其内容作为字符串返回,您可以将其加载到 DOMDocument 中,如下面的最后一段代码所示。

Recommended reading: PHP docs on the $_FILES superglobal array推荐阅读: $_FILES _FILES 超全局数组上的 PHP 文档

You'll need to retrieve your XML data from the $_POST[] superglobal, and then you can parse it with simplexml_load_string() .您需要从$_POST[]超全局中检索 XML 数据,然后您可以使用simplexml_load_string()对其进行解析。

// Sorry I'm unfamiliar with objC, so I can't glean the actual POST 
// value name from your code
// UPDATE misunderstood. the uploaded file is in $_FILES
// indexed by the POST key name.

// In development you can inspect your POST...
// To see the contents of your POST in PHP, do:
var_dump($_POST);
// Also check the contents of $_FILES
var_dump($_FILES);

// Your file is stored in the temp directory 
$xmlfile = $_FILES['userfile']['tmp_name'];

// Load it with SimpleXML
$xml = simplexml_load_file($xmlfile);

Or instead of SimpleXML I usually prefer the more flexible PHP DOMDocument library:或者我通常更喜欢更灵活的 PHP DOMDocument库,而不是 SimpleXML:

$xmlfile = $_FILES['userfile']['tmp_name'];
$dom = new DOMDocument();
$dom->loadXML(file_get_contents($xmlfile));

// Now parse it as necessary using DOM manipulators
$tags = $dom->getElementsByTagName("sometag");
foreach ($tags as $sometag) {
   // whatever...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM