Reputation: 4155
So I am a totally new to PHP. In fact, I know nothing about it. I am developing an iPhone app and aiming to get a list of items from a server. My PHP script is literally just a "echo" command.
I was wondering what would be the best way to format my list on PHP so it will look as close as possible (if not identical) to an NSArray in objective C.
So for example if my list is: AAA, BBB, CCC, DDD
By running this in obj-C:
NSArray *myAwesomeArray = [[NSArray alloc] initWithObjects: @"AAA", @"BBB", @"CCC", @"DDD", nil];
I will get this, which is what I want:
myAwesomeArray IS (
AAA,
BBB,
CCC,
DDD
)
When doing this with PHP like this (again, probably the wrong formatting for that purpose):
echo "AAA, BBB, CCC, DDD"
And this on the objc side:
NSString *urlString = @"http://......";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setTimeoutInterval:60.0];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *myPHPArray = [[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] encoding:NSUTF8StringEncoding];
NSArray *myArray = [myPHPArray componentsSeparatedByString:@","];
I am getting this in objc for myArray:
myArray is (
AAA,
" BBB",
" CCC",
" DDD "
)
which is kind of messed up...
Appreciate any help. thanks!
Upvotes: 0
Views: 433
Reputation: 678
You should use a XML. If you use plist format you can use NSArray *foo = [responseString proprietyList]; or something like this. Or you can use an NSXMLParser ;)
Upvotes: 0
Reputation: 85358
From your comment about knowing very little about PHP, this is a basic example of a webservice that can return either XML or JSON that connects to a MySQL Database. Again very basic but is a working example and should be easy enough to get you started.
XML or JSON are supported in both iOS and PHP and you should have no problems finding examples on either. I would also suggest looking at Webservice Protocols to better understand the types of services available.
Upvotes: 1
Reputation: 5935
You need to adopt a common protocol that both PHP and the iOS speak. I recommend JSON. There is JSON support for PHP, and now JSON support in iOS 5 native (or you can use one of the third party libraries). Simply arrange your data in the structures you want, encode it into JSON, transmit it, and have the JSON decode on iOS into NSArrays and NSDictionaries.
Upvotes: 1