So my iPhone app sends a shopping cart in a XML file to a URL through POST. 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;
}
serial开发者_如何学PythonizedData = [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.
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. I haven't been able to find examples of PHP code that actually is evoked from an app.
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.
In PHP, the filename location of your uploaded file will reside in $_FILES['userfile']['tmp_name']
. It is recommended to var_dump($_FILES)
so you can understand how PHP treats the uploaded file.
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..
Recommended reading: PHP docs on the $_FILES
superglobal array
You'll need to retrieve your XML data from the $_POST[]
superglobal, and then you can parse it with 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:
$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...
}
精彩评论