0

I am trying to make a connection to post and set values to my php file locally located however when I execute this code it doesn't execute my completionHandler. am I doing something wrong in the request?? how can i check if the php is getting that post request??

objective c code

NSString *myRequestString = [NSString stringWithFormat:@"stop=%@&destination=%@&busline=%d",stop,destinationStop,busLineInUse];

NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://localhost/script.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response


NSURLSessionDataTask *uploadTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *returnData,NSURLResponse *response,NSError *error)
{
    NSLog(@"into the block");
    if (error)
    {
        NSLog(@"error posting data");
    }
    // Handle response here
    NSString *responseData = [[NSString alloc] initWithBytes:[returnData bytes]
    length:[returnData length] encoding:NSUTF8StringEncoding];

     // Log Response
     NSLog(@"%@",responseData);
}];

NSLog(@"after");
[uploadTask resume];

on the php side Im trying to check if it has received a post request like this, after I run the code in objective c I run the php but I am getting the echo not set

if($_POST)
{
    $test=$_POST['stop'];
    echo $test;
}
else
{
    echo "not set"; 
}
MahdiY
  • 1,269
  • 21
  • 32

0 Answers0