Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:05:03+00:00 2026-06-09T21:05:03+00:00

EDIT : I have edited the OP with all the suggestions I got from

  • 0

EDIT: I have edited the OP with all the suggestions I got from people, so this is the latest (and its still not working).


I have the following code, which is supposed to POST some data to a .php file I have (and then to a database but that is beyond the scope of this question).

To my method, I pass an array that contains some strings.

-(void)JSON:(NSArray *)arrayData {

    //parse out the json data
    NSError *error;

    //convert array object to data
    NSData* JSONData = [NSJSONSerialization dataWithJSONObject:arrayData 
                                                       options:NSJSONWritingPrettyPrinted 
                                                         error:&error];

    NSString *JSONString = [[NSString alloc] initWithData:JSONData 
                                                 encoding:NSUTF8StringEncoding];


    NSString *URLString = [NSString stringWithFormat:@"websitename"];

    NSURL *URL = [NSURL URLWithString:URLString];

    NSLog(@"URL: %@", URL);


    NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:URL 
                                                              cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                          timeoutInterval:60.0];

    NSData *requestData = [NSData dataWithBytes:[JSONString UTF8String] length:[JSONString length]];

    NSString *requestDataString = [[NSString alloc] initWithData:requestData 
                                                        encoding:NSUTF8StringEncoding];
    NSLog(@"requestData: %@", requestDataString);

    [URLRequest setHTTPMethod:@"POST"];
    NSLog(@"method: %@", URLRequest.HTTPMethod);
    [URLRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [URLRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [URLRequest setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [URLRequest setHTTPBody: requestData];

    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:URLRequest delegate:self];

    if (connection) {
        NSMutableData *receivedData = [[NSMutableData data] retain];
        NSString *receivedDataString = [[NSString alloc] initWithData:receivedData 
                                                             encoding:NSUTF8StringEncoding];
        NSLog(@"receivedData: %@", receivedDataString);
    }

    //potential response
    NSData *response = [NSURLConnection sendSynchronousRequest:URLRequest returningResponse:nil error:nil];

    //convert response so that it can be LOG'd
    NSString *returnString = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];

    NSLog(@"RETURN STRING: %@", returnString);
}

NOTE: I have the actual URL of my website in the code, so that anyone can test if they want to.


Then there is the .php side of things, where all I am trying to do is look for any input at all (but I am getting nothing).

<html>
        <body>
        <?php

        echo "Connection from iOS app to MySQL database<BR>";

        echo '<pre>'.print_r($GLOBALS,true).'</pre>';



        ?>
        </body>
</html>

All I am really trying to do right now is confirm the data is getting through, by echoing it to the page, and then reading the page from my app (so far the data hasn’t been showing up).


The output of the .php:

 <html>
        <body>
        Connection from iOS app to MySQL database<BR>'Array
(
)
'<pre>Array
(
    [_GET] => Array
        (
        )

    [_POST] => Array
        (
        )

    [_COOKIE] => Array
        (
        )

    [_FILES] => Array
        (
        )

    [GLOBALS] => Array
 *RECURSION*
)
</pre>        </body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:05:05+00:00Added an answer on June 9, 2026 at 9:05 pm

    I have no idea about Objective-C, bu assuming you are trying to send following JSON data (i.e. JSONString is the following):

    {
        "firstName": "John",
        "lastName": "Smith",
        "age": 25,
        "address": {
            "streetAddress": "21 2nd Street",
            "city": "New York",
            "state": "NY",
            "postalCode": "10021"
        },
        "phoneNumber": [
            {
                "type": "home",
                "number": "212 555-1234"
            },
            {
                "type": "fax",
                "number": "646 555-4567"
            }
        ]
    }
    

    then your request should be something like this:

    POST /yourwebsite.php HTTP/1.1
    Host: cs.dal.ca
    User-Agent: USER-AGENT
    Accept: text/html,application/json,*/*
    Connection: close
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 624
    
    data=%7B%0A++++%22firstName%22%3A+%22John%22%2C%0A++++%22lastName%22%3A+%22Smith%22%2C%0A++++%22age%22%3A+25%2C%0A++++%22address%22%3A+%7B%0A++++++++%22streetAddress%22%3A+%2221+2nd+Street%22%2C%0A++++++++%22city%22%3A+%22New+York%22%2C%0A++++++++%22state%22%3A+%22NY%22%2C%0A++++++++%22postalCode%22%3A+%2210021%22%0A++++%7D%2C%0A++++%22phoneNumber%22%3A+%5B%0A++++++++%7B%0A++++++++++++%22type%22%3A+%22home%22%2C%0A++++++++++++%22number%22%3A+%22212+555-1234%22%0A++++++++%7D%2C%0A++++++++%7B%0A++++++++++++%22type%22%3A+%22fax%22%2C%0A++++++++++++%22number%22%3A+%22646+555-4567%22%0A++++++++%7D%0A++++%5D%0A%7D%0A%0A%0A
    

    note that Content-Type: application/json is NOT a way for posting data, as you have used, and note that data has been urlencoded, it should not be hard to this, I found this link about doing so in Objective-C: URLEncoding a string with Objective-C

    Sending above request, I got this:

    php code:

    <?php
    $raw_json_data = $_POST['data'];
    $json_data = json_decode($raw_json_data);
    print_r($json_data);
    

    result:

    stdClass Object
    (
        [firstName] => John
        [lastName] => Smith
        [age] => 25
        [address] => stdClass Object
            (
                [streetAddress] => 21 2nd Street
                [city] => New York
                [state] => NY
                [postalCode] => 10021
            )
    
        [phoneNumber] => Array
            (
                [0] => stdClass Object
                    (
                        [type] => home
                        [number] => 212 555-1234
                    )
    
                [1] => stdClass Object
                    (
                        [type] => fax
                        [number] => 646 555-4567
                    )
    
            )
    
    )
    

    which is what you want!

    NOTE: I should mention that your script at http://yourwebsite.php does not work properly, I could not even submit a normal form to it! There might be a problem like server misconfiguration or something similar, but using Apache 2.2 and PHP 5.4.4 and codes mentioned above, I got it working!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I have edited my post... Working on a project (c#), I have a
EDIT: Based on evolution of the problem, I edited this question. First of all,
EDIT I realised my question was not stated clearly enough and have edited it
EDIT: I have fixed all but two warnings now, so thank you all for
EDIT i have something like this in a file: imagecolor=0 arrayimagecolorcopy=0 arrayimagecolorcopy3d=0 when i
I won't want to have edit any working sets. I just want a way
another django question. I have a edit form like this. Look at current_status in
i have a textinput field which has autoComplete, i populate its dataprovider from a
EDIT I have finally figured out the problem i have been having on my
EDIT: I have realized the source of my problem. I only have count information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.