1

i am sending an images array from ios app to php server.at php i am getting a string containg data instead of array. how to i get array sent from ios in php.

here is my IOS CODE:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@questionaire/SaveQuestionaireAnswer", OtherBaseServerURL]];
    NSLog(@"submit ans url : %@", url);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"--------------------BOUNDARY----------------------";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // userId
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // answer
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"answer\"\r\n\r\n%@", choiceStr] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    //answere mail
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"images\"\r\n\r\n%@", ansArray] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    NSLog(@"request str - %@",[[NSString alloc]initWithData:body encoding:NSASCIIStringEncoding]);

    // now lets make the connection to the web
   submitAnsConnection = [NSURLConnection connectionWithRequest:request
                                                     delegate:self];

Here is my PHP code:

$answer=trim($this->input->post('answer'));
$images=$this->input->post('images');
echo "<pre> w/o json decode";  print_r($images); echo "</pre>";

Here is my response in php:Array ( [userid] => 165 [answer] => women [images] => ( "img2.png", "img4.png", "img5.png", "img7.png" ) )

and if i check length of array images i get result 1 instead of 4.

<?php  echo sizeof($images) ?> i am returned 1.
Monika Yadav
  • 381
  • 2
  • 12
  • 3
    what are you getting from ios side?echo and print the result here. – user2936213 Jan 07 '14 at 08:35
  • your array is not clear.check it again – user2936213 Jan 07 '14 at 09:06
  • @user2936213: here is my post array:Array ( [userid] => 165 [answer] => women [images] => ( "img2.png", "img4.png", "img5.png", "img7.png" ) ) – Monika Yadav Jan 07 '14 at 09:11
  • 1
    @user3143770 You can edit your own question. Please put more info into your question, not appending in comment. – Passerby Jan 07 '14 at 09:14
  • what is this `How to Answer`? – user2936213 Jan 07 '14 at 09:15
  • @user2936213: i have added response of php code in my quesstion. – Monika Yadav Jan 07 '14 at 09:23
  • if you what to send an array of images from ios app, try converting the image into base64 string (http://stackoverflow.com/questions/11251340/convert-image-to-base64-string-in-ios)then form json string and then send it to server its very neat and clean method to perform – CoolMonster Jan 07 '14 at 09:25
  • do you want to extract all the four names inside the images? – user2936213 Jan 07 '14 at 09:27
  • [NSString stringWithFormat:@"..%@", ansArray] will replace the %@ token with the result of ansArray.description — which is a human-readable, possibly incomplete, friendly output of the array's contents. PHP can't magically understand that output; you need to convert your images to a common binary format (jpg,png) and then convert that to a safe understandable string (json/xml/base64/your call) that PHP can handle. – magma Jan 07 '14 at 09:33

0 Answers0