POST xml from iPhone to PHP
I am working on one project in which I am preparing API to interact with server from iPhone.
I have created API with POST parameter xmlrequest
and in that parameter, I am passing xml content.
Everything is working correctly, except, when in xml content if any special character comes, it cuts off on server side.
My question is should I apply any header/content type on both sides, if yes, Can I get example ?
开发者_Go百科EDIT:
It is an issue of special character, in xml content I tried to replace "&" with "and" and it worked, but when I try to replace "&" with "&", it does not work.
In HTTP, GET- and POST- parameters are seperated with the special characters ?
, &
and =
. I'm sure you already have seen things like that in URLS: example.com?foo=bar&meow=woof
Your server will parse the query-string, and it assumes that everytime a &
appears, it's a new parameter. So, in order to pass such a special character, you first need to URL-encode it (on the iPhone-side). For example, the space-character, URL-encoded, would be %20
.
You can then decode the URL-encoded string on server side using urldecode()
and rawurldecode()
.
You could pass the problem areas in CDATA tags, or perhaps implement htmlentities, htmlspecialchars, etc if the XML is being generated/handled by the PHP. There is a fair bit on the subject out there.
Can you elaborate
it cuts off on server side.
Thanks to Andre_roesti, He gave me right direction. Here is my solution to encode special characters which are to be passed into POST method.
iPhone side:
@interface NSString (URLEncode)
-(NSString *)urlEncode;
@end
@implementation NSString (URLEncode)
-(NSString *)urlEncode{
return [(NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, CFSTR("=,!$&'()*+;@?\n\"<>#\t :/"),kCFStringEncodingUTF8) autorelease];
}
@end
PHP side:
I made $xmlrequest = html_entity_decode($_POST['xmlrequest']);
精彩评论