I have an app that sends data to a PHP script with the NSURLRequest and NSURLConnexion. What I want is to send a data like "Hello world" with post method, and I want display on my app "Hello world from myPHPScript.php !" but I have no experience on web service and specially n PHP.
Here is my objective-c code which sends the request :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL
URLWithString:@"http://localhost:8888/testNSURL/index.php"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"myVariable=Hello world !";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
receiveData = [NSMutableData data];
}
And this is my PHP code :
<?php
if(isset($_REQUEST["myVariable"])) {
echo json_encode("Hello from index.php !");
}
else echo json_encode('Request failed');
?>
My questions are :
- Is my PHP code correct?
- How can I catch the
echo json_encode("Hello from index.php !");
in my obj-c app?