How i use Php in iphone
I am new in iphone,in my app 开发者_运维百科required data from the server.
1)mysql database uploaded on the server 2)With the help of php code i fetch the data from the server
problem is that how i integrate php in my iphone please solve my problem, give any reference.
Thanks,
I'm not sure to understand your problem, but you can't use PHP on iPhone.
If you need data from an external database, create web services to get the data in JSON, XML, or what suits you best.
Your iPhone application will connect to a specific URL, and get the data from a (PHP) script that will query the database and return the result in a specific format.
The best way in my opinion is to use JSON (see http://code.google.com/p/json-framework/).
You can make requests to your server using this code
NSError *error;
NSString *post =[NSString stringWithFormat:@"any-data=%@",
myData];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://your-server.com/your-script.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Data: %@",data);
NSDictionary* parsedData = [data JSONValue];
You then can access the data using
[parsedData valueForKeyPath:@"myVar"]
Hope this hepls,
Miguel
You could look at passing arrays of objects from the server to the iPhone in the form of a plist file. Have the server generate the plist in the appropriate format on a url and then on the device:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"http://www.example.com/myplist.php"];
NSArray *array = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"http://www.example.com/myplist.php"];
If your unsure what the format is for plist files create one in your xcode project and use that as a template.
精彩评论