1

Following is my iOS code that sends NSmutable Array to PHP webservice:

 // Getting Server Address
            AppDelegate *appDelegate =
            [[UIApplication sharedApplication] delegate];

            NSString *serverAddress = [appDelegate getServerAddress];

            serverAddress = [serverAddress stringByAppendingString:@"ABC.php"];


            NSLog(@"Server Address: %@",serverAddress);

            NSData *post = [NSJSONSerialization dataWithJSONObject:UsersArray options:NSJSONWritingPrettyPrinted error:nil];
            NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:serverAddress]];

            [request setHTTPMethod:@"POST"];
            [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:post];
            [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
            [request setTimeoutInterval:30];
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];
            [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse* theResponse, NSData* theData, NSError* error){
                //Do whatever with return data

                NSString *result = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
                NSLog(@"Result : %@",result);
            }];

I want to retrive that array in PHP. How can I do that?

Here is the php which I tried but returns null:

$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
// Decoding JSON into an Array
$decoded = json_decode($jsonInput,true);

echo json_encode($decoded);
5
  • 1
    What does the log show?
    – Lithu T.V
    Commented Jun 10, 2013 at 11:48
  • Seems your method is post. Try checking the values you got using print_r($_POST); on php side.
    – Rikesh
    Commented Jun 10, 2013 at 11:48
  • Here is link for a good tutorial how-to-write-a-simple-phpmysql-web-service-for-an-ios-app Johan
    – baliman
    Commented Jun 10, 2013 at 11:50
  • print_r($_POST) returns "Result : Array ( ) 1"
    – Ali Shahid
    Commented Jun 10, 2013 at 11:56
  • but I think it doesn't send NSMutable Array to webservice.
    – Ali Shahid
    Commented Jun 10, 2013 at 12:09

1 Answer 1

0

I use a slightly different Content-Type (which shouldn't matter):

[request addValue:@"text/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

And I use a slightly different PHP, too:

<?php

$handle = fopen("php://input", "rb");
$http_raw_post_data = '';
while (!feof($handle)) {
    $http_raw_post_data .= fread($handle, 8192);
}
fclose($handle); 

$json_data = json_decode($http_raw_post_data, true);

echo json_encode($json_data);

?>

If you're getting a blank response, I'd wager you have some PHP error. I'd you check out your server's error log, or temporarily changing the display_errors setting in your php.ini as follows:

display_errors = On
5
  • $http_raw_post_data is an Array. Right?
    – Ali Shahid
    Commented Jun 10, 2013 at 12:18
  • When I do $json_data = json_decode($http_raw_post_data[0], true); in order to send first index value it returns null.
    – Ali Shahid
    Commented Jun 10, 2013 at 12:19
  • So How can I convert in PHP Array?
    – Ali Shahid
    Commented Jun 10, 2013 at 12:23
  • @ChampTaurus The $http_raw_post_data is the raw data. The $json_data is the array that was converted to an array by json_decode.
    – Rob
    Commented Jun 10, 2013 at 12:24
  • Oh yeah I got it. Thanks alot. You have solved my time consuming problem :)
    – Ali Shahid
    Commented Jun 10, 2013 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.