简体   繁体   中英

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 ?

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']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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