0

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!

Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
TommyG
  • 4,145
  • 10
  • 42
  • 66
  • I don't think you can pass an array like this from PHP to iOS as one know nothing of the other. I would suggest looking at a webservice to interchange data from one another. This link might help: http://stackoverflow.com/questions/3152700/what-type-of-webservice-works-best-with-ios and this http://en.wikipedia.org/wiki/List_of_web_service_protocols – Phill Pafford Nov 10 '11 at 14:30
  • componentsSeparatedByString:@", " ? with space maybe? make json or xml and make parsing in app – Volodymyr B. Nov 10 '11 at 14:33
  • Thanks everyone, but since (as mentioned) I know nothing about PHP, I am not sure I could get into encoding/decoding json data... – TommyG Nov 10 '11 at 14:51

3 Answers3

1

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.

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35
1

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.

Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
0

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

Nicolò Ciraci
  • 678
  • 5
  • 23